### TicTacToe Exercise

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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324`` ``````#include #include #include 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'); }``````
I myself would recommend you first start eliminating the global variables. Learn to properly pass the variables to and from your functions.

closed account (3qX21hU5)
 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.
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.