Wednesday, October 7, 2009

Introducing the concepts of game state and move.

To understand the alpha4 phase requires a strong familiarity with fen positional notation.

First a little housekeeping. I ended up re-writing my Utils class. No new functionality, but I now use HashMaps and TreeMaps for all the mappings between the various co-ordinate systems. This eliminates a whole mess of methods using switch() statements, and leaves us with two primary methods in the Utils class.

toFen( int[] ) - converts an integer based position array to the position field of a fen string. This is called by the toFen instance method of the GameState object.

toArray( String ) - converts the position field of a fen string to an integer based position array. This is called by the constructor of the GameState class in order to build its position array.

Also, the Maps are initialised on start-up via a method call from the Table constructor.

In the last installment, we added the ability to drag and drop the pieces, with no restrictions. Today we have added the following restrictions in the Table class...

1. in the mousePressed method, a boolean condition to prevent either side from moving out of turn.

2. in the mouseReleased method, code to prevent a piece from being dragged off the board. This code checks that the xy co-ordinates of the mouseReleased event fall within the bounds of the board (JPanel)

3. If conditions 1 & 2 are satisfied, then in the mouseReleased method there is a move legality test. Currently this test only prevents a piece being dropped on a square occupied by another piece of the same color.

In support of these developments, we have added four new classed.

GameState - GameState objects encapsulate all of the information needed to describe the current state of the game, including the position.

Game - a class to keep track of all game related issues, such as the current game state, a list of all the moves played( currently unused ). This class will grow significantly over time.

Move - an object to represent a move. Includes source square, destination square (both are indexes of the position array) and the type of piece moved.

MoveList - a class which extends ArrayList, each element contains a move that is played on the board. Currently this class is unused.

Also, one critical method was added to class Utils. This is boolean isLegal( GameState, Move ). This is the aforementioned legality check called from MouseReleased. The next phases of development will revolve around this method, as we attempt to define what constitutes a legal move. This is a complex task, involving the properties of the pieces, defining check, checkmate and stalemate, rights to castle and capture en passant, and other issues.

At this point it is worthwhile to trace the sequence of events when the program is loaded.

The main starting point is a call to the Table constructor from Fchess2, the main container class. Currently the following steps are performed by the constructor...

1. A call to a method which initializes all the HashMaps for the co-ordinate system mappings.
2. A call to build and display the GUI, including an empty chessboard.
3. A call to reset(), passing the starting fen. reset() performs steps 4 - 7.
4. resets various flags and variables
5. passes the fen string to the Game constructor, which in turn passes the fen to the GameState constructor.
6. assign the posn array of game.state to the position array of the board.
7. calls board.repaint() to draw the position denoted by the original fen parameter.

This reset() method is called from two other places...

1. the event handler for the reset button calls reset( startFen );
2. the event handler for the fen button calls reset() passing the fen string pasted by the user into the text field.

OK, that's it for now. As mentioned earlier, the next phase is to define what constitues a legal move. The approach will be to write a method that generates all possible legal moves for a position, then we check to see that the move being attempted by the dragging of pieces exists in the possible moves list.

As always, an updated applet and current source code is available on the fchess2 website.

No comments:

Post a Comment