unexplainable error on tic tac toe

can someone please help i have been programming tic-tac toe for 2 hours and i can't find the error it unexpectedly terminates itself after the first two turns
(i have substituted x & o for 3 & 5)
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
#include <iostream>
using namespace std;
int main(){
cout<<"tic-tac toe";
int cor[3][3]={{1,1,1},{1,1,1},{1,1,1}},pl=1,win[8],w=0;
go:
int movv=0,movh=0;
if (pl==1){cout<<"\n\nplayer 1 turn";}
else{cout<<"\n\nplayer 2 turn";}

cout<<"\n\n   1 2 3";
cout<<"\n 1 "<<cor[0][0]<<' '<<cor[0][1]<<' '<<cor[0][2];
cout<<"\n 2 "<<cor[1][0]<<' '<<cor[1][1]<<' '<<cor[1][2];
cout<<"\n 3 "<<cor[2][0]<<' '<<cor[2][1]<<' '<<cor[2][2];

cout<<"\n\nplease enter move\nvertical move::";
cin>>movv;
cout<<"horaztal move::";
cin>>movh;
system ("cls");
if(pl==1)
cor[movv-1][movh-1]=3;
else
cor[movv-1][movh-1]=5;

win[0]=cor[0][0]+cor[0][1]+cor[0][2];
win[1]=cor[1][0]+cor[1][1]+cor[1][2];
win[2]=cor[2][0]+cor[2][1]+cor[2][2];
win[3]=cor[0][0]+cor[1][0]+cor[2][0];
win[4]=cor[0][1]+cor[1][1]+cor[2][1];
win[5]=cor[0][2]+cor[1][2]+cor[2][2];
win[6]=cor[0][0]+cor[1][1]+cor[2][2];
win[7]=cor[0][2]+cor[1][1]+cor[2][0];

if(win[0]==9)w++;
if(win[0]==15)w++;
if(win[1]==9)w++;
if(win[1]==15)w++;
if(win[2]==9)w++;
if(win[2]==15)w++;

if(win[3]==9)w++;
if(win[3]==15)w++;
if(win[4]==9)w++;
if(win[4]==15)w++;
if(win[5]==9)w++;
if(win[5]==15)w++;

if(win[6]==9)w++;
if(win[6]==15)w++;
if(win[7]==9)w++;
if(win[7]==15)w++;
if (w!=1) cout<<"no winner yet!!";

if(pl==1)
pl++;
else
pl--;

if(w!=1)goto go;
if(w==1){
system ("cls");

if(pl==1)
pl++;
else
pl--;

cout<<"player"<<pl<<"wins";}
}
thanx
you dont pause the game after displaying the winner. Run it in command promt and you will see it displays "player2wins" at the end.

I dont know how tictactoe works, so i dont know or player2 should win.
it will only use the winner code at the end if the interger win is at 1 instead of 0 and its the same as x's and o's
1+5+3=9 :)

Using 0 instead of 1 as start-value would solve the problem.

Besides that, you should consider using some for-loops. That would make the code a lot shorter. And i dont see the goto statement often. Using a while or a do loop would make the code clearer.

Its perfectly doable to use x and o instead of 3 and 5:
1
2
3
4
char cor[3][3]
cor[movv-1][movh-1]='o'
if (cor[0][0]==cor[0][1] && cor[0][1]==cor[0][2])
win=true;

i fixed and improved it i suggest playing it its my first game (x's and o's)
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
#include <iostream>
using namespace std;
int main(){
st:
system ("cls");
cout<<"tic-tac toe";
int cor[3][3]={{0,0,0},{0,0,0}},pl=1,win[8],w=0,re=0,time=0;
go:
int movv=0,movh=0;
time++;
cout<<"\n   1 2 3\n  1";
if(cor[0][0]==3)cout<<"x|";
if(cor[0][0]==5)cout<<"o|";
if(cor[0][0]==0)cout<<"_|";
 if(cor[0][1]==3)cout<<"x|";
 if(cor[0][1]==5)cout<<"o|";
 if(cor[0][1]==0)cout<<"_|";
  if(cor[0][2]==3)cout<<"x";
  if(cor[0][2]==5)cout<<"o";
  if(cor[0][2]==0)cout<<"_|";
cout<<"\n  2";
if(cor[1][0]==3)cout<<"x|";
if(cor[1][0]==5)cout<<"o|";
if(cor[1][0]==0)cout<<"_|";
 if(cor[1][1]==3)cout<<"x|";
 if(cor[1][1]==5)cout<<"o|";
 if(cor[1][1]==0)cout<<"_|";
  if(cor[1][2]==3)cout<<"x";
  if(cor[1][2]==5)cout<<"o";
  if(cor[1][2]==0)cout<<"_|";
cout<<"\n  3";
if(cor[2][0]==3)cout<<"x|";
if(cor[2][0]==5)cout<<"o|";
if(cor[2][0]==0)cout<<"_|";
 if(cor[2][1]==3)cout<<"x|";
 if(cor[2][1]==5)cout<<"o|";
 if(cor[2][1]==0)cout<<"_|";
  if(cor[2][2]==3)cout<<"x";
  if(cor[2][2]==5)cout<<"o";
  if(cor[2][2]==0)cout<<"_|";


cout<<"\n\nplayer "<<pl<<" turn "<<time<<"/9";

cout<<"\n\nplease enter move\nvertical move::";
cin>>movv;
cout<<"horaztal move::";
cin>>movh;
system ("cls");
if(pl==1){
if(cor[movv-1][movh-1]==5)cout<<"last move was inadequate";
else
cor[movv-1][movh-1]=3;
}
if(pl==2){
if(cor[movv-1][movh-1]==3)cout<<"last move was inadequate";
else
cor[movv-1][movh-1]=5;
}

win[0]=cor[0][0]+cor[0][1]+cor[0][2];
win[1]=cor[1][0]+cor[1][1]+cor[1][2];
win[2]=cor[2][0]+cor[2][1]+cor[2][2];
win[3]=cor[0][0]+cor[1][0]+cor[2][0];
win[4]=cor[0][1]+cor[1][1]+cor[2][1];
win[5]=cor[0][2]+cor[1][2]+cor[2][2];
win[6]=cor[0][0]+cor[1][1]+cor[2][2];
win[7]=cor[0][2]+cor[1][1]+cor[2][0];

if(win[0]==9)w++;
if(win[0]==15)w++;
if(win[1]==9)w++;
if(win[1]==15)w++;
if(win[2]==9)w++;
if(win[2]==15)w++;

if(win[3]==9)w++;
if(win[3]==15)w++;
if(win[4]==9)w++;
if(win[4]==15)w++;
if(win[5]==9)w++;
if(win[5]==15)w++;

if(win[6]==9)w++;
if(win[6]==15)w++;
if(win[7]==9)w++;
if(win[7]==15)w++;
if(pl==2){
if(cor[movv-1][movh-1]!=3){
if (w!=1) cout<<"no winner yet!";}}

if(pl==1){
if(cor[movv-1][movh-1]!=5){
if (w!=1) cout<<"no winner yet!";}}

if (pl==1)pl++;
else pl--;
if(w!=1)goto go;

system ("cls");
if (pl==1)pl++;
else pl--;
system ("cls");
cout<<"player-"<<pl<<" wins\n\n";

cout<<"\n  |1|2|3|\n  1";
if(cor[0][0]==3)cout<<"x|";
if(cor[0][0]==5)cout<<"o|";
if(cor[0][0]==0)cout<<"_|";
 if(cor[0][1]==3)cout<<"x|";
 if(cor[0][1]==5)cout<<"o|";
 if(cor[0][1]==0)cout<<"_|";
  if(cor[0][2]==3)cout<<"x";
  if(cor[0][2]==5)cout<<"o";
  if(cor[0][2]==0)cout<<"_|";
cout<<"\n  2";
if(cor[1][0]==3)cout<<"x|";
if(cor[1][0]==5)cout<<"o|";
if(cor[1][0]==0)cout<<"_|";
 if(cor[1][1]==3)cout<<"x|";
 if(cor[1][1]==5)cout<<"o|";
 if(cor[1][1]==0)cout<<"_|";
  if(cor[1][2]==3)cout<<"x";
  if(cor[1][2]==5)cout<<"o";
  if(cor[1][2]==0)cout<<"_|";
cout<<"\n  3";
if(cor[2][0]==3)cout<<"x|";
if(cor[2][0]==5)cout<<"o|";
if(cor[2][0]==0)cout<<"_|";
 if(cor[2][1]==3)cout<<"x|";
 if(cor[2][1]==5)cout<<"o|";
 if(cor[2][1]==0)cout<<"_|";
  if(cor[2][2]==3)cout<<"x";
  if(cor[2][2]==5)cout<<"o";
  if(cor[2][2]==0)cout<<"_|";

cout<<"\n\ntype 0 to play again::";
cin>>re;
if (re==0)goto st;
cout<<"\n\n";
system("pause");
}

Nice game :)

You learn a lot from seing how you could have done it even better, so a few suggestions:
1
2
3
4
5
if(win[0]==9)w++;
if(win[0]==15)w++;
....
if(win[7]==9)w++;
if(win[7]==15)w++;

Can be written shorter as
1
2
3
4
5
for (int i=0;i<=7;i++)
{
if(win[i]==9)w++;
if(win[i]==15)w++;
}

And this:
1
2
3
4
5
if(cor[0][0]==3)cout<<"x|";
if(cor[0][0]==5)cout<<"o|";
....
  if(cor[2][2]==5)cout<<"o";
  if(cor[2][2]==0)cout<<"_|";

can be written shorter as:
1
2
3
4
5
6
7
8
9
10
11
for (int i=0;i<=2;i++)
{
         for (int j=0;j<=2;j++)
         {
                if (cor[i][j]==3) cout<<"x";
                if (cor[i][j]==15) cout<<"o";
                if (cor[i][j]==0) cout<<"_";
                cout<<"|";
         }
         cout<<"\n"<<j+1;
}


And i would use a char or string type variable to store the input from the user in after the 'play again' question: if he now types a letter the program will chrash

(I havent tested those codes, there may be some mistakes in it)


what can i do
I played this game, and its looks good, this is in fact is my In Course Assessment for C++ at University of Teesside, but the only difference is they want 7 by 6 grid :(...

Although the program runs good, but i found something that could be improved on...

What if player 1 enters the co-ords of [ 1 1] and player 2 enters the same, i tried this and found that the player switched over, maybe there should be an error telling player 2 that he entered the wrong co-ords and should re enter it again.

Also when i enter the 1st co-ords such as [3 2] it doesn't place where i wanted it, instead it placed it in 2 along the rows and 3 down... :/

Tim
Topic archived. No new replies allowed.