Game help 2!

Hi, Sorry Im posting too much. But Ive reworked the code abit. If you have a compiler put it in and see how the program runs because im not sure how to describe the error. But it was a pretty big error. Maybe I forgot bits I dont know, but im not sure if the logic is right. I really need help on this asap. Thanks, Maryann

//SDD 2012
//Tic tac toe
//l

#include <iostream>
#include <string>

using namespace std;

string playerone;
string playertwo;

//variables for spaces in the game board
char G;
char Grid[9] = {'_','_', '_', '_', '_', '_', '_', '_', '_'};

//tic tac toe board for game play
void Print_Grid (char[])
{
cout << "___________________________\n";
cout << " | | \n";
cout << Grid [0] && "|" && Grid [1] && "|" && Grid [2];
cout << " | | \n";
cout << "___________________________\n";
cout << " | | \n";
cout << Grid [3] && "|" && Grid [4]&& "|" && Grid [5];
cout << " | | \n";
cout << "___________________________\n";
cout << " | | \n";
cout << Grid [6] && "|" && Grid [7] && "|" && Grid [8];
cout << " | | \n";
cout << "___________________________\n";
}

//if array has been filled then the game has ended
int number_of_moves = 0;

//to determine whose turn it is
char current_turn = '0';

//if winner has been found
bool foundwinner = false;

//check if game is over
bool gameover = false;

void determine_turn()
{
playerone = 'X';
playertwo = 'O';


if (current_turn == 'X') //if X just had a turn change to O
{
current_turn = 'O';
}
else
{
current_turn = 'X';
}


}


void get_move ()
{

int move;
if (current_turn == 'X')
{
cout << playerone << "select a location for 'X'" << endl;
cin >> move;

if (move == 1)
{
Grid[0] = 'X';
}
if (move == 2)
{
Grid[1] = 'X';
}
if (move == 3)
{
Grid[2] = 'X';
}
if (move == 4)
{
Grid[3] = 'X';
}
if (move == 5)
{
Grid[4] = 'X';
}
if (move == 6)
{
Grid[5] = 'X';
}
if (move == 7)
{
Grid[6] = 'X';
}
if (move == 8)
{
Grid[7] = 'X';
}
if (move == 9)
{
Grid[8] = 'X';
}
}
else
{
cout << playertwo << "select location for '0'" << endl;
cin >> move;

if (move == 1)
{
Grid[0] = 'O';
}
if (move == 2)
{
Grid[1] = 'O';
}
if (move == 3)
{
Grid[2] = 'O';
}
if (move == 4)
{
Grid[3] = 'O';
}
if (move == 5)
{
Grid[4] = 'O';
}
if (move == 6)
{
Grid[5] = 'O';
}
if (move == 7)
{
Grid[6] = 'O';
}
if (move == 8)
{
Grid[7] = 'O';
}
if (move == 9)
{
Grid[8] = 'O';
}
}
}


void check_win()
{
if
(
((Grid[0] == Grid[3]) && (Grid[0] == Grid[6])) ||
((Grid[1] == Grid[4]) && (Grid[1] == Grid[7])) ||
((Grid[2] == Grid[3]) && (Grid[2] == Grid[8])) ||
((Grid[0] == Grid[3]) && (Grid[0] == Grid[2])) ||
((Grid[3] == Grid[3]) && (Grid[3] == Grid[5])) ||
((Grid[6] == Grid[3]) && (Grid[6] == Grid[8])) ||
((Grid[0] == Grid[3]) && (Grid[0] == Grid[8])) ||
((Grid[2] == Grid[3]) && (Grid[2] == Grid[6]))
)
{
foundwinner = true;
if (current_turn == '0')
{
cout << "the winner is" << playerone << endl;
}
else
{
cout << "the winner is" << playertwo << endl;
}
}
else
{
foundwinner = false;

if (foundwinner = true && number_of_moves == 9)
{
gameover = true;
}
}
}


//main function
int main ()
{
cout << "Tic Tac Toe";
cout << " begin ";

//allowing user to enter name and be identified by own name
cout << "player one enter name\n";
cin >> playerone;
cout << "player two enter name\n";
cin >> playertwo;

//so player names can be shown on the screen.
cout << playerone << " " << playertwo << endl;


char Print_Grid(G);

//while loop
while (gameover == false)
{
while (gameover == false)
{
determine_turn();
get_move();
check_win();
}




}


return 0;

}
Last edited on
closed account (o3hC5Di1)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//SDD 2012
//Tic tac toe
//l

#include <iostream>
#include <string>

using namespace std;

string playerone;
string playertwo;

//variables for spaces in the game board
char G;
char Grid[9] = {'_','_', '_', '_', '_', '_', '_', '_', '_'};

//tic tac toe board for game play
void Print_Grid (char[])
{
cout << "___________________________\n";
cout << " | | \n";
cout << Grid [0] && "|" && Grid [1] && "|" && Grid [2];
cout << " | | \n";
cout << "___________________________\n";
cout << " | | \n";
cout << Grid [3] && "|" && Grid [4]&& "|" && Grid [5];
cout << " | | \n";
cout << "___________________________\n";
cout << " | | \n";
cout << Grid [6] && "|" && Grid [7] && "|" && Grid [8];
cout << " | | \n";
cout << "___________________________\n";
}

//if array has been filled then the game has ended
int number_of_moves = 0;

//to determine whose turn it is
char current_turn = '0';

//if winner has been found
bool foundwinner = false;

//check if game is over
bool gameover = false;

void determine_turn()
{
playerone = 'X';
playertwo = 'O';


if (current_turn == 'X') //if X just had a turn change to O
{
current_turn = 'O';
}
else
{
current_turn = 'X';
}


}


void get_move ()
{

int move;
if (current_turn == 'X')
{
cout << playerone << "select a location for 'X'" << endl;
cin >> move;

if (move == 1)
{
Grid[0] = 'X';
}
if (move == 2)
{
Grid[1] = 'X';
}
if (move == 3)
{
Grid[2] = 'X';
}
if (move == 4)
{
Grid[3] = 'X';
}
if (move == 5)
{
Grid[4] = 'X';
}
if (move == 6)
{
Grid[5] = 'X';
}
if (move == 7)
{
Grid[6] = 'X';
}
if (move == 8)
{
Grid[7] = 'X';
}
if (move == 9)
{
Grid[8] = 'X';
}
}
else
{
cout << playertwo << "select location for '0'" << endl;
cin >> move;

if (move == 1)
{
Grid[0] = 'O';
}
if (move == 2)
{
Grid[1] = 'O';
}
if (move == 3)
{
Grid[2] = 'O';
}
if (move == 4)
{
Grid[3] = 'O';
}
if (move == 5)
{
Grid[4] = 'O';
}
if (move == 6)
{
Grid[5] = 'O';
}
if (move == 7)
{
Grid[6] = 'O';
}
if (move == 8)
{
Grid[7] = 'O';
}
if (move == 9)
{
Grid[8] = 'O';
}
}
}


void check_win()
{
if
(
((Grid[0] == Grid[3]) && (Grid[0] == Grid[6])) ||
((Grid[1] == Grid[4]) && (Grid[1] == Grid[7])) ||
((Grid[2] == Grid[3]) && (Grid[2] == Grid[8])) ||
((Grid[0] == Grid[3]) && (Grid[0] == Grid[2])) ||
((Grid[3] == Grid[3]) && (Grid[3] == Grid[5])) ||
((Grid[6] == Grid[3]) && (Grid[6] == Grid[8])) ||
((Grid[0] == Grid[3]) && (Grid[0] == Grid[8])) ||
((Grid[2] == Grid[3]) && (Grid[2] == Grid[6]))
)
{
foundwinner = true;
if (current_turn == '0')
{
cout << "the winner is" << playerone << endl;
}
else
{
cout << "the winner is" << playertwo << endl;
}
}
else
{
foundwinner = false;

if (foundwinner = true && number_of_moves == 9)
{
gameover = true;
}
}
}


//main function
int main ()
{
cout << "Tic Tac Toe";
cout << " begin ";

//allowing user to enter name and be identified by own name
cout << "player one enter name\n";
cin >> playerone;
cout << "player two enter name\n";
cin >> playertwo;

//so player names can be shown on the screen.
cout << playerone << " " << playertwo << endl;


//while loop
while (gameover == false)
{
while (gameover == false)
{
determine_turn();
get_move();
check_win();
}
}
return 0;

}


Hi there,

You need to call the Print_Grid() function in main(), or it will never display the board to the user:

1
2
3
4
5
6
7
while (gameover == false) //one while-loop is enough
{
Print_Grid(Grid);
determine_turn();
get_move();
check_win();
}


Change your print_grid function as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Print_Grid (char[] Grid) //must add Grid name, or the function still won't know it.
{
cout << "___________________________\n";
cout << " | | \n";
cout << Grid [0] && "|" && Grid [1] && "|" && Grid [2];
cout << " | | \n";
cout << "___________________________\n";
cout << " | | \n";
cout << Grid [3] && "|" && Grid [4]&& "|" && Grid [5];
cout << " | | \n";
cout << "___________________________\n";
cout << " | | \n";
cout << Grid [6] && "|" && Grid [7] && "|" && Grid [8];
cout << " | | \n";
cout << "___________________________\n";
}


And in order for your check_win function to work properly, change:

1
2
3
char Grid[9] = {'_','_', '_', '_', '_', '_', '_', '_', '_'};
char Grid[9] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
//if we make them all underscores they are all equal, so check win will think someone won 


In order to end the game you will have to do something more, to break out of the while() loop in main().
You will have to set the gameover variable to true when someone has won - but I'll leave that up to you.

All the best,
NwN
Last edited on
okay this is the code now. But im still stuck on it. :(
The game board is reprinted after everymove underneath the last one, but it is not filled with 'X' or 'O' it just keeps repeating it self. If you compile it you'll see what I mean. Is there anyway i can work the code to not do that?
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//SDD 2012
//Tic tac toe
//l

#include <iostream>
#include <string>

using namespace std;

string playerone;
string playertwo;

//variables for spaces in the game board
char G;
char Grid[9] = {'1','2', '3', '4', '5', '6', '7', '8','9'};

//tic tac toe board for game play

void Print_Grid ()
{
    cout << "___________________________\n";
    cout << "         |        |        \n";
    cout <<  Grid [0] && "|" && Grid [1] && "|" && Grid [2];
    cout << "         |        |        \n";
    cout << "___________________________\n";
    cout << "         |        |        \n";
    cout <<  Grid [3] && "|" && Grid [4]&& "|" && Grid [5];
    cout << "         |        |        \n";
    cout << "___________________________\n";
    cout << "         |        |        \n";
    cout <<  Grid [6] && "|" && Grid [7] && "|" && Grid [8];
    cout << "         |        |        \n";
    cout << "___________________________\n";
}

//if array has been filled then the game has ended
int number_of_moves = 0;

//to determine whose turn it is
char current_turn = '0';

//if winner has been found
bool foundwinner = false;

//check if game is over
bool gameover = false;

void determine_turn()
{
    playerone = 'X';
    playertwo = 'O';


    if (current_turn == 'X') //if X just had a turn change to O
    {
        current_turn = 'O';
    }
    else
    {
        current_turn = 'X';
    }


}


void get_move ()
{

    int move;
    if (current_turn == 'X')
    {
        cout << playerone << "select a location for 'X'" << endl;
        cin >> move;

        if (move == 1)
        {
            Grid[0] = 'X';
        }
        if (move == 2)
        {
            Grid[1] = 'X';
        }
        if (move == 3)
        {
            Grid[2] = 'X';
        }
        if (move == 4)
        {
            Grid[3] = 'X';
        }
        if (move == 5)
        {
            Grid[4] = 'X';
        }
        if (move == 6)
        {
            Grid[5] = 'X';
        }
        if (move == 7)
        {
            Grid[6] = 'X';
        }
        if (move == 8)
        {
            Grid[7] = 'X';
        }
        if (move == 9)
        {
            Grid[8] = 'X';
        }
    }
    else
    {
        cout << playertwo << "select location for '0'" << endl;
        cin >> move;

        if (move == 1)
        {
            Grid[0] = 'O';
        }
        if (move == 2)
        {
            Grid[1] = 'O';
        }
        if (move == 3)
        {
            Grid[2] = 'O';
        }
        if (move == 4)
        {
            Grid[3] = 'O';
        }
        if (move == 5)
        {
            Grid[4] = 'O';
        }
        if (move == 6)
        {
            Grid[5] = 'O';
        }
        if (move == 7)
        {
            Grid[6] = 'O';
        }
        if (move == 8)
        {
            Grid[7] = 'O';
        }
        if (move == 9)
        {
            Grid[8] = 'O';
        }
    }
}


void check_win()
{
    if
    (
        ((Grid[0] == Grid[3]) && (Grid[0] == Grid[6])) ||
        ((Grid[1] == Grid[4]) && (Grid[1] == Grid[7])) ||
        ((Grid[2] == Grid[3]) && (Grid[2] == Grid[8])) ||
        ((Grid[0] == Grid[3]) && (Grid[0] == Grid[2])) ||
        ((Grid[3] == Grid[3]) && (Grid[3] == Grid[5])) ||
        ((Grid[6] == Grid[3]) && (Grid[6] == Grid[8])) ||
        ((Grid[0] == Grid[3]) && (Grid[0] == Grid[8])) ||
        ((Grid[2] == Grid[3]) && (Grid[2] == Grid[6]))
    )
    {
        foundwinner = true;
        if (current_turn == '0')
        {
            cout << "the winner is" << playerone << endl;
        }
        else
        {
            cout << "the winner is" << playertwo << endl;
        }
    }
    else
    {
        foundwinner = false;

        if (foundwinner = true && number_of_moves == 9)
        {
            gameover = true;
        }
    }
}


//main function
int main ()
{
    cout << "Tic Tac Toe";
    cout << "   begin   ";

    //allowing user to enter name and be identified by own name
    cout << "player one enter name\n";
    cin >> playerone;
    cout << "player two enter name\n";
    cin >> playertwo;

    //so player names can be shown on the screen.
    cout << playerone << "          " << playertwo << endl;




    //while loop
    while (gameover == false)
    {
        Print_Grid();
        determine_turn();
        get_move();
        check_win();
    }


    return 0;
}
closed account (o3hC5Di1)
Hi,

You're using ampersands in your cout statements:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Print_Grid ()
{
    cout << "___________________________\n";
    cout << "         |        |        \n";
    cout <<  Grid [0] && "|" && Grid [1] && "|" && Grid [2];  // see the && here
    cout << "         |        |        \n";
    cout << "___________________________\n";
    cout << "         |        |        \n";
    cout <<  Grid [3] && "|" && Grid [4]&& "|" && Grid [5]; // and here
    cout << "         |        |        \n";
    cout << "___________________________\n";
    cout << "         |        |        \n";
    cout <<  Grid [6] && "|" && Grid [7] && "|" && Grid [8];  //and here
    cout << "         |        |        \n";
    cout << "___________________________\n";
}


Replace those by << .
On a sidenote, you can greatly reduce your amount of code by doing the following:

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
void get_move ()
{

    int move;
    if (current_turn == 'X')
    {
        cout << playerone << " select a location for 'X'" << endl;
        
        if((cin >> move) && move < 9 && move >0)
        {
             Grid[move-1] = 'X'
        }
        else
        {
            std::cout << "Invalid position.";
        }
    }
    else
    {
        cout << playertwo << " select location for '0'" << endl;

        if((cin >> move) && move < 9 && move >0)
        {
             Grid[move-1] = 'O'
        }
        else
        {
            std::cout << "Invalid position.";
        }
    }
}


Even this could be reduced further - but i'll leave that up to you.

All the best,
NwN
line 23, 27, 31: Replace && with << and add spaces

line 186: foundwinner = true is an assignment. use ==

I doubt that the if on line 160 is correct. I mean you need 3 in row. So why do you compare only 2?

I'd suggest that you use a 2-dimensional array as the grid. That'd make it more obvious.

Last edited on
I think your table output looks a lot nicer (both in console and in code) if you use a for loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Print_Grid ()
{
  cout << "\n\n";
  for (int i=0; i < 9; i+=3)
  {
    cout << "   |   | \n";
    cout << " " << Grid[i] << " | " << Grid[i+1] << " | " << Grid[i+2] << "\n";
    if(i != 6)
      cout << "___|___|___\n";
    else
      cout << "   |   | \n";
  }
  cout << "\n\n";
}


The if/else is purely to make the formatting of the table a little nice. You can omit it if you're not too pedantic.
Last edited on
I tried ^ that format
I got a compiling error in the line 'for (int i=0; i < 9; i+=3)'
it says: "error: 1value required as left operand of assignment"
No idea why that is. Works fine for me, VS2010.
Do you think I need to declare anything or add anything earlier in the code or something?
closed account (o3hC5Di1)
Repost your code if you will - maybe you made an error pasting the function?

All the best,
NwN
here it is

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//SDD 2012
//Tic tac toe
//l

#include <iostream>
#include <string>

using namespace std;

string playerone;
string playertwo;

//variables for spaces in the game board
char G;
char Grid[9] = {'1','2', '3', '4', '5', '6', '7', '8','9'};

//tic tac toe board for game play

void Print_Grid ()
{
    cout << "\n\n";
    for (int i= 0; 1 < 9; 1+=3)
    {
        cout << "   |   |  \n";
        cout << " " << Grid [i] << " | " << Grid [i+1] << " | " << Grid [i+2] << "\n";
        if (i != 6)
        cout << "___|___|___\n";
        else
        cout << "   |   |   \n";
    }
    cout << "\n\n";

}

//if array has been filled then the game has ended
int number_of_moves = 0;

//to determine whose turn it is
char current_turn = '0';

//if winner has been found
bool foundwinner = false;

//check if game is over
bool gameover = false;

void determine_turn()
{
    playerone = 'X';
    playertwo = 'O';


    if (current_turn == 'X') //if X just had a turn change to O
    {
        current_turn = 'O';
    }
    else
    {
        current_turn = 'X';
    }


}


void get_move ()
{

    int move;
    if (current_turn == 'X')
    {
        cout << playerone << "select a location for 'X'" << endl;
        cin >> move;
        if ((cin >> move) && move < 9 && move >0)
        {
            Grid[move-1] = 'X';
        }
        else
        {
            std::cout << "Invalid position.";
        }
    }
    else
    {
        cout << playerone << " select location for '0'" << endl;

        if ((cin >> move) && move < 9 && move >0)
        {
            Grid[move-1] = 'O';
        }
        else
        {
            std::cout << "Invalid position.";
        }
    }
}


void check_win()
{
    if
    (
        ((Grid[0] == Grid[3] && Grid[6])) ||
        ((Grid[1] == Grid[4] && Grid[7])) ||
        ((Grid[2] == Grid[3] && Grid[8])) ||
        ((Grid[0] == Grid[3] && Grid[2])) ||
        ((Grid[3] == Grid[3] && Grid[5])) ||
        ((Grid[6] == Grid[3] && Grid[8])) ||
        ((Grid[0] == Grid[3] && Grid[8])) ||
        ((Grid[2] == Grid[3] && Grid[6]))
    )
    {
        foundwinner = true;
        if (current_turn == '0')
        {
            cout << "the winner is" << playerone << endl;
        }
        else
        {
            cout << "the winner is" << playertwo << endl;
        }
    }
    else
    {
        foundwinner = false;

        if (foundwinner == true && number_of_moves == 9)
        {
            gameover = true;
        }
    }
}


//main function
int main ()
{
    cout << "Tic Tac Toe";
    cout << "   begin   ";

    //allowing user to enter name and be identified by own name
    cout << "player one enter name\n";
    cin >> playerone;
    cout << "player two enter name\n";
    cin >> playertwo;

    //so player names can be shown on the screen.
    cout << playerone << "          " << playertwo << endl;




    //while loop
    while (gameover == false)
    {
        Print_Grid();
        determine_turn();
        get_move();
        check_win();
    }


    return 0;
}
You've put 1+=3 in your code, not i+=3.
it didnt work anyways. I ran the program and it just kept extending an array with random symbols. Ill have to revert back to the old array. Even theni ts not going to work properly. Cause it keeps going and wont find a winner. Also it writes 'The winner is 0' at the end of every turn.
I played it making sure X got 3 in a row and it kept going.
Yeah dude, I gave you an function to output your board, not your entire game logic. That's down to you. -_-

The reason it's declaring that someone has won every time is because your if statement in check_win() will currently always evaluate to true.
Last edited on
closed account (o3hC5Di1)
iHutch's code works perfectly well - you copied it erroneously:

for (int i= 0; i < 9; i+=3)

1 will ALWAYS be smaller than 9, so you have an infinite loop printing your grid.

Try this:

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//SDD 2012
//Tic tac toe
//l

#include <iostream>
#include <string>

using namespace std;

string playerone;
string playertwo;

//variables for spaces in the game board
char G;
char Grid[9] = {'1','2', '3', '4', '5', '6', '7', '8','9'};

//tic tac toe board for game play

void Print_Grid ()
{
    cout << "\n\n";
    for (int i= 0; i < 9; i+=3)  //correct version
    {
        cout << "   |   |  \n";
        cout << " " << Grid [i] << " | " << Grid [i+1] << " | " << Grid [i+2] << "\n";
        if (i != 6)
        cout << "___|___|___\n";
        else
        cout << "   |   |   \n";
    }
    cout << "\n\n";

}

int number_of_moves = 0;

//to determine whose turn it is
char current_turn = '0';

//if winner has been found
bool foundwinner = false;

//check if game is over
bool gameover = false;

void determine_turn()
{
    playerone = 'X';
    playertwo = 'O';


    if (current_turn == 'X') //if X just had a turn change to O
    {
        current_turn = 'O';
    }
    else
    {
        current_turn = 'X';
    }


}


void get_move ()
{

    int move;
    if (current_turn == 'X')
    {
        cout << playerone << "select a location for 'X'" << endl;
        if ((cin >> move) && move <= 9 && move >0)
        {
            Grid[move-1] = 'X';
        }
        else
        {
            std::cout << "Invalid position.";
        }
    }
    else
    {
        cout << playerone << " select location for '0'" << endl;

        if ((cin >> move) && move < 9 && move >0)
        {
            Grid[move-1] = 'O';
        }
        else
        {
            std::cout << "Invalid position.";
        }
    }
}


void check_win()
{
    //you had bad logic in this if statement
    //&& is not used to say "if a is equal to 1 AND 3"
    //it is used to say "if a is equal to 1 AND b is equal to 3"
    if
    (
        (Grid[0] == Grid[3] && Grid[6]== Grid[3]) ||
        (Grid[1] == Grid[4] && Grid[7]== Grid[4]) ||
        (Grid[2] == Grid[3] && Grid[8]== Grid[3]) ||
        (Grid[0] == Grid[3] && Grid[2]== Grid[3]) ||
        (Grid[3] == Grid[3] && Grid[5]== Grid[3]) ||
        (Grid[6] == Grid[3] && Grid[8]== Grid[3]) ||
        (Grid[0] == Grid[3] && Grid[8]== Grid[3]) ||
        (Grid[2] == Grid[3] && Grid[6]== Grid[3])
    )
    {
        foundwinner = true;
        if (current_turn == '0')
        {
            cout << "the winner is" << playerone << endl;
        }
        else
        {
            cout << "the winner is" << playertwo << endl;
        }

	gameover = true;
    }
    else
    {
        foundwinner = false;

        if (number_of_moves == 9)
        {
            gameover = true;
        }
    }
}


//main function
int main ()
{
    cout << "Tic Tac Toe";
    cout << "   begin   ";

    //allowing user to enter name and be identified by own name
    cout << "player one enter name\n";
    cin >> playerone;
    cout << "player two enter name\n";
    cin >> playertwo;

    //so player names can be shown on the screen.
    cout << playerone << "          " << playertwo << endl;




    //while loop
    while (gameover == false)
    {
        Print_Grid();
        determine_turn();
        get_move();
        check_win();
    }


    return 0;
}


That works for me.

All the best,
NwN
Last edited on
in line 86 replace move < 9 with move < 10 (or move <= 9)
closed account (o3hC5Di1)
Ah yes, my bad - updated the code, thanks for pointing that out samrux :)

All the best,
NwN
Wow Thanks Guys its actually working for me now!!!!
You guys have helped alot. Thankyou!!!. :D
Topic archived. No new replies allowed.