Advertisement
horror04

krestiki/noliki

Nov 23rd, 2021
1,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. using namespace std;
  5. char h[9] = { '-', '-', '-', '-', '-', '-', '-', '-', '-' };
  6.  
  7. void board_numbers()
  8. {
  9.     cout << "***********" << endl;
  10.     cout << "-7-|-8-|-9-" << endl;
  11.     cout << "-4-|-5-|-6-" << endl;
  12.     cout << "-1-|-2-|-3-" << endl;
  13.     cout << "***********" << endl;
  14. }
  15.  
  16. void board()
  17. {
  18.     cout << "\n";
  19.     cout << "***********" << endl;
  20.     cout << "-" << h[6] << "-|-" << h[7] << "-|-" << h[8] << "-" << endl;
  21.     cout << "-" << h[3] << "-|-" << h[4] << "-|-" << h[5] << "-" << endl;
  22.     cout << "-" << h[0] << "-|-" << h[1] << "-|-" << h[2] << "-" << endl;
  23.     cout << "***********" << endl;
  24. }
  25.  
  26. int get_move()
  27. {
  28.     int move;
  29.     cin >> move;
  30.     while (move > 9 || move < 1 || h[move - 1] != '-')
  31.     {
  32.         cout << "ERROR!!!!" << endl;
  33.         cin >> move;
  34.     }
  35.     return move;
  36. }
  37.  
  38. int endgame_check(char h[])
  39. {
  40.  
  41.     if ((h[0] == 'X') && (h[1] == 'X') && (h[2] == 'X'))
  42.     {
  43.         cout << "Победа! X" << endl;
  44.         exit (0);
  45.     }
  46.     if ((h[3] == 'X') && (h[4] == 'X') && (h[5] == 'X'))
  47.     {
  48.         cout << "Победа! X" << endl;
  49.         exit(0);
  50.     }
  51.     if ((h[6] == 'X') && (h[7] == 'X') && (h[8] == 'X'))
  52.     {
  53.         cout << "Победа! X" << endl;
  54.         exit(0);
  55.     }
  56.     if ((h[0] == 'X') && (h[3] == 'X') && (h[6] == 'X'))
  57.     {
  58.         cout << "Победа! X" << endl;
  59.         exit(0);
  60.     }
  61.     if ((h[1] == 'X') && (h[4] == 'X') && (h[7] == 'X'))
  62.     {
  63.         cout << "Победа! X" << endl;
  64.         exit(0);
  65.     }
  66.     if ((h[2] == 'X') && (h[5] == 'X') && (h[8] == 'X'))
  67.     {
  68.         cout << "Победа! X" << endl;
  69.         exit(0);
  70.     }
  71.     if ((h[0] == 'X') && (h[4] == 'X') && (h[8] == 'X'))
  72.     {
  73.         cout << "Победа! X" << endl;
  74.         exit(0);
  75.     }
  76.     if ((h[2] == 'X') && (h[4] == 'X') && (h[6] == 'X'))
  77.     {
  78.         cout << "Победа! X" << endl;
  79.         exit(0);
  80.     }
  81.  
  82.     if ((h[0] == 'O') && (h[1] == 'O') && (h[2] == 'O'))
  83.     {
  84.         cout << "Победа! O" << endl;
  85.         exit(0);
  86.     }
  87.     if ((h[3] == 'O') && (h[4] == 'O') && (h[5] == 'O'))
  88.     {
  89.         cout << "Победа! O" << endl;
  90.         exit(0);
  91.     }
  92.     if ((h[6] == 'O') && (h[7] == 'O') && (h[8] == 'O'))
  93.     {
  94.         cout << "Победа! O" << endl;
  95.         exit(0);
  96.     }
  97.     if ((h[0] == 'O') && (h[3] == 'O') && (h[6] == 'O'))
  98.     {
  99.         cout << "Победа! O" << endl;
  100.         exit(0);
  101.     }
  102.     if ((h[1] == 'O') && (h[4] == 'O') && (h[7] == 'O'))
  103.     {
  104.         cout << "Победа! O" << endl;
  105.         exit(0);
  106.     }
  107.     if ((h[2] == 'O') && (h[5] == 'O') && (h[8] == 'O'))
  108.     {
  109.         cout << "Победа! O" << endl;
  110.         exit(0);
  111.     }
  112.     if ((h[0] == 'O') && (h[4] == 'O') && (h[8] == 'O'))
  113.     {
  114.         cout << "Победа! O" << endl;
  115.         exit(0);
  116.     }
  117.     if ((h[2] == 'O') && (h[4] == 'O') && (h[6] == 'O'))
  118.     {
  119.         cout << "Победа! O" << endl;
  120.         exit(0);
  121.     }
  122. }
  123.  
  124. int main()
  125. {
  126.     srand(time(NULL));
  127.     setlocale(LC_ALL,"Russian");
  128.     int choice;
  129.     cout << "Кем будете играть? 1 - крестики, 2 - нолики" << endl;
  130.     cin >> choice;
  131.    
  132.     for (int i = 0; i < 10; i++)
  133.     {
  134.         board_numbers();
  135.  
  136.         board();
  137.  
  138.         if (i == 9)
  139.         {
  140.             cout << "Ничья";
  141.             return 0;
  142.         }
  143.  
  144.         int move = get_move();
  145.  
  146.         cout << "Hod: " << i << endl;
  147.  
  148.         if (choice == 1)
  149.         {
  150.             if (i % 2 == 0)
  151.             {
  152.                 h[move - 1] = 'X';
  153.             }
  154.             else h[move - 1] = 'O';
  155.         }
  156.  
  157.         if (choice == 2)
  158.         {
  159.             if (i % 2 == 0)
  160.             {
  161.                 h[move - 1] = 'O';
  162.             }
  163.             else h[move - 1] = 'X';
  164.         }
  165.  
  166.         endgame_check(h);
  167.  
  168.     }
  169.  
  170.     return 0;
  171. }
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement