-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.cpp
164 lines (132 loc) · 3.11 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "Game.h"
#include <QSet>
#include <QDebug>
#include "AIPlayerRandom.h"
#include "AIPlayerDifficult.h"
Game::Game()
{
m_map = GameMap(3, 3);
reset();
active = false;
}
Player* Game::addHumanPlayer()
{
int id = m_players.size();
PlayerIcon icon = (PlayerIcon)(id + 1);
Player *player = new Player(icon);
player->setLocal(true);
player->appendToGame(this);
m_players.append(player);
return m_players.last();
}
Player* Game::addAIPlayer(Difficulty mode)
{
int id = m_players.size();
PlayerIcon icon = (PlayerIcon)(id + 1);
Player *player;
switch(mode) {
case Difficulty::Perfect:
case Difficulty::Easy:
case Difficulty::Medium:
case Difficulty::Hard:
player = new AIPlayerDifficult(mode);
break;
case Difficulty::Random:
player = new AIPlayerRandom();
break;
default:
player = new Player();
break;
}
player->setIcon(icon);
player->setLocal(false);
player->appendToGame(this);
m_players.append(player);
return m_players.last();
}
void Game::removeAllPlayers()
{
qDeleteAll(m_players);
m_players.clear();
}
GameMap* Game::map()
{
return &m_map;
}
void Game::reset()
{
map()->reset();
curPlayerId = 0;
}
Player* Game::currentPlayer()
{
if(m_players.size() == 0)
return Player::NoPlayer;
return m_players[curPlayerId];
}
PlayerIcon Game::currentPlayerIcon()
{
return currentPlayer()->icon();
}
bool Game::activateField(QPoint pos) {
return activateField(pos.x(), pos.y());
}
bool Game::activateField(int x, int y)
{
if(map()->playerAt(x, y) != Player::NoPlayer) {
return false;
}
map()->setField(x, y, currentPlayer());
if(!checkWinner(x, y))
nextTurn();
return true;
}
bool Game::allowsInput()
{
return isRunning() && currentPlayer()->isLocal();
}
bool Game::isRunning()
{
return active;
}
void Game::start()
{
reset();
active = true;
}
void Game::end()
{
active = false;
}
void Game::nextTurn()
{
curPlayerId = (curPlayerId + 1) % m_players.size();
currentPlayer()->process();
}
bool Game::checkWinner(int x, int y)
{
// check the actual winner
QSet<QPoint> winningFields;
// rows
map()->getWinnerFromLine(QPoint(x, y), QPoint(1, 0), winLength, &winningFields);
// columns
map()->getWinnerFromLine(QPoint(x, y), QPoint(0, 1), winLength, &winningFields);
// diagonals
map()->getWinnerFromLine(QPoint(x, y), QPoint(1, 1), winLength, &winningFields);
map()->getWinnerFromLine(QPoint(x, y), QPoint(1, -1), winLength, &winningFields);
if(!winningFields.isEmpty()) {
emit winner(map()->playerAt(*winningFields.begin()), winningFields);
end();
return true;
}
if(map()->isFull()) {
emit winner(Player::NoPlayer, winningFields);
end();
return true;
}
return false;
}
QList<Player*> Game::players() const
{
return m_players;
}