Tic Tac Toe

Hi, I have been working on this Tic Tac Toe project for literally over 40 hours the last few weeks, and I cannot get it to work right. Right now I'm working on the twoHuman() function, haven't even gotten to the rest yet, so ignore what is there. The problem I'm having is that When there is a winner, it does not recognize it. Same thing if there is a scratch. Here is my code:

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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244

#include <iostream> 
#include <iomanip> 
#include "ttt.h" 
using namespace std;

char status;
int counter = 0;

ttt::ttt() // constructor initializes the  board
{
	 for ( int j = 0; j < 3; j++ ) 

      for ( int k = 0; k < 3; k++ )
         board_[ j ][ k ] = ' ';
}


char ttt::CheckForWinner()
{   
   int a;

   // check for a win on diagonals
   if ( board_[ 0 ][ 0 ] != ' ' && board_[ 0 ][ 0 ] == board_[ 1 ][ 1 ] &&
       board_[ 0 ][ 0 ] == board_[ 2 ][ 2 ] )
	   {
	   status = board_[ 0 ][ 0 ];
       cout << "Player" << status << "wins!!!";
	   return status;
	   }
   else if ( board_[ 2 ][ 0 ] != ' ' && board_[ 2 ][ 0 ] == 
      board_[ 1 ][ 1 ] && board_[ 2 ][ 0 ] == board_[ 0 ][ 2 ] )
	   {
	   status = board_[ 2 ][ 0 ];
	   cout << "Player" << status << "wins!!!";	  
	   return status;
	   }	
   // check for win in rows
   for ( a = 0; a < 3; ++a )

      if ( board_[ a ][ 0 ] != ' ' && board_[ a ][ 0 ] == 
         board_[ a ][ 1 ] && board_[ a ][ 0 ] == board_[ a ][ 2 ] )
		  {
		  status = board_[ a ][ 0 ];
          cout << "Player" << status << "wins!!!";
		  return status;
		  }
   // check for win in columns
   for ( a = 0; a < 3; ++a )

      if ( board_[ 0 ][ a ] != ' ' && board_[ 0 ][ a ] == 
         board_[ 1 ][ a ] && board_[ 0 ][ a ] == board_[ 2 ][ a ] )
		  {
		  status = board_[ 0 ][ a ];
          cout << "Player" << status << "wins!!!";
		  return status;
		  }
   // check for a completed game
   for ( int r = 0; r < 3; ++r )

      for ( int c = 0; c < 3; ++c )

         if ( board_[ r ][ c ] == ' ' )
			 {
			 status = 'C';
             return status; // game is not finished
			 }

   status = ' ';	
   return status; // game is a draw
} // end function gameStatus


void ttt::printBoard() 
{
   cout << "   0    1    2\n\n";

   for ( size_t x = 0; x < 3; ++x ) 
   {
      cout << x;
	char value = 1;
      for ( size_t y  = 0; y < 3; ++y ) 
      {
         cout << setw( 3 ) <<  static_cast < char > (board_[ x ][ y ]);

         if ( y != 2 )
            cout << " |";
      } // end for

      if ( x != 2 )
         cout << "\n ____|____|____\n     |    |    \n";
   } // end for

   cout << "\n\n";
} 



bool ttt::IsValidLocation(size_t x, size_t y)
{
    if ( x < 0 || x > 3 )
    {
        return false;
    }
    else if ( y < 0 || y > 3 )
    {
        return false;
    }
    else if ( board_[ x ][ y ] != ' ' )
    {
        return false;
    }
    else
    {
        return true;
    }
}
bool ttt::OccupySquare(size_t x, size_t y, char value)
{
	bool valid = IsValidLocation( x, y );

	if ( !IsValidLocation( x, y ))
    {
	    
			return true;
	}
	
}

void ttt::twoComp()
{	
	size_t x=0;
	size_t y=0;
	char value = 0;
	int counter = 1;
	int turn;

	ttt g;
	cout << "Computer vs. Computer\n" << endl;
	g.printBoard();

		do
	{
		cout << "Player" ;
		if ( counter % 2 == 1 )
			{
				cout << " X ";
			}
		else if ( counter % 2 == 0 )
			{
				cout << " O ";
			}
		cout << "enter coordinates:" << endl;
		cin >> x >> y;

		if ( counter % 2 == 1 && IsValidLocation( x, y ) )
			{
				board_[ x ][ y ] = 'X';
				counter++; 
				cout << board_[ x ][ y ];
				printBoard();
				g.CheckForWinner();
			}

		if ( counter % 2 == 0 && IsValidLocation( x, y ) )
			{				
				board_[ x ][ y ] = 'O';
				counter++;
				cout << board_[ x ][ y ];
				printBoard();		
				g.CheckForWinner();
			}
		
	} while (  OccupySquare( x, y, value ));
}

void ttt::compHuman()
{
	ttt g;
	cout << "Human vs. Computer\n" << endl;
	g.printBoard();

}

void ttt::twoHuman()
{
	ttt g;
	cout << "Human vs. Human\n" << endl;
	g.printBoard();
	size_t x=0;
	size_t y=0;
	char value = 0;
	int counter = 1;
	int turn;

	
	do
	{
		cout << "Player" ;
		if ( counter % 2 == 1 )
			{
				cout << " X ";
			}
		else if ( counter % 2 == 0 )
			{
				cout << " O ";
			}
		cout << "enter coordinates:" << endl;
		cin >> x >> y;

		if ( counter % 2 == 1 && IsValidLocation( x, y ) )
			{
				board_[ x ][ y ] = 'X';
				counter++; 
				cout << board_[ x ][ y ];
				printBoard();
				g.CheckForWinner();
			}

		if ( counter % 2 == 0 && IsValidLocation( x, y ) )
			{				
				board_[ x ][ y ] = 'O';
				counter++;
				cout << board_[ x ][ y ];
				printBoard();		
				g.CheckForWinner();
			}
		
	} while (  status != ' ' );




}


bool ttt::IsScratch()
{
	if ( counter == 9 )
		return true;
}


In your functions that return Booleans, half of the time you explicitly return a value; however, the other half of the time your functions return whatever is on the stack (usually non-zero. For example IsScratch() could be coded this way:

1
2
3
4
5
6
7
8
9
10
11
12
13

bool ttt::IsScratch()
    {
     int brc = false;

    if( 9 == counter )
        brc = true;

    return brc;

    }
        /*    IsScratch()    */
Topic archived. No new replies allowed.