Guest User

Tic Tac Toe using 2d array

a guest
Mar 6th, 2011
5,121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.09 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. //Tic Tac Toe using 2d arrays
  4. //By Devin K.
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     int playerTurn=1;
  11.     const int ROW=3;
  12.     const int COL=3;
  13.     bool bGameOver= false;
  14.     char board[ROW][COL] = {{ '1', '2', '3'},
  15.                          {'4', '5', '6'},
  16.                          {'7', '8', '9'}};
  17.    
  18.    
  19.     while (!bGameOver)//main game loop
  20.     {
  21.    
  22.           // print board
  23.           for (int i = 0; i<ROW; ++i)
  24.           {
  25.               cout<<board[i][0]<<"|"<<board[i][1]<<"|"<<board[i][2]<<endl;
  26.               if (i==0 || i==1)
  27.               {
  28.                       cout<<"-+-+-"<<endl;
  29.               }
  30.           }
  31.          
  32.           // set player mark
  33.           char playerMark;
  34.           if (playerTurn == 1) // sets player mark, X for player 1 O for play 2
  35.           {
  36.               playerMark = 'X';
  37.           }
  38.           else
  39.           {
  40.               playerMark = 'O';
  41.           }
  42.                    
  43.           // ask for player move
  44.           char nextMove;
  45.           cout<<"Player "<<playerTurn<<" please pick a spot to place "<<playerMark<<" using numbers from the board"<<endl;
  46.          
  47.          
  48.          
  49.           bool validMove=false;
  50.          
  51.           while (!validMove) // checks for valid move
  52.           {
  53.                 validMove=true;
  54.                 cin>>nextMove;
  55.                 if(nextMove == '1' && board[0][0] == '1')
  56.                 {
  57.                             board[0][0] = playerMark;
  58.                 }
  59.                 else if(nextMove == '2' && board[0][1] == '2')
  60.                 {
  61.                             board[0][1] = playerMark;
  62.                 }
  63.                 else if(nextMove == '3' && board[0][2] == '3')
  64.                 {
  65.                             board[0][2] = playerMark;
  66.                 }
  67.                 else if(nextMove == '4' && board[1][0] == '4')
  68.                 {
  69.                             board[1][0] = playerMark;
  70.                 }
  71.                 else if(nextMove == '5' && board[1][1] == '5')
  72.                 {
  73.                             board[1][1] = playerMark;
  74.                 }
  75.                 else if(nextMove == '6' && board[1][2] == '6')
  76.                 {
  77.                             board[1][2] = playerMark;
  78.                 }
  79.                 else if(nextMove == '7' && board[2][0] == '7')
  80.                 {
  81.                             board[2][0] = playerMark;
  82.                 }
  83.                 else if(nextMove == '8' && board[2][1] == '8')
  84.                 {
  85.                             board[2][1] = playerMark;
  86.                 }
  87.                 else if(nextMove == '9' && board[2][2] == '9')
  88.                 {
  89.                             board[2][2] = playerMark;
  90.                 }
  91.                 else
  92.                 {
  93.                                  cout<<"Invalid Move, please try again!"<<endl;
  94.                                  validMove = false;
  95.                 }
  96.           }
  97.           // check for game over conditions
  98.           if (board[0][0] != '1') // see if row 0 or col 0 are the same playerMark
  99.           {
  100.                           if (board[0][0] == board[0][1] && board[0][0] == board[0][2]) // checks row 0
  101.                           {
  102.                                           bGameOver=true;
  103.                           }
  104.                           if (board[0][0] == board[1][0] && board[0][0] == board[2][0])//checks col 0
  105.                           {
  106.                                           bGameOver=true;
  107.                           }
  108.           }
  109.           if (board[2][2] != '9')// see if row 2 or col 2 are the same playrMark
  110.           {
  111.                           if (board[2][2] == board[2][1] && board[2][2] == board[2][0])// checks row 2
  112.                           {
  113.                                           bGameOver=true;
  114.                           }
  115.                           if (board[2][2] == board[1][2] && board[2][2] == board[0][2])// checks col 2
  116.                           {
  117.                                           bGameOver=true;
  118.                           }
  119.           }
  120.           if (board[1][1] != '5')
  121.           {
  122.                           if(board[1][1] == board[0][1] && board[1][1] == board[2][1])// checks col 1
  123.                           {
  124.                                          bGameOver=true;
  125.                           }
  126.                           if(board[1][1] == board[1][0] && board[1][1] == board[1][2])// checks row 1
  127.                           {
  128.                                          bGameOver=true;
  129.                           }
  130.                           if(board[1][1] == board[0][0] && board[1][1] == board[2][2])// check if 1 and 9 are equal to 5
  131.                           {
  132.                                          bGameOver=true;
  133.                           }
  134.                           if(board[1][1] == board[2][0] && board[1][1] == board[0][2])// check if 3 and 7 are equal to 5
  135.                           {
  136.                                          bGameOver=true;
  137.                           }
  138.           }
  139.          
  140.           // check for a tie
  141.           bool bWinGame=true; // assumes someone won
  142.           if (board[0][0] !='1' && board[0][1] !='2' && board[0][2] !='3' &&
  143.               board[1][0] !='4' && board[1][1] !='5' && board[1][2] !='6' &&
  144.               board[2][0] !='7' && board[2][1] !='8' && board[2][2] !='9' &&
  145.               !bGameOver)
  146.           {
  147.               bGameOver=true;
  148.               bWinGame=false;
  149.               cout<<"Its a Tie!"<<endl;
  150.           }
  151.          
  152.           // congradulate winner if bGameOver and ask to play again
  153.          
  154.           while (bGameOver)
  155.           {
  156.                         // print board
  157.                         for (int i = 0; i<ROW; ++i)
  158.                         {
  159.                             cout<<board[i][0]<<"|"<<board[i][1]<<"|"<<board[i][2]<<endl;
  160.                             if (i==0 || i==1)
  161.                             {
  162.                                      cout<<"-+-+-"<<endl;
  163.                             }
  164.                         }
  165.                        
  166.                         if(bWinGame) // gratz on winning
  167.                         {
  168.                                     cout<<"Player "<<playerTurn<<" wins!\n";
  169.                         }
  170.                        
  171.                         char again;
  172.                         cout<<"Would you like to play again? (y/n)";
  173.                         cin>>again;
  174.                        
  175.                         if (again == 'y' || again == 'Y')
  176.                         {
  177.                                   bGameOver=false; // reset game
  178.                                   bWinGame=true; // assume someone will win next game
  179.                                  
  180.                                   board[0][0] = '1';
  181.                                   board[0][1] = '2';
  182.                                   board[0][2] = '3';
  183.                                   board[1][0] = '4';
  184.                                   board[1][1] = '5';
  185.                                   board[1][2] = '6';
  186.                                   board[2][0] = '7';
  187.                                   board[2][1] = '8';
  188.                                   board[2][2] = '9';
  189.                         }
  190.                        
  191.                         else if (again == 'n' || again == 'N')
  192.                         {
  193.                                   cout<<"Thanks for playing!\n"; // say thanks for playing
  194.                                   break;
  195.                         }
  196.                        
  197.                         else
  198.                         {
  199.                             cout<<"Please use y or n only"<<endl;
  200.                         }
  201.           }
  202.          
  203.           if (!bGameOver) // if the game is not over switch turns
  204.           {
  205.                          if (playerTurn==1)
  206.                          {
  207.                                          playerTurn=2;
  208.                          }
  209.                          else
  210.                          {
  211.                                          playerTurn=1;
  212.                          }
  213.           }    
  214.     } // game loop over
  215.  system("Pause");
  216.  return 0;
Advertisement
Add Comment
Please, Sign In to add comment