Knoughty was written as a simple demonstration of the alpha-beta search algorithm. Because noughts and crosses is a simple game, a perfect alpha-beta search is possible and can also be done very quickly (on my Palm Zire 72, it only takes a fraction of a second to make each move.) I have written the program much like a chess program (with functions for move generation, evaluation and searching as well as a simple bitboard board representation) and I think it is a good place to start when planning to write your first chess (or any other board game) program.
And how does it work?
First a note: the snippets of code shown will be in C/C++, but it shouldn’t be too difficult to follow if you’re used to a different language. I will also use a bit of Boolean algebra, click here for an introduction of the subject.
Knoughty uses a simple bitboard approach to represent moves. The board is defined as follows:
int side; //side to move
int board; //occupied places
You will notice that the board is comprised of two integers. The one is to represent the positions of the noughts and the other one is for the crosses. At start of play, both boards equals zero. When a player makes a move, a specific bit of the board (say board for noughts and board for crosses) is set. The setting of the bits work as follows: The board is numbered as follows: 678 345 012 If a player makes a cross on position 0, we set bit number 0 (the Least Significant Bit) of the board to one (e.g. board=000000001b). If the opponent now makes a knot on position 4, we set bit number 4 to one (board=000010000b). If the first player now makes a cross on position 2, we also set bit 2 of the cross board (board=0000000101b). This will go on until the game is finished.
Knoughty knows all possible 3-in-a-row winning combinations, they are declared as follows:
Int threeInRow =
0x007,0x038,0x1c0, //3 in a row – horizontal
0x049,0x092,0x124, //3 in a row – vertical
0x111,0x054 //2 diagonal
To know if a side is winning, it simply AND the board of that side with all the possible winning combinations. If the resulting board equals the winning combination, it knows that there are 3 in a row and returns a winning score, e.g. If there is a cross on positions: 0,4,5 and 8 (board=100110001b) First it tries threeInRow: threeInRow AND board = 000000111b AND 100110001b = 000000001b 000000001b does not equal threeInRow, thus it tries the next winning combination. This will keep on until it gets to the second last combination (threeInRow): threeInRow AND board = 100010001b AND 100110001b = 100010001b 100010001b equals threeInRow, thus it returns a winning score.
Knoughty uses the alpha-beta algorithm to decide what move to play. Because there is no winning or losing move to start with, the first is decided on by random (just to make things a bit more interesting). It can be very rewarding to play around with the alpha-beta algorithm and see the effect of small changes. Some ideas to try: · First make a function to show the number of nodes searched (tip: add a counter to the “makeNextMove” function) · Change the search to Minimax and see the nodes searched increasing! (Tip: remove the line: “if (value>=beta) return value;”) · To see more drastic results, remove the code that make a random move and let it also search the first move.
And that’s about it! Feel free to play with the source code and maybe even turn it into a chess program!