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.
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.
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:
Print out Letters (top row)
print out first number
print grid row
print out first number again
loop ends when counter hits 8.
Print out Letters again(bottom row)
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.