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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
using namespace std;
bool checkwin();
char player1_mark;
char player2_mark;
//********BOARDSTATUS CLASS*********
//Holds information about board.
//
class Boardstatus
{
char A1;
char A2;
char A3;
char B1;
char B2;
char B3;
char C1;
char C2;
char C3;
public:
int setsquarestatus( string square, char status )
{
int squarenumber = enumerator ( square );
switch ( squarenumber )
{
case 1:
A1 = status;
break;
case 2:
A2 = status;
break;
case 3:
A3 = status;
break;
case 4:
B1 = status;
break;
case 5:
B2 = status;
break;
case 6:
B3 = status;
break;
case 7:
C1 = status;
break;
case 8:
C2 = status;
break;
case 9:
C3 = status;
break;
}
}
char getsquarestatus( string square )
{
int squarenumber = enumerator( square );
switch ( squarenumber )
{
case 1:
return A1;
break;
case 2:
return A2;
break;
case 3:
return A3;
break;
case 4:
return B1;
break;
case 5:
return B2;
break;
case 6:
return B3;
break;
case 7:
return C1;
break;
case 8:
return C2;
break;
case 9:
return C3;
break;
}
}
//End of public
private:
//This function acts similar to an enumerator, but instead returns a number 1-9 based
//on what string x is entered so that the switch cases in get and setsquarestatus will
//function properly (they require int or char to work)
int enumerator( string x )
{
if ( x == "A1" )
{
return 1;
}
else if ( x == "A2" )
{
return 2;
}
else if (x == "A3")
{
return 3;
}
else if (x == "B1")
{
return 4;
}
else if (x == "B2")
{
return 5;
}
else if (x == "B3")
{
return 6;
}
else if (x == "C1")
{
return 7;
}
else if (x == "C2")
{
return 8;
}
else if (x == "C3")
{
return 9;
}
}
};
//*********BOARDPRINT FUNCTION***********
//
//
void boardprint( Boardstatus& BSTAT)
{
system("cls");
cout << "\n\n\tTic Tac Toe\n\n";
cout << "Player 1 (" << player1_mark << ") - Player 2 (" << player2_mark << ")" << endl << endl;
cout << endl;
cout << " 1 2 3" << endl;
cout << " | | " << endl;
cout << "A " << BSTAT.getsquarestatus("A1") << " | " << BSTAT.getsquarestatus("A2") << " | " << BSTAT.getsquarestatus("A3") << endl;
cout << " _____|_____|_____" << endl;
cout << " | | " << endl;
cout << "B " << BSTAT.getsquarestatus("B1") << " | " << BSTAT.getsquarestatus("B2") << " | " << BSTAT.getsquarestatus("B3") << endl;
cout << " _____|_____|_____" << endl;
cout << " | | " << endl;
cout << "C " << BSTAT.getsquarestatus("C1") << " | " << BSTAT.getsquarestatus("C2") << " | " << BSTAT.getsquarestatus("C3") << endl;
cout << " | | " << endl << endl;
}
bool checkwin( Boardstatus& BSTAT)
{
//Row 'A' checkwin
if (BSTAT.getsquarestatus("A1")==BSTAT.getsquarestatus("A2") && BSTAT.getsquarestatus("A2")==BSTAT.getsquarestatus("A3"))
{
return true;
}
//Row 'B' checkwin
else if (BSTAT.getsquarestatus("B1")==BSTAT.getsquarestatus("B2") && BSTAT.getsquarestatus("B2")==BSTAT.getsquarestatus("B3"))
{
return true;
}
//Row 'C' checkwin
else if (BSTAT.getsquarestatus("C1")==BSTAT.getsquarestatus("C2") && BSTAT.getsquarestatus("C2")==BSTAT.getsquarestatus("C3"))
{
return true;
}
//Column 1 checkwin
else if (BSTAT.getsquarestatus("A1")==BSTAT.getsquarestatus("B1") && BSTAT.getsquarestatus("B1")==BSTAT.getsquarestatus("C1"))
{
return true;
}
//Column 2 checkwin
else if (BSTAT.getsquarestatus("A2")==BSTAT.getsquarestatus("B2") && BSTAT.getsquarestatus("B2")==BSTAT.getsquarestatus("C2"))
{
return true;
}
//Column 3 checkwin
else if (BSTAT.getsquarestatus("A3")==BSTAT.getsquarestatus("B3") && BSTAT.getsquarestatus("B3")==BSTAT.getsquarestatus("C3"))
{
return true;
}
//Diagonal upper-left->bottom-right checkwin
else if (BSTAT.getsquarestatus("A1")==BSTAT.getsquarestatus("B2") && BSTAT.getsquarestatus("B2")==BSTAT.getsquarestatus("C3"))
{
return true;
}
//Diagonal lower-left->upper-right checkwin
else if (BSTAT.getsquarestatus("A3")==BSTAT.getsquarestatus("B2") && BSTAT.getsquarestatus("B2")==BSTAT.getsquarestatus("C1"))
{
return true;
}
else
{
return false;
}
}
void playerturn( Boardstatus& BSTAT )
{
string squareselection;
cout << "Player 1, enter the square you want to play.";
cin >> squareselection;
BSTAT.setsquarestatus ( squareselection, player1_mark );
boardprint(BSTAT);
checkwin(BSTAT);
//Note, the if statement below is supposed check if checkwin is false, but for some reason it only works if it checks checkwin is true. This means checkwin is returning true when it isn't supposed to.
if (checkwin(BSTAT)==true)
{
cout << "Player 2, enter the square you want to play.";
cin >> squareselection;
BSTAT.setsquarestatus ( squareselection, player2_mark );
boardprint(BSTAT);
}
}
int main()
{
//Start of initializing all squares as a blank
Boardstatus BSTAT;
BSTAT.setsquarestatus( "A1", ' ' );
BSTAT.setsquarestatus( "A2", ' ' );
BSTAT.setsquarestatus( "A3", ' ' );
BSTAT.setsquarestatus( "B1", ' ' );
BSTAT.setsquarestatus( "B2", ' ' );
BSTAT.setsquarestatus( "B3", ' ' );
BSTAT.setsquarestatus( "C1", ' ' );
BSTAT.setsquarestatus( "C2", ' ' );
BSTAT.setsquarestatus( "C3", ' ' );
//End of square initialization
boardprint(BSTAT);
do
{
cout << "Player 1, choose X or O: ";
cin >> player1_mark;
cin.clear();
cin.sync();
if ( player1_mark == 'X' )
{
player2_mark = 'O';
}
else if (player1_mark == 'O' )
{
player2_mark = 'X';
}
else
{
cout << "Invalid character. Please try again." << endl;
}
} while ( player1_mark != 'X' && player1_mark != 'O');
boardprint(BSTAT);
//The do while statement needs to check if checkwin is false, not true, again showing that checkwin is returning true when it isn't supposed to.
do
{
playerturn(BSTAT);
} while (checkwin(BSTAT) == true);
return 0;
}
|