http://pastie.org/p/2ItNh0Y93uOI9IEOxiEmzP/raw
here is a link to the code!
In another post i made the following explaination, feel free to read it if you care:
If youre talkign about the part near the end with all the one letter variables. I have absolutely no clue. I know its derived from this: Micro-Max (hccnet.nl) . Supposedly the smalles possible chess engine for arduino.
Most other things I did write myself. Most important of which are:
void reedLezen() ;
This bit of code is meant to read the 64 reed switches under the tiles that are supposed to sense the pieces through magnetism. S0-3 are the inputs for 4 daisy chained multiplexers. Sig1-4 are the outputs. These outputs are saved in the 8x8 "lezing"(meaning reading in Dutch) boolean matrix. A true/1 means that the sensor was active and thus a piece is present on that square.
After that the matrix is rotated 90 degrees because im dumb and installed the multiplexers wrong.
any time a move is confirmed the reedlezen matrix will be copied to board[8][8]. This is to save the board.
void playerMove() Is the bit of code that is supposed to determine what piece the player has moved. first reedlezen() is called to save the current situation in the lezing matrix. Then with two for loops its determined if any part of the lezing matrix is different from the bord matrix. If so that means a piece has moved since the last time the board was saved. If the reading from that specific sensor is 0 that means that there used to be a piece and no longer is. Meaning thats the origin of the piece that the player has moved. The mess of strings that happens after that is there to save the move into. In chess moves are written like "a2a3" for example.
The next bit is a bit of a mess but works surprysingly well. I wont explain the whole thing but the first if statement checks if there is more than one piece missing. If that is the case the game switches to "attack mode" where it now waits for the player to return a piece to one of newly empty squares. That square is then the destination of the piece. The piece that was previously there is captured.
If there is more than 2 pieces missing the screen will display an error. The code loops intill Ystop (one of the buttons next to the display) is pressed to confirm the players move.
The reading will be copied to the c[4] matrix to be input to the chess algo.
Then through some magic i dont understand the code will either output that the move you made was illegal, or the move it made itself.
void translateAImove(byte xo, byte yo, byte xd, byte yd)
After the chess algo has produced a move translateAImove is called to convert the caracter values to integers that can be used by the moving mechanism.
Movepiece() Is called with a x and y origin of the piece and x and y destination of the piece. It moves to the origin, enables the magnet and then moves to the destination after which the magnet is disabled again.
Moving itself is done with MoveTo() Which i think is pretty self explanatory.
The other void statements are all for running the OLED screen and are also self explanatory.
Id love to learn about more ways for me to improve this code. Thank you so much