Advertisement
Guest User

Summer_Practice

a guest
Aug 21st, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.51 KB | None | 0 0
  1. ___________Game.hpp:
  2.  
  3. //
  4. //  Game.hpp
  5. //  Summer_Practice
  6. //
  7. //  Created by Максим Бачурин on 20/08/2017.
  8. //  Copyright © 2017 Максим Бачурин. All rights reserved.
  9. //
  10.  
  11. #ifndef Game_hpp
  12. #define Game_hpp
  13.  
  14. #include <stdio.h>
  15. #include <utility>
  16. #include <set>
  17. #include <vector>
  18. #include <map>
  19. using namespace std;
  20.  
  21. class Game
  22. {
  23.    
  24.     bool in_one_cell;
  25.     pair<int, int> user, computer;
  26.     char **chessboad;
  27.     //int amount_pawn;
  28.     const int inf = 1e9;
  29.     int user_count;
  30.     int computer_count;
  31.     set<pair<int, int>> pawns;
  32.     vector<pair<int, int>> moves;
  33.     map<pair<int, int>, int> distance_to_user, distance_to_computer;
  34.     map<pair<int, int>, pair<int, int>> parent;
  35.     map<pair<int, int>, bool> used;
  36.     char one_game();
  37.     void user_move();
  38.     void computer_move();
  39.     void bfs_for_user();
  40.     void bfs_for_computer();
  41.     //void input(bool first_move);
  42.     void update_chessboard();
  43.     void print();
  44. public:
  45.     Game();
  46.     void play();
  47.     ~Game();
  48. };
  49.  
  50. #endif /* Game_hpp */
  51.  
  52. _______________________________________________________________Game.cpp:
  53.  
  54.  
  55. //
  56. //  Game.cpp
  57. //  Summer_Practice
  58. //
  59. //  Created by Максим Бачурин on 20/08/2017.
  60. //  Copyright © 2017 Максим Бачурин. All rights reserved.
  61. //
  62.  
  63. #include <iostream>
  64. #include "Game.hpp"
  65. #include <ctime>
  66. #include <queue>
  67. #include <vector>
  68. #include <utility>
  69.  
  70. using namespace std;
  71.  
  72. Game::Game()
  73. {
  74.     in_one_cell = false;
  75.     user = computer = {0,0};
  76.     chessboad = NULL;
  77.     user_count = computer_count = 0;
  78. }
  79.  
  80. void Game::update_chessboard()
  81. {
  82.     for(int i = 0; i < 8; i++)
  83.     {
  84.         for(int j = 0; j < 8; j++)
  85.         {
  86.             chessboad[i][j] = '.';
  87.             if(i == user.first && j == user.second)
  88.                 chessboad[i][j] = 'Y';
  89.             if(i == computer.first && j == computer.second)
  90.                 chessboad[i][j] = 'C';
  91.             if(pawns.count({i,j}))
  92.                 chessboad[i][j] = 'P';
  93.         }
  94.     }
  95. }
  96.  
  97. char Game::one_game()
  98. {
  99.     pawns.clear();
  100.     parent.clear();
  101.     moves.clear();
  102.     chessboad = new char *[8];
  103.     for(int i = 0; i < 8; i++)
  104.         chessboad[i] = new char [8];
  105.     user = {0,0};
  106.     computer = {7,7};
  107.     int amount_pawn = 0;
  108.     user_count = computer_count = 0;
  109.     cout << "Введите количество пешек (1-62): ";
  110.     while(amount_pawn < 1 || amount_pawn > 62)
  111.         cin >> amount_pawn;
  112.     srand((int)time(NULL));
  113.     while (pawns.size() < amount_pawn)
  114.     {
  115.         pair<int, int> new_pawn = {rand()%8, rand()%8};
  116.         if(new_pawn != user && new_pawn != computer)
  117.             pawns.insert(new_pawn);
  118.     }
  119.     cout << "Могут ли игроки вставать одновременно\nна одну и ту же клетку? (Y/N): ";
  120.     char c;
  121.     cin >> c;
  122.     if(c == 'Y')
  123.         in_one_cell = true;
  124.     else
  125.         in_one_cell = false;
  126.     update_chessboard();
  127. //    if(rand()%2)
  128. //        user_move();
  129.     while(true)
  130.     {
  131.         computer_move();
  132.         if(pawns.empty())
  133.         {
  134.             if(user_count > computer_count)
  135.                 return '2';
  136.             if(user_count < computer_count)
  137.                 return '1';
  138.             return '3';
  139.         }
  140.         user_move();
  141.         if(pawns.empty())
  142.         {
  143.             if(user_count > computer_count)
  144.                 return '2';
  145.             if(user_count < computer_count)
  146.                 return '1';
  147.             return '3';
  148.         }
  149.     }
  150.     return '0';
  151. }
  152.  
  153. void Game::user_move()
  154. {
  155.     print();
  156.     cout << "Ваш ход. Введите координаты (A-H)(1-8)\n (Например: B8): ";
  157.     char x = ' ', y = ' ';
  158.     while(!(((abs(x - 'A' - user.first) == 1 &&
  159.               abs(y - '1' - user.second) == 2) ||
  160.        (abs(x - 'A' - user.first) == 2 &&
  161.         abs(y - '1' - user.second) == 1)) &&
  162.           (in_one_cell || !(((x - 'A') == computer.first) &&
  163.                             (y - '1') == computer.second)) &&
  164.             x >= 'A' && x <= 'H' && y >= '1' && y <= '8'))
  165.         cin >> x >> y;
  166.     user = {x - 'A', y - '1'};
  167.     if(pawns.count(user))
  168.     {
  169.         user_count++;
  170.         pawns.erase(user);
  171.     }
  172.     update_chessboard();
  173. }
  174.  
  175. void Game::computer_move()
  176. {
  177.     if(moves.empty())
  178.     {
  179.         bfs_for_user();
  180.         bfs_for_computer();
  181.         for(auto x : pawns)
  182.         {
  183.             cout << x.first+1 << ":" << x.second+1 << ":" << distance_to_user[x] << ":" << distance_to_computer[x] << endl;
  184.         }
  185.         int minimal = inf;
  186.         pair<int, int> nearest_pawn;
  187.         for(auto x : pawns)
  188.         {
  189.             if(distance_to_computer[x] <= distance_to_user[x] && distance_to_computer[x] < minimal)
  190.             {
  191.                 minimal = distance_to_computer[x];
  192.                 nearest_pawn = x;
  193.             }
  194.         }
  195.         if(minimal == inf)
  196.         {
  197.             for(auto x : pawns)
  198.             {
  199.                 if(distance_to_computer[x] < minimal)
  200.                 {
  201.                     minimal = distance_to_computer[x];
  202.                     nearest_pawn = x;
  203.                 }
  204.             }
  205.             pair<int, int> move = nearest_pawn;
  206.             while (parent[move] != computer)
  207.                 move = parent[move];
  208.             computer = move;
  209.         }
  210.         else
  211.         {
  212.             pair<int, int> move = nearest_pawn;
  213.             while (parent[move] != computer) {
  214.                 moves.push_back(move);
  215.                 move = parent[move];
  216.             }
  217.             computer = move;
  218.         }
  219.     }
  220.     else
  221.     {
  222.         pair<int, int> move = moves.back();
  223.         moves.pop_back();
  224.         if(pawns.count(move))
  225.         {
  226.             computer_count++;
  227.             pawns.erase(move);
  228.         }
  229.         computer = move;
  230.     }
  231.     update_chessboard();
  232. }
  233.  
  234. void Game::bfs_for_user()
  235. {
  236.     used.clear();
  237.     distance_to_user.clear();
  238.     vector<pair<int, int>> direction{{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}};
  239.     distance_to_user[user] = 0;
  240.     used[user] = true;
  241.     queue<pair<int, int>> q;
  242.     q.push(user);
  243.     while(!q.empty())
  244.     {
  245.         pair<int, int> k = q.front();
  246.         q.pop();
  247.         for(auto x : direction)
  248.         {
  249.             pair<int, int> new_k = {k.first + x.first, k.second + x.second};
  250.             if(0 <= new_k.first && new_k.first < 8 &&
  251.                0 <= new_k.second && new_k.second < 8 && !used.count(new_k))
  252.             {
  253.                 used[new_k] = true;
  254.                 q.push(new_k);
  255.                 distance_to_user[new_k] = distance_to_user[k] + 1;
  256.             }
  257.         }
  258.     }
  259. }
  260.  
  261. void Game::bfs_for_computer()
  262. {
  263.     distance_to_computer.clear();
  264.     parent.clear();
  265.     used.clear();
  266.     vector<pair<int, int>> direction{{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}};
  267.     distance_to_computer[computer] = 0;
  268.     used[computer] = true;
  269.     queue<pair<int, int>> q;
  270.     q.push(computer);
  271.     while(!q.empty())
  272.     {
  273.         pair<int, int> k = q.front();
  274.         q.pop();
  275.         for(auto x : direction)
  276.         {
  277.             pair<int, int> new_k = {k.first + x.first, k.second + x.second};
  278.             if(0 <= new_k.first && new_k.first < 8 &&
  279.                0 <= new_k.second && new_k.second < 8 && !used.count(new_k))
  280.             {
  281.                 used[new_k] = true;
  282.                 q.push(new_k);
  283.                 distance_to_computer[new_k] = distance_to_computer[k] + 1;
  284.                 parent[new_k] = k;
  285.             }
  286.         }
  287.     }
  288. }
  289.  
  290. void Game::print()
  291. {
  292.     //update_chessboard();
  293.     cout << "  1 2 3 4 5 6 7 8 \n";
  294.     for(int i = 0; i < 8; i++)
  295.     {
  296.         cout << " .-.-.-.-.-.-.-.-.\n";
  297.         cout << char('A' + i) << '|';
  298.         for(int j = 0; j < 8; j++)
  299.         {
  300.             cout << chessboad[i][j] << '|';
  301.         }
  302.         cout << endl;
  303.     }
  304.     cout << " .-.-.-.-.-.-.-.-.\n";
  305. }
  306.  
  307. void Game::play()
  308. {
  309.     char solution;
  310.     do
  311.     {
  312.         char result = one_game();
  313.         print();
  314.         if(result == '1')
  315.             cout << "Вы проиграли :(" << endl;
  316.         if(result == '2')
  317.             cout << "Вы выиграли!!!" << endl;
  318.         if(result == '3')
  319.             cout << "Ничья." << endl;
  320.         cout << "Хотите сыграть ещё? (Y/N): ";
  321.         cin >> solution;
  322.     }while(solution == 'Y');
  323. }
  324.  
  325. Game::~Game()
  326. {
  327.     for(int i = 0; i < 8; i++)
  328.         delete [] chessboad[i];
  329.     delete [] chessboad;
  330. }
  331. ____________________________________________________________________
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement