Need help determining the winner of a tic tac toe program

i figured i would use a bunch of if statements but it hasn't worked
i'm lost!

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <cstdlib>
using namespace std;

void intro();
void input();

void show_board(char board[]);
//function displays the board on the screen

int main()
{
    char answer;

    intro();

    do
    {
        input();

        cout << "Game Over!" << endl;
        cout << "Do you wish to play another game? (y/n) \n";
        cin >> answer;

    } while(answer == 'Y' || answer == 'y');

    return 0;
}

void intro()
{
    cout << "This is a game of tic tac toe \n"
         << "Enter the numbers corresponding to the desired space \n";
}

void input()
{
    char board[9], turn = 'X'; //board[] holds the digits X or O, tutrn is the player's move
    int i, move, number_move = 0;

    for(i = 0; i < 9; i++) //this gets a move until 9 moves are reached
    {
        board[i] = '1' + i;
    }

    while(number_move < 9)
    {
        show_board(board);

        cout << "Enter move " << endl;
        cin >> move;

        if(move < 1 || move > 9)
        {
            cout << "Invalid move, try again \n";
        }
        else
        {
            move--;

            if(board[move] == 'X' || board[move] == 'O')
            {
                cout << "The inputted space is already taken \n";
            }
            else
            {
                board[move] = turn; //this switches turns

                if(turn == 'X')
                    turn = 'O';

                else
                    turn = 'X';

                number_move++;
            }
        }

        system("cls");
    }

    show_board(board);
}

void show_board(char board[])
{
    cout << endl;

    for (int i = 0; i < 9; i++)
    {
        cout << board[i] << " ";

        if(((i + 1) % 3) == 0)
            cout << endl;
    }
    cout << endl;
}
Hi,
Use a 2D array of size 3x3 of type int to solve your problem, you may insert in corresponding cell for first playar 1 for second player -1. Each time a player make a choice you should calculate the total of (sum of value) for each row, column and diagonal seperately. if it is 3 the first user wins, if it is -3 the second user wins, otherwise no one is the winner.

hope it helps
Last edited on
A bunch of if statements should work.

How about a function that runs after each move the check for a winning pattern?

So there are 8 winning lines in tic-tac-toe (three horizontals, three verticals and two diagonals). Presuming your array represents the board like this:

0 1 2
3 4 5
6 7 8

Then you can see which array combinations are the winning ones. (0,1,2 or 1,4,7 are two examples).

So you could use a function to check those spaces and return true if a winner is found. It may be a bit crude and, with a bit of thought, there's probably a better way of doing it (for example, you could use different values for the user's pieces and tally up the scores to see if there's a winner), but here's the groundwork for one way:

1
2
3
4
5
6
7
bool HasWon(char board[])
{
   if (board[0] == board[1] == board[2])
   {
       // There's a potential win here.  All three points in a winning row are the same.
   }
}


You might need an extra bit of validation on your check. I'm not quiet sure what's happening when you initialise your board in the for loop on line 41 (be careful when adding integers to chars). I'm presuming you're wanting it to display the numbers 1 - 9 unless there's an X or O in there. If a board is initialised with the same value in each square, that needs to be accounted for in a win check (yes, the three points may match, but if they're not populated with an X or O, it's not a win).

So you might want to expand on that above and add something that checks the positions are populated with an X or O:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool HasWon(char board[])
{
   if (board[0] == board[1] == board[2])
   {
       if (board[0] == 'X')
       {
          // If board[0] is X, we can assume that [1] and [2] are X.  X has won.
          return true;
       } 
       else if (board[0] == 'O')
       {
           // Same as above but for O.
           return true;
       }
       else
       {
           // This depends on how the board is initialised, but if all three spaces are the same
           // and they're not X or O, then they're probably unpopulated.  Not a win here. 
           return false;
       }
   }
}


You'd need a check like that for each winning combination. Here's a thought, though. Instead of a bool function, why not try an integer returning function and make a couple of defines for return types?

1
2
3
#define NO_WIN 0
#define WIN_X  1
#define WIN_O  2 


Then, you could change the returns in the function:

1
2
3
4
5
6
7
. . .
       if (board[0] == 'X')
       {
          // If board[0] is X, we can assume that [1] and [2] are X.  X has won.
          return WIN_X;
       } 
. . .


That way, when you call the function, it's easy to tell from the returned value who the winner is.

Hope this is of some help.
Last edited on
Topic archived. No new replies allowed.