vlatkovski

tic tac toe v1

Jul 18th, 2016
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. // Made 2 days after I started learning C++
  2.  
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6.  
  7.  
  8. int arr[3][3] = {{0,0,0},{0,0,0},{0,0,0}};
  9.  
  10. string najdiSimbolOdBroj(int x) {
  11.     string symbol;
  12.     switch (x) {
  13.         case 1:
  14.             symbol = "X";
  15.             break;
  16.         case 2:
  17.             symbol = "O";
  18.             break;
  19.         default:
  20.             symbol = "?";
  21.     }
  22.     return symbol;
  23. }
  24.  
  25. void printajTabla() {
  26.     cout << "-----------" << endl;
  27.     for (int a=0; a<3; a++) {
  28.         cout << "|";
  29.         for (int b=0; b<3; b++) {
  30.             string symbol = najdiSimbolOdBroj(arr[a][b]);
  31.             cout << " " << symbol << " ";
  32.         }
  33.         cout << "|\n";
  34.     }
  35.     cout << "-----------" << endl;
  36. }
  37.  
  38. int imaKombinacija(int x) {
  39.     return
  40.        ((arr[0][0] == x && arr[0][1] == x && arr[0][2] == x) || // xxx ??? ???
  41.         (arr[1][0] == x && arr[1][1] == x && arr[1][2] == x) || // ??? xxx ???
  42.         (arr[2][0] == x && arr[2][1] == x && arr[2][2] == x) || // ??? ??? xxx
  43.         (arr[0][0] == x && arr[1][0] == x && arr[2][0] == x) || // x?? x?? x??
  44.         (arr[0][1] == x && arr[1][1] == x && arr[2][1] == x) || // ?x? ?x? ?x?
  45.         (arr[0][2] == x && arr[1][2] == x && arr[2][2] == x) || // ??x ??x ??x
  46.         (arr[0][0] == x && arr[1][1] == x && arr[2][2] == x) || // x?? ?x? ??x
  47.         (arr[2][2] == x && arr[1][1] == x && arr[0][0] == x));  // ??x ?x? x??
  48. }
  49.  
  50. int imaPobednik() {
  51.     if (imaKombinacija(1)) {
  52.         return 1;
  53.     } else if (imaKombinacija(2)) {
  54.         return 2;
  55.     } else {
  56.         return 0;
  57.     }
  58. }
  59.  
  60. int smeni(int a, int b, int x) {
  61.     if (arr[a][b] == 0) {
  62.         arr[a][b] = x;
  63.         return 1;
  64.     } else {
  65.         return 0;
  66.     }
  67. }
  68.  
  69. void novRed(int x) {
  70.     int a;
  71.     int b;
  72.     string igrac = najdiSimbolOdBroj(x);
  73.     cout << igrac << "'s turn to enter a row (1, 2, or 3)\n> ";
  74.     cin >> a;
  75.     cout << igrac << "'s turn to enter a column (1, 2 or 3)\n> ";
  76.     cin >> b;
  77.     int uspesno = smeni(a-1, b-1, x);
  78.     if (!uspesno) {
  79.         cout << "That space is already taken, try again." << endl;
  80.         novRed(x);
  81.     }
  82. }
  83.  
  84. void novKrug(int x) {
  85.     printajTabla();
  86.     string igrac = najdiSimbolOdBroj(x);
  87.     novRed(x);
  88.     int pobednik = imaPobednik();
  89.     if (pobednik != 0) {
  90.         cout << igrac << " wins! Bravo!!!";
  91.     } else {
  92.         novKrug(x == 1 ? 2 : 1);
  93.     }
  94. }
  95.  
  96. int main() {
  97.     novKrug(1);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment