Checking for a winner

Alright, so I am incredibly close to finishing this program. I understand why my program wasn't making a move and I was able to fix it, but now I am trying to check for a winner. I realize that my winGame(); function should be in some sort of while or do while loop to end the game. But, when I was trying to do a little debugging to sort some things out, I realize something unsettling. It always says that it's a draw, even when it's not supposed to be. These are such minor things that I am ashamed to not understand and I would really like some assistance on how I can fix it. Also, I know there's supposed to be a while or do while loop to end the game if there's a win. I'm just unsure where to put it, so if you have any suggestions, please let me know.

*Note that in my valid move function there's a little array, I plan on making it a static const array. My get functions return the value in the name (e.g. getIval() returns the initial value of the cell object), while my set functions just assign the values appropriately.

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
  bool TicTacToe::validMove(char move){
        char options[9] = { '1','2', '3', '4','5','6','7', '8','9' };
        bool validate = false;
                for ( int i = 0; i < 9; i++ ){
                        if ( move == options[i]){
                                validate = true;
                        }
                }

        return ( validate );
}
void TicTacToe::setMove( char move ){
        for ( int i = 0; i < ROW; i++ ){
                for ( int j = 0; j < COL; j++ ){
                        if ( board[i][j].getiVal() == move ){
                                board[i][j].setiVal( players[currentPlayer].getMarker() );
                                switchPlayer();
                                break;
                        }
                }
        }
}
void TicTacToe::makeAMove(){
        char move;
        int turns = 1;
        bool validate = true;


        do{
                cout << "Player " << (getCurrentPlayer() + 1) << " make a move." << endl;
                cin >> move;

                if ( validMove( move ) ){
                        if ( turns > 4 ){
                                cout << "Nested if-else statement." << endl;
                                winGame();
                                setMove( move );
                        }
                        else
                                setMove(move);
                }
                else{
                        cout << "Invalid Move. Please reenter." << endl;
                        cin >> move;
                }

                DrawBoard();
                turns++;

        } while ( turns <= 9 );

}
bool TicTacToe::winGame(){
        cout << "Calling winGame() "  << endl;
        bool validate = false;
        int k = 0;
        for ( int i = 0; i < COL; i++ ){
                //check column wins
                if ( board[0][i].getMarker() == board[1][i].getMarker() && board[1][i].getMarker() == board[2][i].getMarker() && board[2][i].getMarker() != (' ')){
                        cout << "Column win " << endl;
                        validate = true;
                        break;
                }
                //check row wins
                 else if ( board[i][0].getMarker() == board[i][1].getMarker() && board[i][1].getMarker() == board[i][2].getMarker() && board[i][2].getMarker() !=  (' ')){
                        cout << "Row win." << endl;
                        validate = true;
                        break;
                }
        }

        if( board[0][0].getMarker() == board[1][1].getMarker() && board[1][1].getMarker() == board[2][2].getMarker() && board[2][2].getMarker() != (' ')){
                cout << "Diagonal 1" << endl;
                validate = true;
        }
        else if ( board[0][2].getMarker() == board[1][1].getMarker() && board[1][1].getMarker() == board[2][0].getMarker() && board[2][0].getMarker() != (' ') ){
                cout << "Diagonal 2 " << endl;
                validate = true;
        }
        else{
                cout << "It's a draw!" << endl;
                validate = true;
        }

        return (validate);
}
//sample run
+--+--+--+
|1 |2 |3 |
+--+--+--+
|4 |5 |6 |
+--+--+--+
|7 |8 |9 |
+--+--+--+
Player 1 make a move.
1

+--+--+--+
|X |2 |3 |
+--+--+--+
|4 |5 |6 |
+--+--+--+
|7 |8 |9 |
+--+--+--+
Player 2 make a move.
2

+--+--+--+
|X |O |3 |
+--+--+--+
|4 |5 |6 |
+--+--+--+
|7 |8 |9 |
+--+--+--+
Player 1 make a move.
3

+--+--+--+
|X |O |X |
+--+--+--+
|4 |5 |6 |
+--+--+--+
|7 |8 |9 |
+--+--+--+
Player 2 make a move.
5

+--+--+--+
|X |O |X |
+--+--+--+
|4 |O |6 |
+--+--+--+
|7 |8 |9 |
+--+--+--+
Player 1 make a move.
+--+--+--+
|X |O |X |
+--+--+--+
|4 |O |6 |
+--+--+--+
|7 |8 |9 |
+--+--+--+
Player 1 make a move.
7
Nested if-else statement.
Calling winGame()
It's a draw!

+--+--+--+
|X |O |X |
+--+--+--+
|4 |O |6 |
+--+--+--+
|X |8 |9 |
+--+--+--+
Player 2 make a move.
8
Nested if-else statement.
Calling winGame()
It's a draw!

+--+--+--+
|X |O |X |
+--+--+--+
|4 |O |6 |
+--+--+--+
|X |O |9 |
+--+--+--+
Problem: winGame can never return false;

In order for a game to be a draw, all spaces on the board should be used.
There are two completed Tic Tac Toe programs here :
http://www.cplusplus.com/forum/beginner/181439/
Here's how i did mine and it works:

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
#include <iostream>
using namespace std;

int playagain();

int main()
{
int board[3][3],x,y,loopcount=0;
char dispboard[3][3];
int PlayerA=79, PlayerB=88;
int i=0;
int winchecker=0, checkx=0, checky=0, leftdiag=0, rightdiag=0;
bool win=0;
cout<<"Player A=X, Player B=O"<<endl;
cout<<"[X,Y]"<<endl;
cout<<"[0,0]\t[1,0]\t[2,0]\n";
cout<<"[0,1]\t[1,1]\t[2,1]\n";
cout<<"[0,2]\t[1,2]\t[2,2]\n\n";


	while(loopcount<9 & win!=1)
	{
	i++;
	//to set board
	cout<<"CURRENT BOARD";
	for (y=0;y<3; y++)
		{
			cout<<endl<<"----------------------"<<endl;;
			for (x=0;x<3; x++)
			{
			if (loopcount==0) {board[x][y]=0;}
			dispboard[x][y]=board[x][y];
			cout<<dispboard[x][y]<<"\t";
		}
		}
	cout<<endl<<"----------------------"<<endl;

	//game play
	{
	do{
	if ((i % 2)!=0) {cout<<"Player A\n";loopcount++;}
	else {cout<<"Player B\n";loopcount++;}
	cin>>x; cin>>y;
	cout<<endl;
	if ((x>2 or x<0) or (y>2 or y<0) or (board[x][y]!=0)){cout<<"Error\n"; loopcount--;}
	if (board[x][y]==0)
	{
		if ((i % 2)!=0) {board[x][y]=PlayerA;}
		else {board[x][y]=PlayerB;}
	}
	else {x=5;}
	
	// to determine winner-vertical
	for (checkx=0;checkx<3;checkx++)
	{
	for (checky=0;checky<3;checky++)
	{
	winchecker+=board[checkx][checky];
	if (winchecker==(3*PlayerA)){cout<<"winner-player A"; winchecker=0; win=1; break;}
	if (winchecker==(3*PlayerB)){cout<<"winner-player B"; winchecker=0; win=1; break;}
	}
	winchecker=0;
	}
	
	// to determine winner-horizontal
	for (checky=0;checky<3;checky++)
	{
	for (checkx=0;checkx<3;checkx++)
	{
	winchecker+=board[checkx][checky];
	if (winchecker==(3*PlayerA)){cout<<"winner-player A"; winchecker=0; win=1; break;}
	if (winchecker==(3*PlayerB)){cout<<"winner-player B"; winchecker=0; win=1; break;}
	}
	winchecker=0;
	}
	
	//to determine winner-left diagnal (left top start) & right diagnal
	{
	leftdiag=board[0][0]+board[1][1]+board[2][2];
	rightdiag=board[2][0]+board[1][1]+board[0][2];
	
	if (((leftdiag==(3*PlayerA))) or ((rightdiag==(3*PlayerA)))){cout<<"winner-player A"; winchecker=0; win=1; break;}
	if (((leftdiag==(3*PlayerB))) or ((rightdiag==(3*PlayerB)))){cout<<"winner-player B"; winchecker=0; win=1; break;} 
	}
			
	if ((win!=1) && loopcount==9){cout<<"No Winners Idiots";}
	}while((x>2 or x<0) or (y>2 or y<0));
	}
	}	
	cout<<endl;
	playagain();
	return 0;
}


int playagain()
{
	int playagain;
	cout<<"Press 1 to play again, anything else to exit\n";
	cin>>playagain;
	while(playagain==1)
	{
	main();
	}
return 0;
}
Topic archived. No new replies allowed.