Spicke

КрестикиНолики

Apr 14th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <windows.h>
  4.  
  5. using namespace std;
  6.  
  7. bool gameOver;
  8.  
  9. class Game {
  10. private:
  11.     static const int _x = 19;
  12.     static const int _y = 19;
  13.     static const int _c = 5;
  14.     int posX;
  15.     int posY;
  16.     int r1, r2;
  17.     char str[_x][_y];
  18.     int pl;
  19.     int mas[3][3];
  20.     int p = 0;
  21. public:
  22.     Game() {
  23.         posX = 1;
  24.         posY = 1;
  25.         r1 = posX;
  26.         r2 = posY;
  27.         pl = 1;
  28.         for (int i = 0; i < 3; i++) {
  29.             for (int j = 0; j < 3; j++) {
  30.                 mas[i][j] = 0;
  31.             }
  32.         }
  33.         for (int i = 0; i < _x; i++) {
  34.             for (int j = 0; j < _y; j++) {
  35.                 if (j % 6 == 0)
  36.                     str[i][j] = '#';
  37.                 else if (i % 6 == 0)
  38.                     str[i][j] = '#';
  39.                 else
  40.                     str[i][j] = ' ';
  41.             }
  42.         }
  43.     }
  44.     void clear() { system("cls"); }
  45.     void Draw();
  46.     void Cursor();
  47.     void update();
  48.     void DrawPl1();
  49.     void DrawPl2();
  50.     void Proverka();
  51.     void Result(int k);
  52. };
  53.  
  54. void Game::Draw() {
  55.     for (int i = 0; i < _x; i++) {
  56.         for (int j = 0; j < _y; j++) {
  57.             cout << str[i][j];
  58.         }
  59.         cout << endl;
  60.     }
  61. }
  62. void Game::Cursor() {
  63.     for (int i = 0; i < _x; i++) {
  64.         for (int j = 0; j < _y; j++) {
  65.             if (6 * r1 + 1 == j && 6 * r2 + 1 == i) {
  66.                 for (int t1 = i; t1 < i + _c; t1++) {
  67.                     for (int t2 = j; t2 < j + _c; t2++) {
  68.                         if (t1 == i)
  69.                             str[t1][t2] = ' ';
  70.                         else if (t1 == i + _c - 1)
  71.                             str[t1][t2] = ' ';
  72.                         if (t2 == i)
  73.                             str[t1][t2] = ' ';
  74.                         else if (t2 == j + _c - 1)
  75.                             str[t1][t2] = ' ';
  76.  
  77.                     }
  78.                 }
  79.             }
  80.         }
  81.     }
  82.     for (int i = 0; i < _x; i++) {
  83.         for (int j = 0; j < _y; j++) {
  84.             if (6 * posX + 1 == j && 6 * posY + 1 == i) {
  85.                 r1 = posX;
  86.                 r2 = posY;
  87.                 for (int t1 = i; t1 < i + _c; t1++) {
  88.                     for (int t2 = j; t2 < j + _c; t2++) {
  89.                         if (t1 == i)
  90.                             str[t1][t2] = 'A';
  91.                         else if (t1 == i + _c - 1)
  92.                             str[t1][t2] = 'A';
  93.                         if (t2 == i)
  94.                             str[t1][t2] = 'A';
  95.                         else if (t2 == j + _c - 1)
  96.                             str[t1][t2] = 'A';
  97.  
  98.                     }
  99.                 }
  100.             }
  101.         }
  102.     }
  103.     if (pl > 0)
  104.         cout << endl << "Ходит игрок №1" << endl;
  105.     else cout << endl << "Ходит игрок №2" << endl;
  106. }
  107. void Game::update() {
  108.     if (_kbhit()) {
  109.         switch (_getch()) {
  110.         case 'a':
  111.         case 'A':
  112.         case 75:
  113.             posX--;
  114.             break;
  115.         case 's':
  116.         case 'S':
  117.         case 80:
  118.             posY++;
  119.             break;
  120.         case 'w':
  121.         case 'W':
  122.         case 72:
  123.             posY--;
  124.             break;
  125.         case 'd':
  126.         case 'D':
  127.         case 77:
  128.             posX++;
  129.             break;
  130.         case 'f':
  131.         case 'F':
  132.         case VK_SPACE:
  133.             if (pl > 0) {
  134.                 if (mas[posX][posY] == 0) {
  135.                     DrawPl1();
  136.                     mas[posX][posY] = pl;
  137.                     pl = -pl;
  138.  
  139.                 }
  140.                 else {
  141.                     cout << endl << "Ошибка, клетка занята";
  142.                 }
  143.             }
  144.             else {
  145.                 if (mas[posX][posY] == 0) {
  146.                     DrawPl2();
  147.                     mas[posX][posY] = pl;
  148.                     pl = -pl;
  149.  
  150.                 }
  151.                 else {
  152.                     cout << endl << "Ошибка, клетка занята";
  153.                 }
  154.             }
  155.             Proverka();
  156.             break;
  157.         }
  158.     }
  159.     if (posX < 0)
  160.         posX = 0;
  161.     else if (posX > 2)
  162.         posX = 2;
  163.     else if (posY < 0)
  164.         posY = 0;
  165.     else if (posY > 2)
  166.         posY = 2;
  167. }
  168. void Game::DrawPl1() {
  169.     for (int i = 0; i < _x; i++) {
  170.         for (int j = 0; j < _y; j++) {
  171.             if (6 * posX + 2 == j && 6 * posY + 2 == i) {
  172.                 str[i][j] = 'x';
  173.                 str[i + 1][j + 1] = 'x';
  174.                 str[i + 2][j + 2] = 'x';
  175.                 str[i + 2][j] = 'x';
  176.                 str[i][j + 2] = 'x';
  177.             }
  178.         }
  179.     }
  180. }
  181. void Game::DrawPl2() {
  182.     for (int i = 0; i < _x; i++) {
  183.         for (int j = 0; j < _y; j++) {
  184.             if (6 * posX + 2 == j && 6 * posY + 2 == i) {
  185.                 str[i + 1][j] = '0';
  186.                 str[i][j + 1] = '0';
  187.                 str[i + 2][j + 1] = '0';
  188.                 str[i + 1][j + 2] = '0';
  189.             }
  190.         }
  191.     }
  192. }
  193. void Game::Proverka() {
  194.     if ( mas[0][0] == 1 && mas[0][1] == 1 && mas[0][2] == 1 ||
  195.          mas[1][0] == 1 && mas[1][1] == 1 && mas[1][2] == 1 ||
  196.          mas[2][0] == 1 && mas[2][1] == 1 && mas[2][2] == 1 ||
  197.          mas[0][0] == 1 && mas[1][0] == 1 && mas[2][0] == 1 ||
  198.          mas[0][1] == 1 && mas[1][1] == 1 && mas[2][1] == 1 ||
  199.          mas[0][2] == 1 && mas[1][2] == 1 && mas[2][2] == 1 ||
  200.          mas[0][0] == 1 && mas[1][1] == 1 && mas[2][2] == 1 ||
  201.          mas[2][0] == 1 && mas[1][1] == 1 && mas[0][2] == 1
  202.         )  {
  203.         Result(1);
  204.     }
  205.     else if (mas[0][0] == -1 && mas[0][1] == -1 && mas[0][2] == -1 ||
  206.         mas[1][0] == -1 && mas[1][1] == -1 && mas[1][2] == -1 ||
  207.         mas[2][0] == -1 && mas[2][1] == -1 && mas[2][2] == -1 ||
  208.         mas[0][0] == -1 && mas[1][0] == -1 && mas[2][0] == -1 ||
  209.         mas[0][1] == -1 && mas[1][1] == -1 && mas[2][1] == -1 ||
  210.         mas[0][2] == -1 && mas[1][2] == -1 && mas[2][2] == -1 ||
  211.         mas[0][0] == -1 && mas[1][1] == -1 && mas[2][2] == -1 ||
  212.         mas[2][0] == -1 && mas[1][1] == -1 && mas[0][2] == -1
  213.         ) {
  214.         Result(2);
  215.     }
  216.     else {
  217.         p = 0;
  218.         for (int i = 0; i < 3; i++) {
  219.             for (int j = 0; j < 3; j++) {
  220.                 if (mas[i][j] != 0) {
  221.                     p++;
  222.                     if (p == 9) {
  223.                         Result(0);
  224.                     }
  225.                 }
  226.             }
  227.         }
  228.     }
  229. }
  230. void Game::Result(int k) {
  231.     gameOver = true;
  232.     if (k == 0)
  233.         cout << endl << "Ничья";
  234.     else if (k == 1)
  235.         cout << endl << "Выиграл игрок №1";
  236.     else if (k == 2)
  237.         cout << endl << "Выиграл игрок №2";
  238. }
  239.  
  240. int main() {
  241.     setlocale(LC_ALL, "Rus");
  242.     Game go;
  243.  
  244.     while (!gameOver) {
  245.         go.clear();
  246.         go.Draw();
  247.         go.Cursor();
  248.         go.update();
  249.     }
  250.     cin.get();
  251.     return 0;
  252. }
Advertisement
Add Comment
Please, Sign In to add comment