Public Group

# c++ code question

This topic is 3687 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I got this code and I can't understand what exactly it does. I think, there is a two dimensional array WINNING_ROWS, and the for cycle checks if it suits combinations in the array, but how exaclty is this happening? 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; 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]]; } }

##### Share on other sites
From what I can tell from what you posted, it's a Tic-Tac-Toe game.

The WINNING_ROWS constant contains an array of the board positions for a winning game.

The Tic-Tac-Toe game is laid out like this:

0|1|2
-----
3|4|5
-----
6|7|8

{(0,1,2), (3,4,5), (6,7,8)} correspond to a horizontal win.
{(0,3,6), (1,4,7), (2,5,8)} correspond to a vertical win.
{(0,4,8), (2,4,6)} correspond to a diagonal win.

The board array likely stores an 'X' or an 'O'. The if structure checks if each one of the sets of positions above has all three values the same.

The odd thing about the code is that, if it finds a winning set, then it returns the position in the board corresponding to the first value in the WINNING_ROWS set. I would think you would want all three positions that create the win, or at least which row in the WINNING_ROWS array is a win.

• 40
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631371
• Total Posts
2999605
×