Posted on

Aint no playin chess round these parts! Day 1


Today I started working on a console version of the chess project that many of us will have to face down in just a few months…

Ok, nuff talk, lets get to it:

Today I focused just on getting the interface done. Meaning that I wanted to be able to place my pieces on the board and have the show their positions.

Creating the grid:

The first thing I had to tackle was just how I was going to display the board. This is easier said than done because there are a lot of ways to do this. One of the ways I tried was to have a for loop do a string of ‘_’ characters followed by a newline character and then a string of “| |” characters. This was easy to implement, but will be a pain in the placing pieces department. So I opted for something way simpler. A 8X8 array of strings. The strings will consist of two brackets and a space in between them. That will serve as the grid, and it’ll be incredibly simple to generate and place. I’ll touch on that in a minute.

The codez:

So basically to make this work there are two functions.

An init function which runs when the program first runs, and a draw function which draws the interface:

The init function is very simple:

At the beginning of the code I created a string array, as in an array of strings. I made the array 8 X 8, the length and width of a chess board. All I needed to do was be able to dynamically create the board. So what the init function does, is place the string “[ ]” into each element of the 8 X 8 array. That’s it.

The draw function simply outputs every element of the array.

Classes:

For now I’ve kept the classes very simple, all of the classes follow the same skeleton for the time being. All they contain is their X and Y position, a function to change the position, and functions to return the X and Y position of the piece to main() , they also each contain a constructor which takes in an X and Y value which sets the piece’s position when it is first placed.

Placing the pieces:

Ok, so once I have instantiated each piece and given it an X and Y value. I have a place function which simply places the piece in the correct position on the grid. To do that, I simply get the X and Y coordinate from the piece and then replace the “[ ]” put by the init function with a symbol corresponding to the piece’s type so, a pawn is “[P]”, king is “[K]”, etc.

Getting the letters and numbers:

So as you guessed, the letters and numbers are not part of the string array. What I did instead was mess with the draw function to add these characters on the fly:

PseudoCodez:

Print out Letters (top row)

loop:

print out first number

print grid row

print out first number again

loop ends when counter hits 8.

Print out Letters again(bottom row)

That’s it.

Here’s the main function so you can see how all of this works:

That’s all I’ve done so far. Stay tuned for day 2.

Advertisements

About boringtechie

Hello, I'm the boring techie! So you've landed yourself on this site due to me or someone else linking you here, and you're wondering: "What does this kid do, and why should I even care?" Right? Of course you are. Basically, I'm a Techie, though I'm not as boring as the title says, that's one of those things where the word means the opposite of what's REALLY going on... Anyway, I write tuts and make commentaries. That's about it...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s