Transcript
Documentation This is Project 3 of 15-466 – Computer Game Programming Game: Chess Implemented:
Basic chess logic, without promotion of pawns and castling
Can be played without connecting to network. In this case, it is in single-player mode, against a basic computer ai
Can be playing over the network, against other people, with 2 instances of the game. (so multiplayer)
Can have more than 1 game going on over the network, as long as there are multiple hosts
Also, a chat server that allows people in the same game over the network to talk to each other. If you are not connected to a game, then you can only talk to yourself. I.E., this is a private chat limited to the players playing against each other in a game right now.
Basic Chess Logic
Set up the chess board and pieces to start the game. Had to code the logic for valid and legal movements of each chess piece on the board. All of this followed the normal rules of chess for each piece. Also had to do checks for whether a player is in check or checkmate. If he is in check, then as per usual, he can move any piece as long as it brings him out of check, else it is considered invalid. If he is in checkmate, then gameover.
However, I did not manage to set up promotions of pawns to other pieces, nor was I able to implement castling yet.
Chess AI in Single-Player Mode
I used a minimax search with alpha-beta pruning in order to search through the game tree. Currently, the AI searches at a depth of 5. The Ai works like this. It first takes the current state of the game and makes it the root node. Then, for each piece belonging to current player, it finds all legal moves for each piece, makes a new node for all the moves, and adds them as children to the root node.
I then run the alphabeta function on each child node to return a best-value for each child node. I then take the node with the max value out of each child node, and have that be the best move for the ai to take. So technically, my search tree has depth 6.
The main strategy of the AI takes place when evaluating the value of each state. I.e, assigning a weight to each moves. For the current player's turn at each node, he is a maximizer, so we want to assign positive values to situations and scenarios which are beneficial for him and negative values to those which are not. Currently, if the player can check or checkmate the opponent, extremely high positive values are assigned to the state and vice-versa if he will be checked or checkmated. Also, each chess piece is assigned a value, and the maximizer wants to maximize the value of his pieces, and minimize the values of his opponents. This makes the AI more likely to attack and capture the player's pieces, in order to minimize the value of the player's pieces. Also, a random number is added to the weight each time, but it is a small random number, so this helps the ai pick between states which have similar scores.
Soo.. game AI is not that smart yet... good chess AI actually takes a lot of research and also being good at chess...
Chat Server and Networked Multiplayer
The chat server works by pressing the enter button, which loads up a text input box to type into. However, if you are not in an online game, then you only chat with yourself. The chat server is meant to be a private chat between you and your opponent online.
Each player can host a game over the server, and a max of 2 players can be in each hosted game, the host and 1 opponent. So whenever someone hosts a game, their game shows up on the hosts list window, and can be joined by clicking on the button. Whenever a player joins a hosted game, the game then starts, with the host as white and guest as black.
In an online multiplayer game, each player has the option to surrender the game, request a draw, and reject or accept a draw request. When a player surrenders, a draw is reached, or one of the player checkmates the other, the game is over, and the guest can either wait for the host to restart the game, or the guest can leave the hosted game to join some other game.
So the guest can immediately leave a game whenever he wants, causing that game to be reset and any other online player can join the game. The host also
has the option of kicking the guest out immediately, leaving the room open for other players.
Both players can use the chat here to talk to each other.
Both players actually have their own instance of the game. Whenever the guest makes a move, he sends his move to the host. The host instance validates the move, plays the move on its own instance, and then sends that move back to the guest so the guest can play the move on his own board. So basically, we are doing remote procedure calls, not state synchronization.
The guest sends all the info to the host, who decides what to do with it and then tells the guest instance what he should do on his board.
HOW TO RUN Single-Player:
Simply start the game, don't click on the host a game button, and don't join any hosted games. Then, you can simply start playing immediately by clicking a white piece and moving it. The human player is always white. You can't make invalid moves, and whichever piece you want to move is marked as red. Click on the piece you want to move and then click on a square, and it will move there. You can also chat with yourself here if you want...
There is a reset game button on the right, which resets the game against the AI, whenever you want to, and when the game is over.
MultiPlayer:
Either host a game or join a game. Click on the host a game button to host a game. Then, anyone running other instances of the game over the same network, can see your hosted game by clicking the show host list button. You will be assigned a random user tag. Click on one of the host buttons to join their game. The host can't make any moves on the board till a player joins.
Once both players are in, the game starts, with both players only allowed to move their own pieces, on their own turns, and only legal moves. Players can chat with each other by pressing the enter button. Also, they can surrender the game by pressing the surrender button or request a draw by pressing request draw button. The guest can leave the room and host or join other games, while the host can kick the guest out, but can only quit his game by closing his instance of the game.