Have you ever played Othello? The Wikipedia has a nice article explaining the rules. It will probably remind you to other strategy board-games such as draughts (checkers) or the Chinese Go.
This example is very basic. It doesn't give weights to the different moves nor takes any strategies into account; it only calculates the available positions and the disks that must be flipped.
The player plays with the black disks and chooses one of the calculated available positions. The computer then does the same with its available positions, choosing one randomly.
Players are represented by a Boolean variable player or thisPlayer, which is false for blacks.
The class Board has two constant attributes ROWS and COLS that allow us to change the dimensions of the board.
1 – if it contains a white disk.
The different openings go here, only one is defined at the moment.
Prints the board (the board matrix) to the console, with or without the available positions.
Converts from algebraic notation to vector notation.
Calculates the available squares for a player. Returns a matrix of ones and zeros, where the ones appear in the available positions.
Launches a turn for each player. The variable noMorePos checks if both players have no more available positions, in which case the game is over.
Fancy way of disclosing the winner.
In the main class (Program) a Board is instantiated, and a loop of 32 turns is created.
Searches for a disk of the opposite colour.
If nothing is found, found is set to zero and there are no available squares. We exit the method.
Indexes (m, n) represent the adjacent squares of the position where the enemy's disk is placed, which are the positions that the payer's disk can occupy.
If the square is not empty or we get out of the board limits, the loop jumps to the next (m,n) position.
If we aren't out and the square is empty, we advance in the direction marked by (m,n), to search a player's disk. The positions in the direction of advance are given by the pairs (k,l).
To implement the direction of advance, we calculate the sign of the unit increment so that it increases or decreases automatically in the vertical or horizontal direction as appropriate. The solution is deltam = i-m and deltan = j-n.
The first pair (k,l) will start searching in (i+deltam, j+deltan) = (3,3) and will continue incrementing in that direction.
The first pair (k,l) will start searching in (i+deltam, j+deltan) = (2,4) and will continue incrementing in that direction.
An empty square is found. In this case, there is no point in continuing with the search, since we won't be able to capture an enemy's disk.
A square occupied by one of the players' disks. In that case, we will be able to capture the enemy's disks. Then, the pair (m,n) is added to the list of available positions and the found variable is incremented in one.
An enemy's disk is found. In this case, we continue searching until we find an empty square or a player's disk.
The algorithm to flip the disks is similar, but now the increments are deltam = m-i and deltan = n-j, and the indexes of the direction start in (k,l) = (m + deltam, n + deltan).
Can you improve this code? For example, how would you add weights for the available positions?