Problem with Tik Tak Toe Game C++

I'm having a hard time completing my tik tak toe game. I'm stuck at the horizontal check as you can see below. I don't know why it won't declare the winner even though I have the command to declare it at the bottom.

I know this is just basic, but I really can't see my problem.

A quick help would be gladly appreciated.

PS.: Sorry for the messy code. I've done a lots of trial and error so that's what caused it.
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
#include <iostream>
using namespace std;

int main ()
{
    char position[3][3]={{'_','_','_'},{'_','_','_'},{'_','_','_'}};
    int row,column;
    int winx=0,wino=0;

for(int turn=1;;turn++)
{    
     
     system ("cls");
   for (int i=0;i<3;i++)
    {
    cout<<"   ";
    for (int j=0;j<3;j++)
    cout<<position[i][j]<<"   |   ";
    cout<<endl<<"_______|_______|_______|"<<endl;
}
cout<<endl;


if (turn%2==1)
{
cout<<"Player 1 turn.\n\nPlease enter the location (row(space)column): ";
cin>>row>>column;
if (((row>4)||(row<1))||((column>4)||(column<1)))
{
cout<<"Invalid row/column.";
system("pause>0");
continue;
}

if ((position[row-1][column-1]=='o')||(position[row-1][column-1]=='x'))
{
cout<<"This spot is already taken.";
system ("pause>0");
turn=turn-1;
continue;
}
position[row-1][column-1]='x';


}

else if (turn%2==0)
{
cout<<"Player 2 turn.\n\nPlease enter the location (row(space)column): ";
cin>>row>>column;
if (((row>4)||(row<1))||((column>4)||(column<1)))
{
cout<<"Invalid row/column.";
system("pause>0");
continue;
}

if ((position[row-1][column-1]=='o')||(position[row-1][column-1]=='x'))
{
cout<<"This spot is already taken.";
system ("pause>0");
turn=turn-1;
continue;
}
position[row-1][column-1]='o';

//Horizontal Check
for (int i=0;i<3;i++)
{
    int totalx=0,totalo=0;
 for (int j=0;j<3;j++)
 {
     if (position[i][j]=='x')
     totalx=totalx+1;
     else if (position[i][j]=='o')
     totalo=totalo+1;
     if (totalx==3)
                {
                   winx=1;
                       break;
                       }
                else if (totalo==3)
                       {
                       wino=1;
                       break;
                       }
 }
}

if (winx==1)
{
cout<<"Player 1 wins!";
break;
}
if (wino==1)
{
cout<<"Player 2 wins!";
break;
}


}



}
system ("pause>0");

return 0;
}
Last edited on
The problem in your program is hard to spot without identation. This is a good example of why you want to stay away from sloppy looking code. On line 102 you end the else if that starts on line 47. It might be a little hard to spot this because everything is not indented properly. In the future you might want to save yourself the trouble and just keep your code indented at all times. To fix the error just end the else if before you check for wins. I think that should do it.

I recommend Notepad++ if you are using windows. It has a menu command that can automatically redo indenting for c++. (That is how I spotted the error.)
Last edited on
Thanks for the solution. And also thanks for the advice, It will sure be useful for me now and in the future.

Cheers man. :)
Topic archived. No new replies allowed.