TicTacToe Exercise
Apr 12, 2013 at 1:56pm UTC
I've done the TacTacToe exercise, And it look' a bit bloated with if staement's.
Is there a way i could have done it with i.e. for loop's ?
Open to any criticism of my programming.
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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
#include <iostream>
#include <array>
#include <cstdlib>
int Carr[9] = {3,3,3,3,3,3,3,3,3};
int ArrPos = 0;
int Player = 1;
int Winner = 0;
int MoveNotLegal =1;
char LetInp;
int GameLive = 1;
int Xcoord = 0;
int Ycoord = 0;
int P2Let = 0;
int P2Num = 0;
int Valid = 0;
int Column =0;
int Moves =0;
int RedrawBoard() // 1 = Plyr 1; 2 = Plyr 2; 3 = Blank
{
int Column = 1;
std::cout << std::endl;
std::cout << " A B C\n\n" << Column;
int LineCount = 1;
for (int y = 0; y <= 8; ++y, ++LineCount)
{
if (Carr[y] == 1)
{
std::cout << " X" ;
}
if (Carr[y] == 2)
{
std::cout << " O" ;
}
if (Carr[y] == 3)
{
std::cout << " *" ;
}
if (LineCount == 3 && Column != 3)
{
LineCount = 0;
++Column;
std::cout << '\n' << Column;
}
}
return 0;
};
void ResetBoard()
{
for (int i = 0; i <= 8; ++i)
Carr[i] = 3;
};
int GetPlayerMove(int Plyr)
{
MoveNotLegal = 1;
do
{
std::cout << "\nPlayer " << Plyr << " Enter column from (A - C): " ;
std::cin >> LetInp;
switch (LetInp)
{
case ('a' ):
Ycoord = 0;
break ;
case ('b' ):
Ycoord = 1;
break ;
case ('c' ):
Ycoord = 2;
break ;
}
std::cout << "\nPlayer " << Plyr << " Enter row from (1-3): " ;
std::cin >> Xcoord;
--Xcoord;
ArrPos = (3 * Xcoord) + Ycoord;
if (Carr[ArrPos] == 3)
{
Carr[ArrPos] = Plyr;
MoveNotLegal = 0;
}
}
while (MoveNotLegal == 1);
MoveNotLegal = 1;
return 0;
}
void CheckForWin()
{
if (Carr[0] == Player && Carr[3] == Player && Carr[6] == Player)
{
GameLive = 0;
}
if (Carr[1] == Player && Carr[4] == Player && Carr[7] == Player)
{
GameLive = 0;
}
if (Carr[2] == Player && Carr[5] == Player && Carr[8] == Player)
{
GameLive = 0;
}
if (Carr[0] == Player && Carr[1] == Player && Carr[2] == Player)
{
GameLive = 0;
}
if (Carr[3] == Player && Carr[4] == Player && Carr[5] == Player)
{
GameLive = 0;
}
if (Carr[6] == Player && Carr[7]== Player && Carr[8] == Player)
{
GameLive = 0;
}
if (Carr[0] == Player && Carr[4] == Player && Carr[8] == Player)
{
GameLive = 0;
}
if (Carr[2] == Player && Carr[4] == Player && Carr[8] == Player)
{
GameLive = 0;
}
if (GameLive == 0)
{
Winner = Player;
}
}
void Player2Move()
{
Valid = 0;
Column = 0;
if (Carr[0] == 1 && Carr[1] == 1 && Carr[2] == 3) // Horizontal Block Ckeck.
{
Carr[2] = Player;
Valid = 1;
}
if (Valid == 0 && Carr[1] == 1 && Carr[2] == 1 && Carr[0] == 3)
{
Carr[0] = Player;
Valid = 1;
}
if (Valid == 0 && Carr[0] == 1 && Carr[2] == 1 && Carr[1] == 3)
{
Carr[1] = Player;
Valid = 1;
}
if (Valid == 0 && Carr[3] == 1 && Carr[4] == 1 && Carr[5] == 3)
{
Carr[5] = Player;
Valid = 1;
}
if (Valid == 0 && Carr[4] == 1 && Carr[5] == 1 && Carr[3] == 3)
{
Carr[3] = Player;
Valid = 1;
}
if (Valid == 0 && Carr[3] == 1 && Carr[5] == 1 && Carr[4] == 3)
{
Carr[4] = Player;
Valid = 1;
}
if (Valid == 0 && Carr[6] == 1 && Carr[7] == 1 && Carr[8] == 3)
{
Carr[8] = Player;
Valid = 1;
}
if (Valid == 0 && Carr[7] == 1 && Carr[8] == 1 && Carr[6] == 3)
{
Carr[6] = Player;
Valid = 1;
}
if (Valid == 0 && Carr[6] == 1 && Carr[8] == 1 && Carr[7] == 3)
{
Carr[7] = Player;
Valid = 1;
}
if (Valid == 0 && Carr[0] == 1 && Carr[3] == 1 && Carr[6] == 3) // Vertical Block Check.
{
Carr[6] = Player;
Valid = 1;
}
if (Valid == 0 && Carr[0] == 1 && Carr[6] == 1 && Carr[3] == 3)
{
Carr[3] = Player;
Valid = 1;
}
if (Valid == 0 && Carr[3] == 1 && Carr[6] == 1 && Carr[0] == 3)
{
Carr[0] = Player;
Valid = 1;
}
if (Valid == 0 && Carr[1] == 1 && Carr[4] == 1 && Carr[7] == 3)
{
Carr[7] = Player;
Valid = 1;
}
if (Valid == 0 && Carr[1] == 1 && Carr[7] == 1 && Carr[4] == 3)
{
Carr[4] = Player;
Valid = 1;
}
if (Valid == 0 && Carr[4] == 1 && Carr[7] == 1 && Carr[1] == 3)
{
Carr[1] = Player;
Valid = 1;
}
if (Valid == 0 && Carr[2] == 1 && Carr[5] == 1 && Carr[8] == 3)
{
Carr[8] = Player;
Valid = 1;
}
if (Valid == 0 && Carr[2] == 1 && Carr[8] == 1 && Carr[5] == 3)
{
Carr[5] = Player;
Valid = 1;
}
if (Valid == 0 && Carr[5] == 1 && Carr[8] == 1 && Carr[2] == 3)
{
Carr[2] = Player;
Valid = 1;
}
if (Valid == 0 && Carr[0] == 1 && Carr[4] == 1 && Carr[8] == 3)
{
Carr[8] = Player;
Valid = 0;
}
if (Valid == 0 && Carr[0] == 1 && Carr[8] == 1 && Carr[4] == 3)
{
Carr[4] = Player;
Valid = 0;
}
if (Valid == 0 && Carr[4] == 1 && Carr[8] == 1 && Carr[0] == 3)
{
Carr[0] = Player;
Valid = 0;
}
if (Valid == 0 && Carr[2] == 1 && Carr[4] == 1 && Carr[6] == 3)
{
Carr[6] = Player;
Valid = 0;
}
if (Valid == 0 && Carr[2] == 1 && Carr[6] == 1 && Carr[4] == 3)
{
Carr[4] = Player;
Valid = 0;
}
if (Valid == 0 && Carr[4] == 1 && Carr[6] == 1 && Carr[2] == 3)
{
Carr[2] = Player;
Valid = 0;
}
if (Valid == 0)
{
do
{
P2Let = rand() %3;
P2Num = rand() %3;
if (Carr[(3 * P2Num) + P2Let] == 3)
{
Carr[(3 * P2Num) + P2Let] = Player;
Valid =1;
}
}
while (Valid == 0);
}
}
int main()
{
char PlayAgain = 'y' ;
RedrawBoard();
Moves = 0;
do
{
GetPlayerMove(Player);
RedrawBoard();
CheckForWin();
++Player;
++Moves;
if (Moves == 9)
{
GameLive = 0;
}
if (GameLive == 1)
{
Player2Move();
RedrawBoard();
CheckForWin();
--Player;
++Moves;
if (Moves == 9)
{
GameLive = 0;
}
}
if (GameLive == 0)
{
if (Winner != 0)
{
std::cout << "\nPlayer " << Winner << " wins!" ;
}
else
{
std::cout << "\nGame drawn!" ;
}
std::cout << "\nWould you like to play again? (y or n) " ;
std::cin >> PlayAgain;
ResetBoard();
if (PlayAgain == 'y' )
{
RedrawBoard();
Player = 1;
GameLive = 1;
Winner = 0;
}
}
}
while (PlayAgain == 'y' || PlayAgain == 'Y' );
}
Apr 12, 2013 at 2:34pm UTC
I myself would recommend you first start eliminating the global variables. Learn to properly pass the variables to and from your functions.
Apr 12, 2013 at 2:42pm UTC
I myself would recommend you first start eliminating the global variables. Learn to properly pass the variables to and from your functions.
Agreed.
I also didn't really look into your code all that well but you don't seem to have the Winning Game Logic just right. When I ran your program (By the way the computer made nice moves and wasn't to stupid so nice job on that ;p) it ignored when I won the game and just called the game a draw so you might want to look at that.
Here is my output for when I played the game and shows what should be winning game being counted as a draw. It also didn't end the game when I got 3 in a row which would be expected, instead it went until the whole board was filled.
A B C
1 * * *
2 * * *
3 * * *
Player 1 Enter column from (A - C): a
Player 1 Enter row from (1-3): 1
A B C
1 X * *
2 * * *
3 * * *
A B C
1 X * *
2 * * *
3 * * O
Player 1 Enter column from (A - C): c
Player 1 Enter row from (1-3): 1
A B C
1 X * X
2 * * *
3 * * O
A B C
1 X O X
2 * * *
3 * * O
Player 1 Enter column from (A - C): a
Player 1 Enter row from (1-3): 3
A B C
1 X O X
2 * * *
3 X * O
A B C
1 X O X
2 O * *
3 X * O
Player 1 Enter column from (A - C): b
Player 1 Enter row from (1-3): 2
A B C
1 X O X
2 O X *
3 X * O
A B C
1 X O X
2 O X O
3 X * O
Player 1 Enter column from (A - C): a
Player 1 Enter row from (1-3): 3
Player 1 Enter column from (A - C): b
Player 1 Enter row from (1-3): 3
A B C
1 X O X
2 O X O
3 X X O
Game drawn!
Would you like to play again? (y or n) n
Process returned 0 (0x0) execution time : 98.202 s
Press any key to continue.
Hope that can help a bit, I will look into the code more in a bit and give some more in depth comments on the source code.
Apr 13, 2013 at 11:52am UTC
Thank's for your comment's.
Found an error in the win check at line 121
if (Carr[2] == Player && Carr[4] == Player && Carr[8] == Player)
should have been:
if (Carr[2] == Player && Carr[4] == Player && Carr[6] == Player)
Also forgot to reset the moves counter back to zero. This was causing a tie before a game was finished.
Probably rewrite it now... Without the globals :)
Topic archived. No new replies allowed.