Advertisement
Lawnknome

ticTacToe.cpp

Nov 16th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <stdlib.h>
  4. using namespace std;
  5.  
  6. void blank_board(char board[][4], int size);
  7. bool player_turn(char board[][4], int size, bool &player_1);
  8. bool win_check_X(char board[][4], int size, int &wins_1);
  9. bool win_check_O(char board[][4], int size, int &wins_2);
  10. bool tie_check(char board[][4], int size);
  11.  
  12. int main (int argc, char* argv[])
  13. {
  14.     int games;          //number of games to be played
  15.     int played = 0;     //number of games played so far
  16.     int wins_1 = 0;     //number of wins for player 1
  17.     int wins_2 = 0;     //number of wins for player 2
  18.     char board[4][4];   //tic tac toe board
  19.     int size = 4;       //size of array row
  20.     bool player_1;      //bool value to help switch turns
  21.     bool over_X;        //bool value to tell if X won a game
  22.     bool over_O;        //bool value to tell if O won a game
  23.     bool tie;           //is game tied
  24.  
  25.     //if no command line entered default number of games to 1
  26.     if(argc == 1)
  27.         games = 1;
  28.  
  29.     //if command line entered, games equals number entered by user
  30.     else
  31.         games = atoi(argv[1]);
  32.          
  33.     do
  34.     {  
  35.         cout << "Tic Tac Toe is based on a 3x3 grid.  Please select your moves "
  36.          << "by entering your position in coordinate form." << endl;
  37.  
  38.         blank_board(board, size);    
  39.  
  40.         if(played % 2 == 0)
  41.         {
  42.             cout << "Player 1, you are 'X' and will be going first.\n";
  43.             player_1 = true;
  44.         }
  45.  
  46.         else
  47.         {
  48.             cout << "Player 2, you are 'O' and will be going first.\n";
  49.             player_1 = false;
  50.         }  
  51.  
  52.         do
  53.         {
  54.            
  55.             player_1 = player_turn(board, size, player_1);
  56.             over_X = win_check_X(board, size, wins_1);
  57.             over_O = win_check_O(board, size, wins_2);
  58.             tie = tie_check(board, size);
  59.  
  60.         }while((over_X == false) && (over_O == false) && (tie == false));
  61.  
  62.         if(over_X == true)
  63.             cout << "Player 1 has won the game.\n";
  64.         if(over_O == true)
  65.             cout << "Player 2 has won the game.\n";
  66.         if(tie == true)
  67.             cout << "That game was a tie.\n";
  68.  
  69.         played++;
  70.  
  71.     }while(played < games);
  72.  
  73.     cout << "Player 1 has won " << wins_1 << " games." << endl;
  74.     cout << "Player 2 has won " << wins_2 << " games." << endl;
  75.  
  76.     if(wins_1 > wins_2)
  77.         cout << "Player 1 is the overall victor.\n";
  78.     if(wins_2 > wins_1)
  79.         cout << "Player 2 is the overall victor.\n";
  80.     if(wins_1 == wins_2)
  81.         cout << "The end result is a tie.\n";
  82.  
  83.     return 0;
  84. }
  85.  
  86. void blank_board(char board[][4], int size)
  87. {
  88.     board[0][0] = ' ';
  89.     board[0][1] = '1';
  90.     board[0][2] = '2';
  91.     board[0][3] = '3';
  92.     board[1][0] = '1';
  93.     board[2][0] = '2';
  94.     board[3][0] = '3';
  95.  
  96.     board[1][1] = '.';
  97.     board[2][1] = '.';
  98.     board[3][1] = '.';
  99.     board[1][2] = '.';
  100.     board[2][2] = '.';
  101.     board[3][2] = '.';
  102.     board[1][3] = '.';
  103.     board[2][3] = '.';
  104.     board[3][3] = '.';
  105.  
  106.     for (int i = 0; i < 4; i++)
  107.     {
  108.         for (int j = 0; j < 4; j++)
  109.         {
  110.             cout << board[i][j] << " ";
  111.         }
  112.        
  113.         cout << endl;
  114.     }
  115. }
  116.  
  117. bool player_turn(char board[][4], int size, bool &player_1)
  118. {
  119.     int row;
  120.     int col;   
  121.  
  122.     if(player_1 == true)
  123.     {
  124.         cout << "Player 1, please enter your move: ";
  125.         cin >> row;
  126.         cin >> col;
  127.  
  128.         while(board[row][col] == '.')
  129.         {
  130.             cout << "That space has already been taken.\n";
  131.             cout << "Player 1, please enter another move: ";
  132.             cin >> row;
  133.             cin >> col;
  134.         }  
  135.  
  136.         board[row][col] = 'X';
  137.  
  138.         for (int i = 0; i < 4; i++)
  139.         {
  140.             for (int j = 0; j < 4; j++)
  141.             {
  142.                  cout << board[i][j] << " ";
  143.             }
  144.        
  145.             cout << endl;
  146.         }
  147.  
  148.         return false;
  149.  
  150.     }  
  151.  
  152.     if(player_1 == false)
  153.     {
  154.         cout << "Player 2, please enter your move: ";
  155.         cin >> row;
  156.         cin >> col;
  157.  
  158.         while(board[row][col] == '.')
  159.         {
  160.             cout << "That space has already been taken.\n";
  161.             cout << "Player 2, please enter another move: ";
  162.             cin >> row;
  163.             cin >> col;
  164.         }  
  165.  
  166.         board[row][col] = 'O';
  167.  
  168.         for (int i = 0; i < 4; i++)
  169.         {
  170.             for (int j = 0; j < 4; j++)
  171.             {
  172.                  cout << board[i][j] << " ";
  173.             }
  174.        
  175.             cout << endl;
  176.  
  177.         }
  178.        
  179.         return true;
  180.     }  
  181. }
  182.  
  183. bool win_check_X(char board[][4], int size, int &wins_1)
  184. {
  185.     if (board[1][1] == 'X' && board[2][2] == 'X' && board[3][3] == 'X')
  186.     {  
  187.         wins_1++;
  188.         return true;
  189.     }
  190.  
  191.     else if (board[3][1] == 'X' && board[2][2] == 'X' && board[1][3] == 'X')
  192.     {  
  193.         wins_1++;
  194.         return true;
  195.     }      
  196.    
  197.     else if (board[1][1] == 'X' && board[1][2] == 'X' && board[1][3] == 'X')
  198.     {  
  199.         wins_1++;
  200.         return true;
  201.     }      
  202.  
  203.     else if (board[1][2] == 'X' && board[2][2] == 'X' && board[3][2] == 'X')
  204.     {  
  205.         wins_1++;
  206.         return true;
  207.     }      
  208.  
  209.     else if (board[1][3] == 'X' && board[2][3] == 'X' && board[3][3] == 'X')
  210.     {  
  211.         wins_1++;
  212.         return true;
  213.     }      
  214.  
  215.     else if (board[2][1] == 'X' && board[2][2] == 'X' && board[2][3] == 'X')
  216.     {  
  217.         wins_1++;
  218.         return true;
  219.     }      
  220.  
  221.     else if (board[3][1] == 'X' && board[3][2] == 'X' && board[3][3] == 'X')
  222.     {  
  223.         wins_1++;
  224.         return true;
  225.     }      
  226.  
  227.     else if (board[1][1] == 'X' && board[2][1] == 'X' && board[3][1] == 'X')
  228.     {  
  229.         wins_1++;
  230.         return true;
  231.     }      
  232.  
  233.     else
  234.         return false;  
  235. }
  236.  
  237. bool win_check_O(char board[][4], int size, int &wins_2)
  238. {
  239.     if (board[1][1] == 'O' && board[2][2] == 'O' && board[3][3] == 'O')
  240.     {  
  241.         wins_2++;
  242.         return true;
  243.     }
  244.  
  245.     else if (board[3][1] == 'O' && board[2][2] == 'O' && board[1][3] == 'O')
  246.     {  
  247.         wins_2++;
  248.         return true;
  249.     }      
  250.    
  251.     else if (board[1][1] == 'O' && board[1][2] == 'O' && board[1][3] == 'O')
  252.     {  
  253.         wins_2++;
  254.         return true;
  255.     }      
  256.  
  257.     else if (board[1][2] == 'O' && board[2][2] == 'O' && board[3][2] == 'O')
  258.     {  
  259.         wins_2++;
  260.         return true;
  261.     }      
  262.  
  263.     else if (board[1][3] == 'O' && board[2][3] == 'O' && board[3][3] == 'O')
  264.     {  
  265.         wins_2++;
  266.         return true;
  267.     }      
  268.  
  269.     else if (board[2][1] == 'O' && board[2][2] == 'O' && board[2][3] == 'O')
  270.     {  
  271.         wins_2++;
  272.         return true;
  273.     }      
  274.  
  275.     else if (board[3][1] == 'O' && board[3][2] == 'O' && board[3][3] == 'O')
  276.     {  
  277.         wins_2++;
  278.         return true;
  279.     }      
  280.  
  281.     else if (board[1][1] == 'O' && board[2][1] == 'O' && board[3][1] == 'O')
  282.     {  
  283.         wins_2++;
  284.         return true;
  285.     }      
  286.  
  287.     else
  288.         return false;  
  289. }
  290.  
  291. bool tie_check(char board[][4], int size)
  292. {
  293.  
  294.     if (board[1][1] != '.' && board[1][2] != '.' && board[1][3] == '.'
  295.         && board[2][1] != '.' && board[2][2] != '.' && board[2][3] == '.'
  296.         && board[3][1] != '.' && board[3][2] != '.' && board[3][3] == '.')
  297.     {  
  298.         return true;
  299.     }  
  300.  
  301.     return false;
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement