Advertisement
vlatkovski

tic tac toe v1.3

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