vlatkovski

tic tac toe v1.1

Jul 18th, 2016
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5.  
  6. int Array[3][3] = {{0,0,0},{0,0,0},{0,0,0}};
  7.  
  8. string findSymbolFromNum(int x) {
  9.     switch (x) {
  10.         case 1: return "X"; break;
  11.         case 2: return "O"; break;
  12.         default: return "~";
  13.     }
  14. }
  15.  
  16. void drawTable() {
  17.     cout << "\n-----------\n";
  18.     for (int a=0; a<3; a++) {
  19.         cout << "|";
  20.         for (int b=0; b<3; b++) {
  21.             cout << " " << findSymbolFromNum(Array[a][b]) << " ";
  22.         }
  23.         cout << "|\n";
  24.     }
  25.     cout << "-----------\n\n";
  26. }
  27.  
  28. void clearTable() {
  29.     for (int a=0; a<3; a++) {
  30.         for (int b=0; b<3; b++) {
  31.             Array[a][b] = 0;
  32.         }
  33.     }
  34. }
  35.  
  36. int hasCombination(int x) {
  37.     return
  38.        ((Array[0][0] == x && Array[0][1] == x && Array[0][2] == x) || // xxx ??? ???
  39.         (Array[1][0] == x && Array[1][1] == x && Array[1][2] == x) || // ??? xxx ???
  40.         (Array[2][0] == x && Array[2][1] == x && Array[2][2] == x) || // ??? ??? xxx
  41.         (Array[0][0] == x && Array[1][0] == x && Array[2][0] == x) || // x?? x?? x??
  42.         (Array[0][1] == x && Array[1][1] == x && Array[2][1] == x) || // ?x? ?x? ?x?
  43.         (Array[0][2] == x && Array[1][2] == x && Array[2][2] == x) || // ??x ??x ??x
  44.         (Array[0][0] == x && Array[1][1] == x && Array[2][2] == x) || // x?? ?x? ??x
  45.         (Array[2][2] == x && Array[1][1] == x && Array[0][0] == x));  // ??x ?x? x??
  46. }
  47.  
  48. int place(int a, int b, int x) {
  49.     if (Array[a][b] == 0) {
  50.         Array[a][b] = x;
  51.         return 1;
  52.     } else {
  53.         return 0;
  54.     }
  55. }
  56.  
  57. void newTurn(int x) {
  58.     int a;
  59.     int b;
  60.     string player = findSymbolFromNum(x);
  61.     cout << player << "'s turn to enter a row (1, 2, or 3)\n> ";
  62.     cin >> a;
  63.     cout << player << "'s turn to enter a column (1, 2 or 3)\n> ";
  64.     cin >> b;
  65.     int successful = place(a-1, b-1, x);
  66.     if (!successful) {
  67.         cout << "That space is already taken, try again.\n";
  68.         newTurn(x);
  69.     }
  70. }
  71.  
  72. int hasWinner() {
  73.     if (hasCombination(1)) {
  74.         return 1;
  75.     } else if (hasCombination(2)) {
  76.         return 2;
  77.     } else {
  78.         return 0;
  79.     }
  80. }
  81.  
  82. void newCycle(int x);
  83.  
  84. void newGame(int x=1) {
  85.     int z;
  86.     cout << "Type in a number and press enter for new game.\n";
  87.     cin >> z;
  88.     cout << "\nStarting new game.\n";
  89.     clearTable();
  90.     newCycle(x);
  91. }
  92.  
  93. void newCycle(int x) {
  94.     drawTable();
  95.     string player = findSymbolFromNum(x);
  96.     newTurn(x);
  97.     int winner = hasWinner();
  98.     if (winner != 0) {
  99.         cout << "\n" << player << " wins! Bravo!\n";
  100.         drawTable();
  101.         newGame(x == 1 ? 2 : 1);
  102.     } else {
  103.         newCycle(x == 1 ? 2 : 1);
  104.     }
  105. }
  106.  
  107. int main() {
  108.     newGame();
  109. }
Advertisement
Add Comment
Please, Sign In to add comment