Explain this function for me please

i know what it does, i just don't understand the flow of the loop, could someone please explain that loop? btw that function is part of the tic tac toe game determines who's the winner, this whole const vector<char>&&&&&&&&&&&&& is pissing me off, really overwhelming :/



char winner(const vector<char>& board)
{
// all possible winning rows
const int WINNING_ROWS[8][3] = { {0, 1, 2},//top line
{3, 4, 5},//middle line
{6, 7, 8},//bottom line
{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]];
}

}

// since nobody has won, check for a tie (no empty squares left)
if (count(board.begin(), board.end(), EMPTY) == 0)
return TIE;

// since nobody has won and it isn't a tie, the game ain't over
return NO_ONE;
}
Hi !
I think its no big mystery !
all possible winning combinations are coded in the 8 rows of the WINNING_ROWS array.
the loop simply checks evey possible combination and thats it.

Or did i get your question wrong ?!

Bye,

Nils
Topic archived. No new replies allowed.