SHARE
TWEET

Lesson 11 activity 5

Porr011 Apr 24th, 2019 (edited) 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <conio.h>
  3. using namespace std;
  4.  
  5. // constant varible for 3 x 3 size of the tic tact toe board
  6. const int SIZE = 3;
  7.  
  8. //*********************
  9. // Function for printing the board
  10. // Arguments have the array and the array size of the board and the size = 3 x 3
  11. //*********************
  12. void printBoard(char arr[][SIZE])
  13. {
  14.     // nested for loop for the printing of the board
  15.     for (int i = 0; i < SIZE; i++){
  16.         for (int j = 0; j < SIZE; j++)
  17.         {
  18.             cout << arr[i][j] << '*';
  19.         }
  20.         cout << endl;
  21.     }
  22. }
  23.  
  24. //***************************************************
  25. // Finding the winner function
  26. // using a boolean data type in this function to find the winner
  27. // arguments have the
  28. //***************************************************
  29. bool isWin(char arr[][SIZE], char ch)
  30. {
  31.     int count;
  32.     // nested for loop for [i] and [j] winner X
  33.     for (int i = 0; i < SIZE; i++)
  34.     {
  35.         count = 0;
  36.         for (int j = 0; j < SIZE; j++)
  37.         {
  38.             if (arr[i][j] == ch)
  39.                 count++;
  40.         }
  41.         if (count == SIZE)
  42.             return true;
  43.     }
  44.    
  45.     // nested for loop for [j] and [i] winner O
  46.     for (int i = 0; i < SIZE; i++)
  47.     {
  48.         count = 0;
  49.         for (int j = 0; j < SIZE; j++)
  50.         {
  51.             if (arr[j][i] == ch)
  52.                 count++;
  53.         }
  54.         if (count == SIZE)
  55.             return true;
  56.     }
  57.    
  58.     // nested for loop for determining if the user input is already taken  
  59.     count = 0;
  60.     for (int i = 0; i < SIZE; i++)
  61.     {
  62.         if (arr[i][i] == ch)
  63.             count++;
  64.     }
  65.    
  66.     if (count == SIZE)
  67.         return true;
  68.     count = 0;
  69.    
  70.     // j-- is an important part to determine if the users input is already taken
  71.     for (int i = 0, j = SIZE - 1; i < SIZE; i++, j--)
  72.     {
  73.         if (arr[i][j] == ch)
  74.             count++;
  75.     }
  76.     // return statement if the user inputs an already taken spot
  77.     if (count == SIZE)
  78.         return true;
  79.     return false;
  80. }
  81.  
  82. //**************************
  83. // Determining if the game is a tie function with bool
  84. // arguments char array and the array size
  85. //**************************
  86. bool gameDraw(char arr[][SIZE])
  87. {
  88.     for (int i = 0; i < SIZE; i++)
  89.     {
  90.         for (int j = 0; j < SIZE; j++)
  91.         {
  92.             if (arr[i][j] == '-')
  93.                 return false; // returning if its a tie
  94.         }
  95.     }
  96.         return true;
  97. }
  98.  
  99. //*****************
  100. // Main function
  101. // creats the board
  102. // figuring out whos turn it is
  103. // Finding the winner or if its a tie
  104. //****************
  105. int main()
  106. {
  107.     cout << "Tic Tac Toe Game" << endl;
  108.     char board[SIZE][SIZE];
  109.     for (int i = 0; i < SIZE; i++){
  110.         for (int j = 0; j < SIZE; j++){
  111.             board[i][j] = '-';
  112.         }
  113.     }
  114.    
  115.     // varibles for finding if the game has ended
  116.     bool gameEnd = false;
  117.     int row, col, playerTurn = 1;
  118.     char player;
  119.    
  120.     // Boolean nested while if loop for ask user for choices
  121.     // also saves these choices
  122.     while (!gameEnd)
  123.     {
  124.         //if statements for determining whos turn it is
  125.         printBoard(board);
  126.         if (playerTurn == 1)
  127.         {
  128.             player = 'X';
  129.             playerTurn = 2;
  130.         }
  131.         else if (playerTurn == 2)
  132.         {
  133.             player = 'O';
  134.             playerTurn = 1;
  135.         }
  136.         do
  137.         {
  138.             //getting the user inputs
  139.             cout << "Player " << player << " Turn" << endl;
  140.             cout << "Enter a row: "; cin >> row;
  141.             cout << "Enter a colums: "; cin >> col;
  142.             row--;
  143.             col--;
  144.         }
  145.         // then has a while statement that enetrs the inputs into the board
  146.         while (!(row >= 0 && row < SIZE && col >= 0 && col < SIZE && board[row][col] == '-'));
  147.         board[row][col] = player;
  148.         // using the cins and putting them in the right row and colums
  149.         if (isWin(board, player))
  150.         {
  151.             // gameEnd
  152.             printBoard(board);
  153.             cout << "Player " << player << " wins" << endl;
  154.             gameEnd = true;
  155.         }
  156.         // else if loop for tie game
  157.         else if (gameDraw(board))
  158.         {
  159.             printBoard(board);
  160.             cout << "Game is a tie" << endl;
  161.             gameEnd = true;
  162.         }
  163.     }
  164.     return 0;
  165. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top