Advertisement
avr39-ripe

tickTacToeSimple

Mar 27th, 2020
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. const int fieldSize{ 3 };
  4.  
  5. char checkWin(char arr[fieldSize][fieldSize])
  6. {
  7.     char same{ arr[0][0] }; // compare symbol for horizontal and vertical
  8.     char sameD{ arr[0][0] }; // compare symbol for main and reverse diagonal
  9.     int cnt{ 0 }; // how much symbols was the same for horizontal and vertical
  10.     int cntD{ 0 }; // how much symbols was the same for main and reverse diagonal
  11.     int x{ 0 };
  12.     int y{ 0 };
  13.  
  14.     // check if three symbols are the same for vertical and main diagonal
  15.     for (x = 0; x < fieldSize; ++x)
  16.     {
  17.         for (y = 0, cnt = 0, same = arr[y][x]; y < fieldSize; ++y)
  18.         {
  19.             (arr[y][x] != same) ? cnt = 0 : ++cnt;
  20.  
  21.             if (x == y)
  22.             {
  23.                 (arr[y][x] != sameD) ? cntD = 0 : ++cntD;
  24.             }
  25.         }
  26.         if (cnt == fieldSize and same != ' ') { return same; };
  27.     }
  28.     if (cntD == fieldSize and sameD != ' ') { return sameD; };
  29.  
  30.     // check if three symbols are the same for horizontal and reverse diagonal
  31.     sameD = arr[0][fieldSize-1];
  32.     cntD = 0;
  33.     for (y = 0; y < fieldSize; ++y)
  34.     {
  35.         for (x = 0, cnt = 0, same = arr[y][x]; x < fieldSize; ++x)
  36.         {
  37.             (arr[y][x] != same) ? cnt = 0 : ++cnt;
  38.  
  39.             if (x + y == fieldSize-1)
  40.             {
  41.                 (arr[y][x] != sameD) ? cntD = 0 : ++cntD;
  42.             }
  43.         }
  44.         if (cnt == fieldSize and same != ' ') { return same; };
  45.     }
  46.     if (cntD == fieldSize and sameD != ' ') { return sameD; };
  47.     return 'z';
  48. }
  49.  
  50. void printBoard(char arr[fieldSize][fieldSize])
  51. {
  52.     char symbol{ ' ' };
  53.     int numY{ 0 };
  54.     int numX{ 0 };
  55.     int elY{ 0 };
  56.     int elX{ 0 };
  57.     for (int y{ 0 }; y <= (fieldSize + fieldSize - 1); ++y)
  58.     {
  59.         for (int x{ 0 }; x <= (fieldSize * 3 + fieldSize - 1); ++x)
  60.         {
  61.             symbol = ' ';
  62.             if (y == 0 and (x % 2 == 0) and (x % 4)) { symbol = '1' + numX++; };
  63.             if (x == 0 and y % 2) { symbol = '1' + numY++; };
  64.             if (x and y)
  65.             {
  66.                 if (x % 4 == 0) { symbol = '|'; };
  67.                 if (y % 2 == 0) { symbol = ((x % 4 == 0) ? '+' : '-'); };
  68.                 if (x % 2 == 0 and (x % 4 and y % 2)) { symbol = arr[elY][elX++]; };
  69.             }
  70.             std::cout << symbol;
  71.         }
  72.         std::cout << '\n';
  73.         elX = 0;
  74.         if (y and y % 2 == 0) { ++elY; };
  75.     }
  76. }
  77.  
  78. void makeMove(char arr[fieldSize][fieldSize], char who)
  79. {
  80.     int y{ 0 };
  81.     int x{ 0 };
  82.    
  83.     while (true)
  84.     {
  85.         std::cout << "Put " << who << " to X Y\n";
  86.         std::cin >> x >> y;
  87.         x--;
  88.         y--;
  89.         if ((x < 0 and x > fieldSize-1) or (y < 0 or y > fieldSize-1))
  90.         {
  91.             std::cout << "Out of game field! Try again!\n";
  92.             continue;
  93.         }
  94.         if (arr[y][x] == ' ') { arr[y][x] = who; break; }
  95.         else { std::cout << "Occuped! Choose another place!\n"; };
  96.     }
  97. }
  98.  
  99. void initBoard(char arr[fieldSize][fieldSize])
  100. {
  101.     for (int y{ 0 }; y < fieldSize; ++y)
  102.     {
  103.         for (int x{ 0 }; x < fieldSize; ++x)
  104.         {
  105.             arr[y][x] = ' ';
  106.         }
  107.     }
  108. }
  109.  
  110. int main()
  111. {
  112.     char field[fieldSize][fieldSize];
  113.     char winner{'z'};
  114.     int moves{ 0 };
  115.     char who{ 'o' };
  116.     initBoard(field);
  117.     printBoard(field);
  118.  
  119.     for (int moves{0}; winner == 'z' and  moves < fieldSize * fieldSize; ++moves)
  120.     {
  121.         who = (who == 'o') ? 'x' : 'o';
  122.         makeMove(field, who);
  123.         printBoard(field);
  124.         winner = checkWin(field);
  125.     }
  126.     if (winner == 'z')
  127.     {
  128.         std::cout << "Nobody wins! HaHa!\n";
  129.     }
  130.     else
  131.     {
  132.         std::cout << "The winner is " << winner << " congratulation!!!\n";
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement