Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.51 KB | None | 0 0
  1. //Paul Fynn Kuemmel
  2. //This plays tic tac toe, between 2 users.
  3. #include <stdio.h>
  4. #include <iostream>
  5. #include <string>
  6. using namespace std;
  7.  
  8. /**
  9.  *@param The game board that the game is played on
  10.  * @return Nothing
  11.  */
  12. void printBoard(char gameBoard[3][3] ){
  13.     for (int y = 0; y < 3; y++){ //For each value of the row and column, the current value is printed
  14.         for (int x = 0; x < 3; x++){
  15.             cout << gameBoard [y] [x]; //This creates the board
  16.         }
  17.         cout << endl; //Seperates for the next line.
  18.     }
  19. }
  20. /**
  21.  * @param The Game board that the game is played on
  22.  * @return The boolean variable of the victory.
  23.  */
  24. bool winnerDetermination(char gameBoard[3][3]){
  25.     bool victoryRoyale = false; //Will determine if won or not
  26.     string checker = ""; //String to plug characters into.
  27.     int r = 0;
  28.     int c = 0;
  29.     string diagonal1 = ""; //Hardcoding the 2 diagonals.
  30.     string diagonal2 = "";
  31.     diagonal1 += gameBoard[0] [0];
  32.     diagonal1 += gameBoard[1] [1];
  33.     diagonal1 += gameBoard[2] [2];
  34.    
  35.     diagonal2 += gameBoard[0] [2];
  36.     diagonal2 += gameBoard[1] [1]; //This creates the diagonals with each location being specific for each diagonal
  37.     diagonal2 += gameBoard[2] [0];
  38.     for(r = 0; r < 3; r++){ //For each row of the board this will run
  39.        
  40.         checker += gameBoard[r] [c];
  41.         checker += gameBoard[r] [c + 1]; //Checks throughout each column.
  42.         checker += gameBoard[r] [c + 2];
  43.         if(checker == "XXX" or checker == "OOO"){ //If there are 3 of a kind, then victory is true
  44.             victoryRoyale = true;
  45.             break;
  46.         }
  47.         checker = "";
  48.     }
  49.     r = 0;
  50.     for(c = 0; c < 3; c++){
  51.         checker += gameBoard[r] [c];
  52.         checker += gameBoard[r + 1] [c]; //For each column of the board, this will run
  53.         checker += gameBoard[r + 2] [c];
  54.        
  55.         if(checker == "XXX" or checker == "OOO"){ //If there are 3 of a kind, then victory is true
  56.           victoryRoyale = true;
  57.           break;
  58.         }
  59.        
  60.         checker = "";
  61.     }
  62.     c = 0;
  63.     if(diagonal1 == "XXX" or diagonal1 == "OOO"){
  64.         victoryRoyale = true;
  65.     } //If the diagonals are 3 of a kind, then victory is true.
  66.     if(diagonal2 == "XXX" or diagonal2 == "OOO"){
  67.         victoryRoyale = true;
  68.     }
  69.     return victoryRoyale;
  70. }
  71. /***
  72.  * @param The Victory determination
  73.  * @return 0 to end cold heartedly and efficiently.
  74.  */
  75. int main()
  76. {
  77.     bool victoryRoyale = false;
  78.     int r = 0; //Creates the rows and columns
  79.     int c = 0;
  80.     int playerTurn = 1; //Starts this off to determine the turn
  81.     char gameBoard [3] [3]; //Creates the board
  82.     bool endGame(false);
  83.     for (int r = 0; r < 3; r++){
  84.             for (int c = 0; c < 3; c++){
  85.                gameBoard [c] [r] = '*';  //Places symbol as placeholders
  86.             }
  87.         }
  88.     while (1){
  89.         printBoard(gameBoard);
  90.         if (playerTurn % 2 != 0){ //If it is odd then this runs
  91.             cout << "Please enter the row and column of the location you want to place your X.";
  92.             cin >> r >> c;
  93.             while(gameBoard [r] [c] != '*'){ //Error validation
  94.                 cout << "There is already a character there you cheeky sod. Different row and column please.";
  95.                 cin >> r >> c;
  96.             }
  97.             gameBoard [r] [c] = 'X'; //Places character
  98.             playerTurn ++;
  99.             victoryRoyale = winnerDetermination(gameBoard); //Determines if there is a victory
  100.         }
  101.         else{
  102.             cout << "Please enter the row and column of the location you want to place your O.";
  103.             cin  >> r >> c;
  104.             while(gameBoard [r] [c] != '*'){ //Error validation
  105.                 cout << "There is already a character there you cheeky sod. Different row and column please.";
  106.                 cin >> r >> c;
  107.             }
  108.             gameBoard [r] [c] = 'O'; //Places character
  109.             playerTurn ++;
  110.             victoryRoyale = winnerDetermination(gameBoard);
  111.         }
  112.         r = 0; //Refreshes the addresses.
  113.         c = 0;
  114.     if(victoryRoyale){ //If victory is true, then this comes out
  115.             cout << "Congrats! You won the Game!";
  116.             cout << endl;
  117.             printBoard(gameBoard);
  118.             break;
  119.     }
  120.     else if (playerTurn == 10){
  121.         cout << "Well done to both of you for the Tie!";
  122.         cout << endl;
  123.         printBoard(gameBoard);
  124.         break; //If not, then they tied.
  125.     }
  126. }
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement