Guest User

TicTacToeGame.cpp

a guest
Jun 6th, 2014
5,984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.76 KB | None | 0 0
  1. #include "TicTacToeGame.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. //Constructor, ours does nothing :P
  7. TicTacToeGame::TicTacToeGame()
  8. {
  9. }
  10.  
  11. //The game!
  12. void TicTacToeGame::playGame()
  13. {
  14.     clearBoard();
  15.  
  16.     char player1 = 'X';
  17.     char player2 = 'O';
  18.  
  19.     char currentPlayer = player1;
  20.     bool isDone = false;
  21.  
  22.     int x, y;
  23.  
  24.     int turn = 0;
  25.  
  26.     //Inner game loop
  27.     while (isDone == false) {
  28.  
  29.         //Print out the board each time we loop
  30.         printBoard();
  31.  
  32.         //Get the coordinates of where the user wants to place a marker
  33.         x = getXCoord();
  34.         y = getYCoord();
  35.  
  36.         //Try to place a marker
  37.         if (placeMarker(x, y, currentPlayer) == false) {
  38.             //If we failed to place a marker, tell him he failed!
  39.             cout << "That spot is occupied!\n";
  40.         } else {
  41.             //Otherwise, we successfully did this turn!
  42.             turn++;
  43.             //See if the player won!
  44.             if (checkForVictory(currentPlayer) == true) {
  45.                 //He won!
  46.                 cout << "The game is over! Player " << currentPlayer << " has won!\n";
  47.                 isDone = true;
  48.             } else if (turn == 9) {
  49.                 //Tie game!
  50.                 cout << "Its a tie game!\n";
  51.                 isDone = true;
  52.             }
  53.  
  54.             // Switch players
  55.             if (currentPlayer == player1) {
  56.                 currentPlayer = player2;
  57.             } else {
  58.                 currentPlayer = player1;
  59.             }
  60.         }
  61.     }
  62. }
  63.  
  64.  
  65.  
  66. int TicTacToeGame::getXCoord()
  67. {
  68.     bool isInputBad = true;
  69.  
  70.     int x;
  71.  
  72.     while (isInputBad == true) {
  73.         cout << "Enter the X coordinate: ";
  74.         cin >> x;
  75.  
  76.         if (x < 1 || x > 3) {
  77.             cout << "Invalid Coordinate!\n";
  78.         } else {
  79.             isInputBad = false;
  80.         }
  81.     }
  82.     return x - 1;
  83. }
  84.  
  85. int TicTacToeGame::getYCoord()
  86. {
  87.     bool isInputBad = true;
  88.  
  89.     int y;
  90.  
  91.     while (isInputBad == true) {
  92.         cout << "Enter the Y coordinate: ";
  93.         cin >> y;
  94.  
  95.         if (y < 1 || y > 3) {
  96.             cout << "Invalid Coordinate!\n";
  97.         } else {
  98.             isInputBad = false;
  99.         }
  100.     }
  101.     return y - 1;
  102. }
  103.  
  104. bool TicTacToeGame::placeMarker(int x, int y, char currentPlayer)
  105. {
  106.     if (board[y][x] != ' ') {
  107.         return false;
  108.     }
  109.  
  110.     board[y][x] = currentPlayer;
  111.     return true;
  112. }
  113.  
  114. bool TicTacToeGame::checkForVictory(char currentPlayer)
  115. {
  116.     //Check the rows
  117.     for (int i = 0; i < 3; i++) {
  118.         if ((board[i][0] == currentPlayer) && (board[i][0] == board[i][1]) && (board[i][1] == board[i][2])) {
  119.             return true; //We won!
  120.         }
  121.     }
  122.  
  123.     //Check the columns
  124.     for (int i = 0; i < 3; i++) {
  125.         if ((board[0][i] == currentPlayer) && (board[0][i] == board[1][i]) && (board[1][i] == board[2][i])) {
  126.             return true; //We won!
  127.         }
  128.     }
  129.  
  130.     //Check top left diagonal
  131.     if ((board[0][0] == currentPlayer) && (board[0][0] == board[1][1]) && (board[1][1] == board[2][2])) {
  132.         return true; //We won!
  133.     }
  134.  
  135.     //Check top right diagonal
  136.     if ((board[2][0] == currentPlayer) && (board[2][0] == board[1][1]) && (board[1][1] == board[0][2])) {
  137.         return true; //We won!
  138.     }
  139.  
  140.     return false;
  141. }
  142.  
  143. void TicTacToeGame::clearBoard()
  144. {
  145.     //Empties the board
  146.     for (int i = 0; i < 3; i++) {
  147.         for (int j = 0; j < 3; j++) {
  148.             board[i][j] = ' ';
  149.         }
  150.     }
  151. }
  152.  
  153. void TicTacToeGame::printBoard()
  154. {
  155.     //ugly formatting yay
  156.     cout << endl;
  157.     cout << " |1 2 3|\n";
  158.     for (int i = 0; i < 3; i++) {
  159.         cout << " -------\n";
  160.         cout << i+1 << "|" << board[i][0] << "|" << board[i][1] << "|" << board[i][2] << "|\n";
  161.     }
  162.     cout << " -------\n";
  163. }
Advertisement
Add Comment
Please, Sign In to add comment