enum { PLAYER_X = 'X', PLAYER_O = 'O', AVAILABLE = ' '};
An enumeration is a type that contains discrete values. So this declares a type that has three values: PLAYER_X, PLAYER_O, and AVAILABLE.
Have you seen the type
bool
yet? bool is very much like an
enum bool {false=0, true=1};
board[r][c] = AVAILABLE
Board is defined at line 103. This statement is assigning the enum AVAILABLE to an item in the array.
char player = (turns % 2 == 0 ? PLAYER_X : PLAYER_O);
This creates a single character called "player". The expression after "=" is the value that gets assigned to player initially. It's sort of a way to do an if-then-else inside an expression. If
turns % 2 == 0
then this assigns PLAYER_X to player. Otherwise it assigns PLAYER_0 to player.
legal = isLegalMove(row-1, col-1);
It looks like the user can enter a column or row number between 1-3. But in C++, the first element in an array is number 0, not number 1. So it looks to me like isLegalMove() expects its row and column numbers to be "C++ index values" an not "user input values."
This presents a good opportunity for you to make a change. Rather than having "row" and "col" be in "user coordinates," try converting them to "C++ coordinates" immediately after the user inputs them by subtracting 1 from each. This way all of the code deals with C++ coordinates and you won't have to remember that these variables are an exception.
1 2
|
if (board[r][0] == player && board[r][1] == player && board[r][2] == player)
return true;
|
This says that if columns 1, 2, and 3 in a single row (row r) are all occupied by the player, then player wins. More generally, the for loop at line 85 checks if the player has won by getting all three columns in any row. The loop at line 90 checks if they have all three rows in any column, and the code at lines 95 and 96 checks if they either diagonal.
Hope this helps,
Dave