Piezas.cpp
/**
 * Returns which Piece has won, if there is a winner, Invalid if the game
 * is not over, or Blank if the board is filled and no one has won ("tie").
 * For a game to be over, all locations on the board must be filled with X's
 * and O's (i.e. no remaining Blank spaces). The winner is which player has
 * the most adjacent pieces in a single line. Lines can go either vertically
 * or horizontally. If both X's and O's have the same max number of pieces in a
 * line, it is a tie.
**/
Returns which Piece has won, if there is a winner, Invalid if the game
is not over, or Blank if the board is filled and no one has won ("tie").
For a game to be over, all locations on the board must be filled with X's
and O's . The winner is which player has
the most adjacent pieces in a single line. Lines can go either vertically
or horizontally. If both X's and O's have the same max number of pieces in a
line, it is a tie.
Returns which Piece has won if there is a winner Invalid if the game is not over or Blank if the board is filled and no one has won ( " tie " ) . For a game to be over all locations on the board must be filled with X ' s and O ' s . The winner is which player has the most adjacent pieces in a single line . Lines can go either vertically or horizontally . If both X ' s and O ' s have the same max number of pieces in a line it is a tie .
Piece Piezas::gameState()
{
    std::size_t O_counter_max = 0;
    std::size_t X_counter_max = 0;
    std::size_t O_counter = 0;
    std::size_t X_counter = 0;
    bool O_sequence = false;
    bool X_sequence = false;
    for (int row = 0; row < BOARD_ROWS; ++row) {
        for (int col = 0; col < BOARD_COLS; ++col) {
            if (board[row][col] == Blank)
                return Invalid;
            if (!O_sequence && board[row][col] == O) {
                O_sequence = true;
                O_counter = 1;
            } else if (O_sequence && board[row][col] != O) {
                O_sequence = false;
                if (O_counter > O_counter_max)
                    O_counter_max = O_counter;
                O_counter = 0;
            } else if (O_sequence && board[row][col] == O) {
                ++O_counter;
            }
            if (!X_sequence && board[row][col] == X) {
                X_sequence = true;
                X_counter = 1;
            } else if (X_sequence && board[row][col] != X) {
                X_sequence = false;
                if (X_counter > X_counter_max)
                    X_counter_max = X_counter;
                X_counter = 0;
            } else if (X_sequence && board[row][col] == X) {
                ++X_counter;
            }
        }
        O_sequence = false;
        if (O_counter > O_counter_max)
            O_counter_max = O_counter;
        O_counter = 0;
        X_sequence = false;
        if (X_counter > X_counter_max)
            X_counter_max = X_counter;
        X_counter = 0;
    }
    for (int col = 0; col < BOARD_COLS; ++col) {
        for (int row = 0; row < BOARD_ROWS; ++row) {
            if (!O_sequence && board[row][col] == O) {
                O_sequence = true;
                O_counter = 1;
            } else if (O_sequence && board[row][col] != O) {
                O_sequence = false;
                if (O_counter > O_counter_max)
                    O_counter_max = O_counter;
                O_counter = 0;
            } else if (O_sequence && board[row][col] == O) {
                ++O_counter;
            }
            if (!X_sequence && board[row][col] == X) {
                X_sequence = true;
                X_counter = 1;
            } else if (X_sequence && board[row][col] != X) {
                X_sequence = false;
                if (X_counter > X_counter_max)
                    X_counter_max = X_counter;
                X_counter = 0;
            } else if (X_sequence && board[row][col] == X) {
                ++X_counter;
            }
        }
        O_sequence = false;
        if (O_counter > O_counter_max)
            O_counter_max = O_counter;
        O_counter = 0;
        X_sequence = false;
        if (X_counter > X_counter_max)
            X_counter_max = X_counter;
        X_counter = 0;
    }
    if (O_counter_max == X_counter_max) {
        return Blank;
    } else if (O_counter_max > X_counter_max) {
        return O;
    } else if (O_counter_max < X_counter_max) {
        return X;
    }
}
Piece Piezas :: gameState ( ) { std :: size_t O_counter_max = 0 ; std :: size_t X_counter_max = 0 ; std :: size_t O_counter = 0 ; std :: size_t X_counter = 0 ; bool O_sequence = false ; bool X_sequence = false ; for ( int row = 0 ; row < BOARD_ROWS ; ++ row ) { for ( int col = 0 ; col < BOARD_COLS ; ++ col ) { if ( board [ row ] [ col ] == Blank ) return Invalid ; if ( ! O_sequence && board [ row ] [ col ] == O ) { O_sequence = true ; O_counter = 1 ; } else if ( O_sequence && board [ row ] [ col ] != O ) { O_sequence = false ; if ( O_counter > O_counter_max ) O_counter_max = O_counter ; O_counter = 0 ; } else if ( O_sequence && board [ row ] [ col ] == O ) { ++ O_counter ; } if ( ! X_sequence && board [ row ] [ col ] == X ) { X_sequence = true ; X_counter = 1 ; } else if ( X_sequence && board [ row ] [ col ] != X ) { X_sequence = false ; if ( X_counter > X_counter_max ) X_counter_max = X_counter ; X_counter = 0 ; } else if ( X_sequence && board [ row ] [ col ] == X ) { ++ X_counter ; } } O_sequence = false ; if ( O_counter > O_counter_max ) O_counter_max = O_counter ; O_counter = 0 ; X_sequence = false ; if ( X_counter > X_counter_max ) X_counter_max = X_counter ; X_counter = 0 ; } for ( int col = 0 ; col < BOARD_COLS ; ++ col ) { for ( int row = 0 ; row < BOARD_ROWS ; ++ row ) { if ( ! O_sequence && board [ row ] [ col ] == O ) { O_sequence = true ; O_counter = 1 ; } else if ( O_sequence && board [ row ] [ col ] != O ) { O_sequence = false ; if ( O_counter > O_counter_max ) O_counter_max = O_counter ; O_counter = 0 ; } else if ( O_sequence && board [ row ] [ col ] == O ) { ++ O_counter ; } if ( ! X_sequence && board [ row ] [ col ] == X ) { X_sequence = true ; X_counter = 1 ; } else if ( X_sequence && board [ row ] [ col ] != X ) { X_sequence = false ; if ( X_counter > X_counter_max ) X_counter_max = X_counter ; X_counter = 0 ; } else if ( X_sequence && board [ row ] [ col ] == X ) { ++ X_counter ; } } O_sequence = false ; if ( O_counter > O_counter_max ) O_counter_max = O_counter ; O_counter = 0 ; X_sequence = false ; if ( X_counter > X_counter_max ) X_counter_max = X_counter ; X_counter = 0 ; } if ( O_counter_max == X_counter_max ) { return Blank ; } else if ( O_counter_max > X_counter_max ) { return O ; } else if ( O_counter_max < X_counter_max ) { return X ; } }
Returns which Piece has won, if there is a winner, Invalid if the game
 is not over, or Blank if the board is filled and no one has won ("tie").
Returns which Piece has won if there is a winner Invalid if the game is not over or Blank if the board is filled and no one has won ( " tie " ) .
// These variables hold the maximum number of continuous squares for each player in the whole board. // These variables hold the current number of continuous squares for each player in the whole board. // These variables record if the current cell is within a continuous sequence of X's or O's. // First scan the rows. // Check for any Blank squares only in the first round, which touches all the squares. // The start of an O sequence. // The end of an O sequence. // In the middle of an O sequence. // The start of an X sequence. // The end of an X sequence. // In the middle of an X sequence. // The end of an O sequence. // The end of an X sequence. // Second scan the columns. // The start of an O sequence. // The end of an O sequence. // In the middle of an O sequence. // The start of an X sequence. // The end of an X sequence. // In the middle of an X sequence. // The end of an O sequence. // The end of an X sequence. // Third compare the maximum counters to see who won.