Advertisement
legoaisu

Tic Tac Toe for Terminal

Aug 19th, 2015
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stdio.h>
  4.  
  5. void printBoard();
  6. void resetBoard();
  7. bool checkSpace(int);
  8. bool checkForWin(int);
  9. void setSpace(int, char);
  10.  
  11. using namespace std;
  12.  
  13. char spaces[9];
  14. bool in_progress = true;
  15. int turn = 0;
  16. char playerTurn = 'X';
  17.  
  18. int main ()
  19. {
  20.     // print out title
  21.     cout << endl << "\tT I C \n\tT A C \n\tT O E " << endl << "    ~For Terminal~" << endl << endl;
  22.  
  23.     resetBoard();
  24.  
  25.     // while game is in session
  26.     while (in_progress) {
  27.         int spaceNum;
  28.         bool noValidInput = true;
  29.         string temp = "";
  30.  
  31.         printBoard();
  32.         while (noValidInput) { // waits for player to select valid and empty space on board
  33.             cout << "Player (" << playerTurn << "), enter your choice space: [1-9]\n";
  34.             getline(cin, temp);
  35.             cout << endl;
  36.             spaceNum = stoi(temp);
  37.             if (spaceNum > 0 || spaceNum < 10) {
  38.                 if (checkSpace(spaceNum)) {
  39.                     setSpace(spaceNum, playerTurn);
  40.                     noValidInput = false;
  41.                 }
  42.             } else {
  43.                 cout << "Error: invalid input\n";
  44.             }
  45.         }
  46.         system("clear");
  47.  
  48.         if (checkForWin(turn)) {
  49.             cout << "Player (" << playerTurn << ") has won!\n";
  50.             printBoard();
  51.             in_progress = false;
  52.         }
  53.  
  54.         if (playerTurn == 'X') {
  55.             playerTurn = 'O';
  56.         } else if (playerTurn == 'O') {
  57.             playerTurn = 'X';
  58.         } else {
  59.             cout << "Fatal Error; Error Code: \"Dinosaur\"";
  60.             in_progress = false;
  61.         }
  62.         turn++;
  63.         if (turn >= 9 && in_progress) {
  64.             cout << "Tie!\n";
  65.             printBoard();
  66.             in_progress = false;
  67.         }
  68.     }
  69.  
  70.     return(0);
  71. }
  72.  
  73. bool checkForWin(int turn) {
  74.     // no way to win prior to turn 4
  75.     if (turn < 4) {
  76.         return false;
  77.     }
  78.  
  79.     // check rows
  80.     if ((spaces[0] != '_') && (spaces[0] == spaces[1] && spaces[0] == spaces[2])) {
  81.         return true;
  82.     } else if ((spaces[3] != '_') && (spaces[3] == spaces[4] && spaces[3] == spaces[5])) {
  83.         return true;
  84.     } else if ((spaces[6] != '_') && (spaces[6] == spaces[7] && spaces[6] == spaces[8])) {
  85.         return true;
  86.     }
  87.  
  88.     // check columns
  89.     if ((spaces[0] != '_') && (spaces[0] == spaces[3] && spaces[0] == spaces[6])) {
  90.         return true;
  91.     } else if ((spaces[1] != '_') && (spaces[1] == spaces[4] && spaces[1] == spaces[7])) {
  92.         return true;
  93.     } else if ((spaces[2] != '_') && (spaces[2] == spaces[5] && spaces[2] == spaces[8])) {
  94.         return true;
  95.     }
  96.  
  97.     // check diagonals
  98.     if ((spaces[0] != '_') && (spaces[0] == spaces[4] && spaces[0] == spaces[8])) {
  99.         return true;
  100.     } else if ((spaces[2] != '_') && (spaces[2] == spaces[4] && spaces[2] == spaces[6])) {
  101.         return true;
  102.     }
  103.  
  104.     return false;
  105. }
  106.  
  107. // checkSpace determines if space is already owned
  108. bool checkSpace(int spaceValue) {
  109.     if (spaces[spaceValue - 1] == '_') {
  110.         return true;
  111.     } else {
  112.         cout << "Space (" << spaceValue << ") has already been selected.\n";
  113.         return false;
  114.     }
  115. }
  116.  
  117. // setSpace sets the current player's character to spaces array
  118. void setSpace(int spaceValue, char playerChar) {
  119.     spaces[spaceValue - 1] = playerChar;
  120.     cout << "Space (" << spaceValue << ") has been set to: " << playerChar << endl;
  121. }
  122.  
  123. // resetBoard clears the array and sets turn count to 0
  124. void resetBoard() {
  125.     turn = 0;
  126.     playerTurn = 'X';
  127.     for (int i = 0; i < 9; i ++) {
  128.         spaces[i] = '_';
  129.     }
  130. }
  131.  
  132. // printBoard displays the spaces array as tic-tac-toe board for players
  133. void printBoard() {
  134.     cout << endl;
  135.     for (int i = 0; i < 9; i ++) {
  136.         if ((i + 3) % 3 == 0) {
  137.             cout << "\t";
  138.         }
  139.         cout << spaces[i] << " ";
  140.         if ((i + 1) % 3 == 0) {
  141.             cout << endl;
  142.         }
  143.     }
  144.     cout << endl;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement