Two-Player Java Chess Applet With Chess Board Evaluator
J. R. Gray
Computer Science 3361
Spring 1998
About The Applet
This chess applet began its life as an assignment in Computer Science 1502 in Summer 1997. The underlying engine of the application was written in Java and was designed as a console application. Later in the quarter, graphics were added and the code was rewritten as an applet.
As part of my CS 3361 project, I resurrected the applet and added to it a board evaluator which uses most of the same evaluation function items as intelligent chess applications. As with most game applications, the basic tree search and alpha-beta pruning remain constant, but it is the evaluation function which determines the success of the application. The more factors considered, and the more precise the evaluation function, the more likely it is the application will choose the best board.
The Evaluation Function
This applet borrows most of its evaluation methods from a combination of the methods used in Deep Blue, IBM's world-renowned chess computer, Green Light, Windows-based chess application which has seen some success in tournament play and Diep, a popular Windows chess application which has defeated two chess grandmasters.
Each of those applications' evaluation functions were examned and compared against each other and against research material from the Chess world. There was almost unanimous opinion on assigning point values to pieces, so the program uses the following values when calculating the material score ( the total number of pieces a player has in play ):
A total material score is obtained by subtracting the opponent's material score from your material score. The program implements this scoring by simply traversing the chessboard and counting the number of pieces each side still has in play, just as a human would do and multiplying the count by the appropriate factor.
A position score is also calculated for each player. The position score is a calculation of the number of places on the board controlled by a player's pieces. It is determined by assigning one point for every valid move that a piece can make. A total position score is obtained by subtracting the opponent's position score from your position score.
The program implements this by looking at each piece and determining the number of legal moves it can make by looking at all possible moves from its current position which are legal and valid. This same function would be used later when the tree-search algorithm is added to allow a computer player.
The application provides a ten point bonus for rooks advanced to the seventh rank. Advancing rooks to the seventh rank is considered important in chess, particularly if the opponent's king is still on his back rank. By placing a rook on the seventh, as it is called, the King has difficulty advancing down the board to maneuver away from another attacking piece. An additional ten point bonus is assigned is a player advances both of his rooks to the seventh, since they virtually constrains the king to the back rank. This bonus is implemented by looking at the seventh rank for the appropriate player and determining if there is a rook on that rank.
The program also provides bonuses for putting rooks on open files, which are files with no other pieces on them. This is good chess strategy since it allows a player to advance his rooks as needed either to attack or defend. This is implemented by finding the player's rooks and then examining the rest of that file. In no other pieces are found, then a five point bonus is given. An additional five point bonus is given if both rooks are on open files.
A 20 point bonus is given if it is possible for a king to castle on a given board. This means the king and rook have not moved and there is clear space between and them. (In tournament chess, there are other castling rules as well, such as preventing the king from castling into check or preventing a castle if any of the spaces between the rook and king can be attacked by another piece. These rules are not implemented since the point of the program is not to prevent the player from making dumb moves, only to alert him of the score of the board.) Castling is given such a high bonus because it is the number one way of getting your king out of harm's way, particularly against on opponent playing center position.
This application calculates several pawn bonuses and penalties based upon pawn structure. These bonuses / penalties are based on the concept of trying to develop wholes, which are weaknesses in the enemy pawn structure. Wholes are squares on which a piece can be placed which cannot be driven off by an enemy pawn. Wholes are critical to establishing pieces on powerful squares. Imagine you have placed a knight on a square where you would like it to stay. If your opponent threatens your knight with a piece, you have the option to protect it with a piece. If exchanges occur, you will be left with a piece on the critical square. However, if your opponent threatens the knight with a pawn, the knight must move, and you will lose the advantage of having a piece on a poweful square.
There are two commons ways in which wholes are created. The first is when pawns become isolated. An isolated pawn is one which does not have another pawn in the file immediately left or right. When a pawn has been isolated, the square immediately in front of it becomes a whole, as no pawn can force a piece from landing on that square. This application looks at the pawns and their neighboring files and provides a five point penalty for every isolated pawn. The other way wholes are created is when pawns are backwards. A backward pawn is one which has no pawns on the neighboring files which are either equal to or farther back than it is. At the beginning of the game, neither side has any backward pawns, as every pawn has at least one neighbor which is equally as far advanced as it is. However, if white played its b pawn forward on the first move, the a pawn would become backward, as its only neighbor would be in front of him. In such a situation, the whole is created immediately in front of the pawn, as again, even though there is a neighboring pawn, that pawn has no hope of driving away a piece which settles in the critical square. A five point deduction is made for every backwards pawn. This is determined by looking at each pawn and determining if there are any pawns even or behind it.
There are also five-point pawn bonuses given for passed pawns. A passed pawn is one that is sufficiently advanced that no enemy pawns are on neighboring or adjacent files. These pawns cannot be captured by other pawns. The program looks at each pawn and examines if there are any enemy pawns on the same or adjacent files.
A ten point bonus is awarded for any pawn on the seventh rank that is capable of moving onto the back row on the next move. Such a pawn is inline for promotion and therefore will soon be another, more powerful piece. (For simplicity, in this chess program, the pawn is always promoted to a Queen. This is typical and doing so allowed me to restrict user input to simple mouse clicks rather than asking which piece the player wanted to promote his pawn to.)
Center coverage has long been considered crucial to good chess. Center position is of special importance in developing knight and bishop positions. A knight placed in the center of the board controls eight squares, as opposed to four from its starting position. The bishop, like the knight, becomes more powerful once it is moved off its starting square. On its original square, the bishop controls seven squares. Placed in the middle, the bishop controls thirteen squares. The application looks at the center square and adds a one-point bonus for each piece the player has in the center of the board. This bonus is in addition to the points already awarded for positions controlled.
Because of my limited Java knowledge at the time the application was originally written, there are some serious inefficienies in the game player which hinder its performance. First of all, the chessboard is stored as a two-dimensional array of chess pieces. The chessboard itself knows nothing about the pieces and must query the pieces as it needs information. This means that for many of the evaluation functions, the applet must search through the array to determine if files are open or closed or what not. It would be obviously easier for the chessboard object to just have a piece array with each known piece it in and then just detemine as it needed which pieces were in which location.
The Future Of The Application
First of all, a computer player needs to be added. All of the pieces, pardon the pun, are in place, with the exception of the game tree for choosing a move. We have an evaluation function and the ability to determine all the possible moves for a given board. All we need is a way to search all the successor boards down to a depth set by the user. Before implementing this, the inefficient use of memory addressed above needs to be corrected, since a search to a depth greater than one was simply too memory-intensive. That's why the computer player feature was removed.
As an artificial intelligence project, this program is lacking. The evaluation function serves to embed substantial knowledge in the applet, making it much smarter than the average chess player and certainly more capable of selecting a good next move. Encoding even more intelligence into the application would be a priority. For example, the bonus point rankings are based on statistical sampling of other chess programs. But who is to say they are right? Ideally, the program would be capable of adjusting its bonus points figures during the game if it realizes it is making poor moves.
For example, if the computer found itself at a man disadvantage, it might want to increase the weighting given to the material score so that it would choose boards that would increase its material score. Or, it might choose to play a positional advantage game by placing its pieces in key positions to compensate for the disadvantage. This is the kind of thing an intelligent application would do.
There are a number of additional chess rules to be implemented. This program does not support en passant passing, which is a minor flaw. En passant should be added. Additional castling rules would need to be added so a computer player does not make an illegal move. Likewise, the program does not force a player in check to make a move out of check. This is required in tournament chess. The program does not detect checkmates or stalemates, which it would need to do.