Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ___________Game.hpp:
- //
- // Game.hpp
- // Summer_Practice
- //
- // Created by Максим Бачурин on 20/08/2017.
- // Copyright © 2017 Максим Бачурин. All rights reserved.
- //
- #ifndef Game_hpp
- #define Game_hpp
- #include <stdio.h>
- #include <utility>
- #include <set>
- #include <vector>
- #include <map>
- using namespace std;
- class Game
- {
- bool in_one_cell;
- pair<int, int> user, computer;
- char **chessboad;
- //int amount_pawn;
- const int inf = 1e9;
- int user_count;
- int computer_count;
- set<pair<int, int>> pawns;
- vector<pair<int, int>> moves;
- map<pair<int, int>, int> distance_to_user, distance_to_computer;
- map<pair<int, int>, pair<int, int>> parent;
- map<pair<int, int>, bool> used;
- char one_game();
- void user_move();
- void computer_move();
- void bfs_for_user();
- void bfs_for_computer();
- //void input(bool first_move);
- void update_chessboard();
- void print();
- public:
- Game();
- void play();
- ~Game();
- };
- #endif /* Game_hpp */
- _______________________________________________________________Game.cpp:
- //
- // Game.cpp
- // Summer_Practice
- //
- // Created by Максим Бачурин on 20/08/2017.
- // Copyright © 2017 Максим Бачурин. All rights reserved.
- //
- #include <iostream>
- #include "Game.hpp"
- #include <ctime>
- #include <queue>
- #include <vector>
- #include <utility>
- using namespace std;
- Game::Game()
- {
- in_one_cell = false;
- user = computer = {0,0};
- chessboad = NULL;
- user_count = computer_count = 0;
- }
- void Game::update_chessboard()
- {
- for(int i = 0; i < 8; i++)
- {
- for(int j = 0; j < 8; j++)
- {
- chessboad[i][j] = '.';
- if(i == user.first && j == user.second)
- chessboad[i][j] = 'Y';
- if(i == computer.first && j == computer.second)
- chessboad[i][j] = 'C';
- if(pawns.count({i,j}))
- chessboad[i][j] = 'P';
- }
- }
- }
- char Game::one_game()
- {
- pawns.clear();
- parent.clear();
- moves.clear();
- chessboad = new char *[8];
- for(int i = 0; i < 8; i++)
- chessboad[i] = new char [8];
- user = {0,0};
- computer = {7,7};
- int amount_pawn = 0;
- user_count = computer_count = 0;
- cout << "Введите количество пешек (1-62): ";
- while(amount_pawn < 1 || amount_pawn > 62)
- cin >> amount_pawn;
- srand((int)time(NULL));
- while (pawns.size() < amount_pawn)
- {
- pair<int, int> new_pawn = {rand()%8, rand()%8};
- if(new_pawn != user && new_pawn != computer)
- pawns.insert(new_pawn);
- }
- cout << "Могут ли игроки вставать одновременно\nна одну и ту же клетку? (Y/N): ";
- char c;
- cin >> c;
- if(c == 'Y')
- in_one_cell = true;
- else
- in_one_cell = false;
- update_chessboard();
- // if(rand()%2)
- // user_move();
- while(true)
- {
- computer_move();
- if(pawns.empty())
- {
- if(user_count > computer_count)
- return '2';
- if(user_count < computer_count)
- return '1';
- return '3';
- }
- user_move();
- if(pawns.empty())
- {
- if(user_count > computer_count)
- return '2';
- if(user_count < computer_count)
- return '1';
- return '3';
- }
- }
- return '0';
- }
- void Game::user_move()
- {
- print();
- cout << "Ваш ход. Введите координаты (A-H)(1-8)\n (Например: B8): ";
- char x = ' ', y = ' ';
- while(!(((abs(x - 'A' - user.first) == 1 &&
- abs(y - '1' - user.second) == 2) ||
- (abs(x - 'A' - user.first) == 2 &&
- abs(y - '1' - user.second) == 1)) &&
- (in_one_cell || !(((x - 'A') == computer.first) &&
- (y - '1') == computer.second)) &&
- x >= 'A' && x <= 'H' && y >= '1' && y <= '8'))
- cin >> x >> y;
- user = {x - 'A', y - '1'};
- if(pawns.count(user))
- {
- user_count++;
- pawns.erase(user);
- }
- update_chessboard();
- }
- void Game::computer_move()
- {
- if(moves.empty())
- {
- bfs_for_user();
- bfs_for_computer();
- for(auto x : pawns)
- {
- cout << x.first+1 << ":" << x.second+1 << ":" << distance_to_user[x] << ":" << distance_to_computer[x] << endl;
- }
- int minimal = inf;
- pair<int, int> nearest_pawn;
- for(auto x : pawns)
- {
- if(distance_to_computer[x] <= distance_to_user[x] && distance_to_computer[x] < minimal)
- {
- minimal = distance_to_computer[x];
- nearest_pawn = x;
- }
- }
- if(minimal == inf)
- {
- for(auto x : pawns)
- {
- if(distance_to_computer[x] < minimal)
- {
- minimal = distance_to_computer[x];
- nearest_pawn = x;
- }
- }
- pair<int, int> move = nearest_pawn;
- while (parent[move] != computer)
- move = parent[move];
- computer = move;
- }
- else
- {
- pair<int, int> move = nearest_pawn;
- while (parent[move] != computer) {
- moves.push_back(move);
- move = parent[move];
- }
- computer = move;
- }
- }
- else
- {
- pair<int, int> move = moves.back();
- moves.pop_back();
- if(pawns.count(move))
- {
- computer_count++;
- pawns.erase(move);
- }
- computer = move;
- }
- update_chessboard();
- }
- void Game::bfs_for_user()
- {
- used.clear();
- distance_to_user.clear();
- vector<pair<int, int>> direction{{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}};
- distance_to_user[user] = 0;
- used[user] = true;
- queue<pair<int, int>> q;
- q.push(user);
- while(!q.empty())
- {
- pair<int, int> k = q.front();
- q.pop();
- for(auto x : direction)
- {
- pair<int, int> new_k = {k.first + x.first, k.second + x.second};
- if(0 <= new_k.first && new_k.first < 8 &&
- 0 <= new_k.second && new_k.second < 8 && !used.count(new_k))
- {
- used[new_k] = true;
- q.push(new_k);
- distance_to_user[new_k] = distance_to_user[k] + 1;
- }
- }
- }
- }
- void Game::bfs_for_computer()
- {
- distance_to_computer.clear();
- parent.clear();
- used.clear();
- vector<pair<int, int>> direction{{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}};
- distance_to_computer[computer] = 0;
- used[computer] = true;
- queue<pair<int, int>> q;
- q.push(computer);
- while(!q.empty())
- {
- pair<int, int> k = q.front();
- q.pop();
- for(auto x : direction)
- {
- pair<int, int> new_k = {k.first + x.first, k.second + x.second};
- if(0 <= new_k.first && new_k.first < 8 &&
- 0 <= new_k.second && new_k.second < 8 && !used.count(new_k))
- {
- used[new_k] = true;
- q.push(new_k);
- distance_to_computer[new_k] = distance_to_computer[k] + 1;
- parent[new_k] = k;
- }
- }
- }
- }
- void Game::print()
- {
- //update_chessboard();
- cout << " 1 2 3 4 5 6 7 8 \n";
- for(int i = 0; i < 8; i++)
- {
- cout << " .-.-.-.-.-.-.-.-.\n";
- cout << char('A' + i) << '|';
- for(int j = 0; j < 8; j++)
- {
- cout << chessboad[i][j] << '|';
- }
- cout << endl;
- }
- cout << " .-.-.-.-.-.-.-.-.\n";
- }
- void Game::play()
- {
- char solution;
- do
- {
- char result = one_game();
- print();
- if(result == '1')
- cout << "Вы проиграли :(" << endl;
- if(result == '2')
- cout << "Вы выиграли!!!" << endl;
- if(result == '3')
- cout << "Ничья." << endl;
- cout << "Хотите сыграть ещё? (Y/N): ";
- cin >> solution;
- }while(solution == 'Y');
- }
- Game::~Game()
- {
- for(int i = 0; i < 8; i++)
- delete [] chessboad[i];
- delete [] chessboad;
- }
- ____________________________________________________________________
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement