Check for winner

closed account (3vX4LyTq)
I am working on a tic tac toe program. How do I check for a winner?

Here's what I have so far

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
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

using namespace std;

void board(char b[]);
void play(char b[], char turn);

int main(){
char b[]={'1','2','3','4','5','6','7','8','9'}, turn='x';
int x = 0;
int o = 0;


board(b);
while(o<10) {
  play(b, turn);
  board(b);

  if(turn=='x'){
    turn='o';
  }
  else{
    turn='x';
  }

  o++;
}



}
//FUNCTIONS
void board(char b[]){
  
  ///prints and updates the board
  cout << b[0] << " " << b[1] << " " << b[2] << endl;
  cout << b[3] << " " << b[4] << " " << b[5] << endl;
  cout << b[6] << " " << b[7] << " " << b[8] << endl;
}

void play(char b[], char turn){
  //decides whos turn it
  int pos;
  cin >> pos;
  b[pos-1]=turn;
}
Last edited on
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
35
36
37
38
39
40
41
using board_type = char[9] ;

// return character for winner if any, otherwise return 0
char check_rows( const board_type& board ) {

    for( int row = 0 ; row < 3 ; ++row )
    {
        const int i = row * 3 ;
        if( board[i] == board[i+1] && board[i] == board[i+2] ) return board[i] ;
    }

    return 0 ; // no winner
}

// return character for winner if any, otherwise return 0
char check_cols( const board_type& board ) {

    for( int col = 0 ; col < 3 ; ++col )
        if( board[col] == board[col+3] && board[col] == board[col+6] ) return board[col] ;

    return 0 ; // no winner
}

// return character for winner if any, otherwise return 0
char check_diags( const board_type& board ) {

    if( board[0] == board[4] && board[0] == board[8] ) return board[0] ;
    if( board[2] == board[4] && board[2] == board[6] ) return board[2] ;

    return 0 ;
}

// return character for winner if any, otherwise return 0
char check_winner( const board_type& board ) {

    char winner = check_rows(board) ;
    if( !winner ) winner = check_cols(board) ;
    if( !winner ) winner = check_diags(board) ;

    return winner ;
}

http://coliru.stacked-crooked.com/a/46056bc2f6205dae
closed account (3vX4LyTq)
JLBorges thanks for the help but I'm really new at c++ and I wouldn't know how this works or how to implement it. Do you think you can explain a bit?
It may be easier to understand if a two-dimensional 3x3 array is used for the board.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// type 'board_type' is an alias (another name) for the type '2d array of 3x3 char'
// http://en.cppreference.com/w/cpp/language/type_alias
using board_type = char[3][3] ;

// return character for winner if any, otherwise return 0
// we pass the board (the array) by reference (to const)
// see 'Arguments passed by value and by reference' in http://www.cplusplus.com/doc/tutorial/functions/
char check_rows( const board_type& board ) {

    // http://www.stroustrup.com/C++11FAQ.html#auto
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( const auto& row : board ) { // for each row in the board

        // if the characters in each cell of the row are the same
        // we have a winner: return the character (of the winner)
        if( row[0] == row[1] && row[1] == row[2] ) return row[0] ;
    }

    return 0 ; // no winner in any of the rows
}

// return character for winner if any, otherwise return 0
char check_cols( const board_type& board ) {

    for( int col = 0 ; col < 3 ; ++col ) { // for each column 1, 2, 3

        // if the three characters in each row of that column are the same
        // we have a winner: return the character (of the winner)
        if( board[0][col] == board[1][col+3] && board[0][col] == board[2][col] ) return board[0][col] ;
    }

    return 0 ; // no winner in any of the columns
}

// return character for winner if any, otherwise return 0
char check_diags( const board_type& board ) {

    // check diagonal top left to bottom right
    if( board[0][0] == board[1][1] && board[0][0] == board[2][2] ) return board[0][0] ;

    // check diagonal top right to bottom left
    if( board[0][2] == board[1][1] && board[0][2] == board[2][0] ) return board[0][2] ;

    return 0 ; // no winner in either of the columns
}

// return character for winner if any, otherwise return 0
char check_winner( const board_type& board ) {

    char winner = check_rows(board) ; // check the rows for a winner

    // if there is no winner in any of the rows (check_rows returned 0)
    // check the columns for a winner
    if( !winner ) winner = check_cols(board) ;

    // if there is no winner in any of the columns (check_cols returned 0)
    // check the diagonals for a winner
    if( !winner ) winner = check_diags(board) ;

    return winner ;
}

Click on this link for a compilable program (C++11) using the above:
http://coliru.stacked-crooked.com/a/c941d7863ef275d9
Topic archived. No new replies allowed.