ELE 408 Final Project Chess AI game played over the Network Domenic Ferri Brandon Lian
Project Goal: The project goal is to create a single player versus AI chess game using socket programming to establish connectivity between the chess GUI and the AI engine. Detailed Design: The project is divided into three parts. First is the Chess GUI which is responsible for displaying the chess board and pieces as well as allowing the user to input moves and play the game. The Chess GUI was completely programmed from scratch using a Python package called Pygame. The GUI is them loaded onto the FPGA board and executed using the ARM processor. Next is the chess engine which is responsible for playing against the player by choosing optimal chess moves using basic AI theory. The chess engine was found online as an open source program that we modified slightly in order to use in this project. The engine is completely written in C programming language and is run from a desktop computer. Finally, the socket programming is used to establish connectivity between the GUI and engine. The GUI sends the current state of the game to the engine over the network, and the engine sends its chosen move back to the GUI. In order to create the chess GUI, we used the basic concept of giving each square number 0 to 63. This allows us to track the position of pieces by assigning a number to a piece. The piece positions are stored in a list which is updated when pieces are moved and captured. We also use the numbers to track mouse clicks so we know which piece was clicked on. A 2 dimensional list called current keeps track of the state of the game. The list has an element for each piece remaining on the board. Each of these elements has 3 of its own elements that state the name of the piece, position, and an array of legal moves. In order to find a list of legal moves, the rules of the game and the movement of each piece are hardcorded, and the conditions for each possible move are checked. For example, a Pawn can only move one square forward unless it has not moved before, in which it can move two squares forward. It can move diagonally only if capturing. Using the numbered squares, we know the one square forward is always the current position plus 8 for black and current position minus 8 for black. We then check if that destination square already has a piece on it. If it does, then it cannot move to it. The overall concept is that the rules of the game for each piece are used to generate a list of legal moves for each piece remaining. When a user makes a move, the GUI cheks if the move is in the list of legal moves. In order to send the user move to the chess AI, we need to translate the move into Universal Chess Interface convention. UCI used columns labeled A to H and rows labeled 1 to 8 to identify each square instead of using just numbers. Therefore, we translate the square's number to UCI convention before sending it. The move is sent over socket programming which establishes communication between the GUI and Engine. The socket program allows the chess AI to run on any computer on the same network as long as the ip is known. It also stops any unneeded information from the communication, such as extra white space, so that the GUI has
an easier time processing the machines moves. The GUI code sends different commands to the chess engine in order to begin a new game, restart the game, and more. The chess AI uses a classical approach to determining the best move by using a combination of the Minimax algorithm and alpha beta pruning. The engine sends the move back over the socket program to the GUI, which updates the game state with the new move and waits for the players next move. Procedure for running project: A short video run down/explanation is given here: https://www.youtube.com/watch?v=iofd9j3bky4 However these instructions below are more in depth and should be followed. * = may vary/from here on (like the use in linux terminal) Follow instructions in this exact order: 1. Open Terminal on a linux desktop computer Cntrl+alt+t 2. Make a desired directory for files to go to: Mkdir AI_Chess_Over_Network cd AI_Chess_Over_Network 3. Copy and unzip ELE408_FinalProj.zip Unzip ELE408_FinalProj.zip 4. Like in the lab, insert your microsd card and start up the DE1-SoC board running LXDE linux 5. In a separate terminal run connect to the DE1-SoC and copy the ChessBoard/ directory to the DE1-SoC board screen /dev/ttyusb0 115200 mkdir AI_Chess_Over_Network cd AI_Chess_Over_Network scp -r hostname@hostip:~/ai_chess_over_network/chessboard/. 6. Install pygame packages on DE1-SoC board As root: apt-get install python-pygame (make sure glibc6 is version < 2.16, I believe this the lib version on the boards before hand is 2.15, but in our development process we had updated to 2.17 and then had to reinstall 2.15 in order to get the GUI running, if you have this issue you can download
libc6_2.15-0ubuntu20_armhf.deb to you desktop from https://launchpad.net/ubuntu/quantal/armhf/libc6/2.15-0ubuntu20 and copy the file from there to the DE1-SoC board: On DE1-SoC: scp -r hostname@hostip:~/(directorypath)/ libc6_2.15-0ubuntu20_armhf.deb. and can run As root on DE1: dpkg --install libc6_2.15-0ubuntu20_armhf.deb From whatever directory you copied this package to on the DE1-Soc) 7. In the terminal for your desktop go to: cd MisterQueen-master/bin/release 8. In this same terminal run: ifconfig 9. From enp0* copy the inet addr: IP address for use in the programs 10. In this terminal run the python script uci.py and paste in the IP address when asked, this is the server side python uci.py Input IP Address:(ip address goes here) 11. From the terminal that is running screen, the terminal for the DE1-SoC board, move into the ChessBoard directory and run the chess.py script and paste in the same IP address when asked, this is the client side cd ChessBoard/ python chess.py Input IP Address:(ip address goes here) 12. On the server side terminal you should see that you have connected to the client IP, and the initializations taking place for the game. On the DE1-SoC you should see the GUI appear on the touchscreen with all the chess pieces in their starting positions 13. You can use the touch screen as mouse inputs to move the pieces, however we recommend connecting a mouse to the DE1-SoC because of the inaccuracy of the touch screen 14. If you are not familiar with the rules and moves involved with chess here is a link to learn: https://www.chess.com/learn-how-to-play-chess 15. You can move a piece by clicking the piece, and then clicking the square you would like to go to. If you move a piece on the DE1-SoC GUI you should see the move received by
the the server side, the script sends that move to the engine and prints out what the best move will be 16. The best move will be sent to the DE1-SoC GUI and update the board with this new information 17. If the game is over, either one side has no pieces left, or the king from either side has been taken, press the New Game button on the DE1-SoC GUI to reset the board and will also restart the game on the engine side. Was our Best Outcome met? Originally we planned to have the chess engine operate on the FPGA board and that would act as the server. We now realize the better option is to use the FPGA as the platform for the GUI, and the desktop which has more computational power and resources as the server side, you can see this decision was made in our final product. We also wanted to work with machine algorithms so that the engine would get better and better, but due to time constraints and lack of familiarity with chess engines we made the executive decision to use Michael Fogleman s chess engine MisterQueen that was written in C to expedite development. We do have a chess game that communicates between a GUI on the DE1-SoC as a client and an open source chess engine on the desktop as our server using socket programming. Our Best outcome was not met because we did not implement and integrate deep learning algorithms. Features for Future Development: Adding functionality for determining check and checkmate states, currently the AI just takes our king and we restart the game, or we take his king and we restart the game Ideally we would like to modify the engine further to use machine learning algorithms to constantly get better at the game Adding levels of difficulty so that we can invite beginners to play the game and challenge the most hardened of veterans Animations would be really nice, the refresh is a little hard to follow so implementing smoother transitions from square to square would be visually pleasing Adding some music would be fun as well, having a good combination of audio and visual is necessary for a good game
Conclusion: In conclusion we learned a lot throughout the process developing this project and bringing it to life. We learned a lot about writing scripts in python developing the GUI, using python for socket programming and how to interact with a C program executable inside of the python script, and learned about deterministic chess engines and the methods they use in order to complete computations and determine what the best move to make is. We would like to thank Michael Fogleman for producing his open source chess engine, MisterQueen, and allowing the community of developers to use and learn from his research and development.