-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.cpp
107 lines (90 loc) · 2.35 KB
/
Game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// Created by Brazhenko Andrew on 19.01.2021.
//
#include "Game.h"
Gomoku::Game::Game()
: clock_(0, 0)
{}
void Gomoku::Game::Go(const std::string &player1, const std::string &player2, const std::string &gameVersion,
const std::string &gameTime)
{
if (this->state_ == State::GameInPause)
{
clock_.Continue();
state_ = State::GameInProcess;
return;
}
else if (this->state_ == State::Main)
{
if (gameTime == "1 minute")
clock_ = {60, 60, board_.WhiteMove()};
else if (gameTime == "3 minutes")
clock_ = {180, 180, board_.WhiteMove()};
else if (gameTime == "5 minutes")
clock_ = {300, 300, board_.WhiteMove()};
else if (gameTime == "10 minutes")
clock_ = {600, 600, board_.WhiteMove()};
else if (gameTime == "15 minutes")
clock_ = {900, 900, board_.WhiteMove()};
else
clock_ = {100, 100, board_.WhiteMove()};
Gomoku::MakeMove_t MakeMoveWhite = [this](int row, int col) {
const auto &tmp = this->board_.GetAvailableMoves();
if (std::find(tmp.begin(), tmp.end(), std::make_pair(row, col)) != tmp.end())
{
auto ret = this->board_.MakeMove({row, col});
this->clock_.ChangeMove();
this->blackPlayer->YourTurn();
return ret;
}
return Board::MoveResult::Default;
};
Gomoku::MakeMove_t MakeMoveBlack = [this](int row, int col) {
const auto &tmp = this->board_.GetAvailableMoves();
if (std::find(tmp.begin(), tmp.end(), std::make_pair(row, col)) != tmp.end())
{
auto ret = this->board_.MakeMove({row, col});
this->clock_.ChangeMove();
this->whitePlayer->YourTurn();
return ret;
}
return Board::MoveResult::Default;
};
whitePlayer = PlayerFactory(player1, Board::Side::White, MakeMoveWhite, board_, board_.WhiteMove());
blackPlayer = PlayerFactory(player2, Board::Side::Black, MakeMoveBlack, board_, !board_.WhiteMove());
state_ = State::GameInProcess;
clock_.Start();
}
}
void Gomoku::Game::Pause()
{
if (this->state_ == State::GameInProcess)
{
state_ = State::GameInPause;
clock_.Pause();
}
}
void Gomoku::Game::Stop()
{
state_ = State::Main;
whitePlayer.reset();
blackPlayer.reset();
clock_.Stop();
board_.Reset();
}
void Gomoku::Game::TakeBack()
{
if (this->state_ == State::GameInPause)
{
if (!board_.TakeBackMove())
return;
if (board_.WhiteMove())
{
this->whitePlayer->YourTurn();
}
else
{
this->blackPlayer->YourTurn();
}
}
}