Can somebody explain this code?

Hi - I am going through a book about programming and we're doing tic-tac-toe. Here are the two functions I found puzzling.

void displayBoard(const vector<char>& board)
{
cout << "\n\t" << board[0] << " | " << board[1] << " | " << board[2];
cout << "\n\t" << "---------";
cout << "\n\t" << board[3] << " | " << board[4] << " | " << board[5];
cout << "\n\t" << "---------";
cout << "\n\t" << board[6] << " | " << board[7] << " | " << board[8];
cout << "\n\n";
}

char winner(const vector<char>& board)
{
// all possible winning rows
const int WINNING_ROWS[8][3] = { {0, 1, 2},
{3, 4, 5},
{6, 7, 8},
{0, 3, 6},
{1, 4, 7},
{2, 5, 8},
{0, 4, 8},
{2, 4, 6} };
const int TOTAL_ROWS = 8;

// if any winning row has three values that are the same (and not EMPTY),
// then we have a winner
for(int row = 0; row < TOTAL_ROWS; ++row)
{
if ( (board[WINNING_ROWS[row][0]] != EMPTY) &&
(board[WINNING_ROWS[row][0]] == board[WINNING_ROWS[row][1]]) &&
(board[WINNING_ROWS[row][1]] == board[WINNING_ROWS[row][2]]) )
{
return board[WINNING_ROWS[row][0]];
}
}

Specifically, what is going on with the notation such as:

(board[WINNING_ROWS[row][0]] == board[WINNING_ROWS[row][1]])

and why does it return board[WINNING_ROWS[row][0]]?

(as soon as two dimensional arrays pop up with brackets I start getting confused).

How would your read "(board[WINNING_ROWS[row][0]] == board[WINNING_ROWS[row][1]])?

Would is be something like "when the vector board with the two-dimensional array called WINNING_ROWS of type int at [row][0] equals the vector board with the two-dimensional array called WINNING_ROWS of type int at [row][1]?

Also, where/what is [row][0]? How do you say in English what that refers to?

I hope you get the gist of my question. I'm having a really hard time visualizing what exactly is happening and where the [row][#'s] are referring to. So if you can put this in layman terms that would be great.

Thanks.

Matt
Does no one use code tags anymore? Here, lemme help you out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void displayBoard(const vector<char>& board)
{
cout << "\n\t" << board[0] << " | " << board[1] << " | " << board[2];
cout << "\n\t" << "---------";
cout << "\n\t" << board[3] << " | " << board[4] << " | " << board[5];
cout << "\n\t" << "---------";
cout << "\n\t" << board[6] << " | " << board[7] << " | " << board[8];
cout << "\n\n";
}

char winner(const vector<char>& board)
{
// all possible winning rows
const int WINNING_ROWS[8][3] = { {0, 1, 2},
{3, 4, 5},
{6, 7, 8},
{0, 3, 6},
{1, 4, 7},
{2, 5, 8},
{0, 4, 8},
{2, 4, 6} };
const int TOTAL_ROWS = 8;

// if any winning row has three values that are the same (and not EMPTY),
// then we have a winner
for(int row = 0; row < TOTAL_ROWS; ++row)
{
if ( (board[WINNING_ROWS[row][0]] != EMPTY) &&
(board[WINNING_ROWS[row][0]] == board[WINNING_ROWS[row][1]]) &&
(board[WINNING_ROWS[row][1]] == board[WINNING_ROWS[row][2]]) )
{
return board[WINNING_ROWS[row][0]];
}
}
WINNING_ROW is an array of eight arrays.
I'll use the second entry of WINNING_ROW so the values don't get confused with the indices.

The second entry in the WINNING_ROW array (that is, WINNING_ROW[1]) is an array of three values, {3,4,5}.

So WINNING_ROW[1][0] is 3 (the first value), WINNING_ROW[1][1] is 4 (second value) and WINNING_ROW[1][2] is 5 (third value).

This code checks whether the characters in the board vector at the indices given by each of the eight WINNING_ROW arrays are the same. That is, all 'x' or all 'o'. The code also makes sure three empty positions are not mistaken for a win.

1
2
3
4
5
6
7
8
9
for(int row = 0; row < TOTAL_ROWS; ++row)
{
if ( (board[WINNING_ROWS[row][0]] != EMPTY) &&
(board[WINNING_ROWS[row][0]] == board[WINNING_ROWS[row][1]]) &&
(board[WINNING_ROWS[row][1]] == board[WINNING_ROWS[row][2]]) )
{
return board[WINNING_ROWS[row][0]];
}
}


For WINNING_ROW[1] this code reduces to:

1
2
3
4
if ( (board[3] != EMPTY) && (board[3] == board[4]) && (board[4] == board[5]) )
{
return board[3];
}


What the function returns is the character stored at board[3]. So it returns 'x' or 'o', whichever won the game.

Last edited on
That clarified things very much. and to answer residentbiscuit I didn't know about code tags :( - (but now I do).

And thanks for the transparent explanation. It made it much easier to visualize.
Topic archived. No new replies allowed.