vlatkovski

tic tac toe (iks nula) 1.4

Mar 12th, 2017
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3.  
  4. char Array[3][3] = {{' ', ' ', ' '},{' ', ' ', ' '},{' ', ' ', ' '}};
  5. int turnsTaken = 0;
  6.  
  7. void drawTable() {
  8.     printf("\n --- --- --- \n| %c | %c | %c |\n --- --- --- \n| %c | %c | %c |\n --- --- --- \n| %c | %c | %c |\n --- --- ---\n\n",
  9.            Array[0][0], Array[0][1], Array[0][2],
  10.            Array[1][0], Array[1][1], Array[1][2],
  11.            Array[2][0], Array[2][1], Array[2][2]);
  12. }
  13.  
  14. void clearTable() {
  15.     for (int a = 0; a < 3; a++) {
  16.         for (int b = 0; b < 3; b++) {
  17.             Array[a][b] = ' ';
  18.         }
  19.     }
  20. }
  21.  
  22. int hasCombination(char x) {
  23.     return
  24.        ((Array[0][0] == x && Array[0][1] == x && Array[0][2] == x) || // xxx ??? ???
  25.         (Array[1][0] == x && Array[1][1] == x && Array[1][2] == x) || // ??? xxx ???
  26.         (Array[2][0] == x && Array[2][1] == x && Array[2][2] == x) || // ??? ??? xxx
  27.         (Array[0][0] == x && Array[1][0] == x && Array[2][0] == x) || // x?? x?? x??
  28.         (Array[0][1] == x && Array[1][1] == x && Array[2][1] == x) || // ?x? ?x? ?x?
  29.         (Array[0][2] == x && Array[1][2] == x && Array[2][2] == x) || // ??x ??x ??x
  30.         (Array[0][0] == x && Array[1][1] == x && Array[2][2] == x) || // x?? ?x? ??x
  31.         (Array[2][2] == x && Array[1][1] == x && Array[0][0] == x));  // ??x ?x? x??
  32. }
  33.  
  34. int place(int a, int b, char p) {
  35.     if (Array[a][b] == ' ') {
  36.         Array[a][b] = p;
  37.         return 1;
  38.     }
  39.     return 0;
  40. }
  41.  
  42. int isDraw() {
  43.     //printf("turnsTaken = %i\n", turnsTaken);
  44.     return turnsTaken > 9;
  45. }
  46.  
  47. char oppositePlayer(char player) {
  48.     return player == 'x' ? 'o' : 'x';
  49. }
  50.  
  51. void askForNewGame(char);
  52.  
  53. void newTurn(char player) {
  54.     int a, b;
  55.  
  56.     printf("%c's turn to enter a row (1, 2, or 3) and column (1, 2, or 3)\n>", player);
  57.     std::cin >> a >> b;
  58.  
  59.     int successful = place(a - 1, b - 1, player);
  60.     if (!successful) {
  61.         printf("That space is already taken, try again.\n");
  62.         newTurn(player);
  63.     }
  64. }
  65.  
  66. int hasWinner() {
  67.     if (hasCombination('x')) {
  68.         return 'x';
  69.     } else if (hasCombination('o')) {
  70.         return 'o';
  71.     }
  72.     return ' ';
  73. }
  74.  
  75. void newCycle(char);
  76.  
  77. void newGame(char player = 'x') {
  78.     printf("\nStarting new game.\n");
  79.     turnsTaken = 0;
  80.     clearTable();
  81.     newCycle(player);
  82. }
  83.  
  84. void askForNewGame(char player) {
  85.     printf("New game? (y/n)\n>");
  86.     char option;
  87.     std::cin >> option;
  88.  
  89.     if (tolower(option) == 'y') {
  90.         newGame(oppositePlayer(player));
  91.     } else {
  92.         printf("\nGoodbye!");
  93.     }
  94. }
  95.  
  96. void newCycle(char player) {
  97.     drawTable();
  98.  
  99.     turnsTaken++;
  100.  
  101.     if (isDraw()) {
  102.         printf("Draw!\n");
  103.         askForNewGame(player);
  104.     }
  105.  
  106.     newTurn(player);
  107.  
  108.  
  109.     if (hasWinner() != ' ') {
  110.         printf("%c wins! Bravo!\n", player);
  111.         drawTable();
  112.         askForNewGame(player);
  113.     } else {
  114.         newCycle(oppositePlayer(player));
  115.     }
  116.  
  117. }
  118.  
  119. int main() {
  120.     newGame();
  121. }
Advertisement
Add Comment
Please, Sign In to add comment