-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.h
326 lines (272 loc) · 7.91 KB
/
Game.h
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#pragma once
/*
Copyright (C) 2018 Pharap (@Pharap)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#define DISABLEAVRMIN
#include <Pokitto.h>
#include <algorithm>
#include <utility>
#include <cstddef>
#include <cstdint>
#include "Grid.h"
#include "Point.h"
class Game
{
public:
enum class Cell : std::uint8_t
{
None,
Nought,
Cross,
};
enum class Status : std::uint8_t
{
Unfinished,
NoughtsWins,
CrossesWins,
Draw,
};
public:
using CellGrid = Grid<Cell, 3, 3>;
using PointType = Point<std::size_t>;
private:
CellGrid grid;
PointType selector;
Cell currentTurn = Cell::Nought;
Status status = Status::Unfinished;
public:
void run(void);
private:
void update(void);
void draw(void);
std::pair<bool, Cell> getWinner(void) const;
bool hasAnyEmptyCells(void) const;
Status calculateStatus(void) const;
void drawGrid(void);
void drawStatus(void);
};
void Game::run(void)
{
Pokitto::Core::begin();
while (Pokitto::Core::isRunning())
if (Pokitto::Core::update())
{
this->update();
this->draw();
}
}
void Game::update(void)
{
if(Pokitto::Buttons::pressed(BTN_LEFT))
if(this->selector.x > CellGrid::FirstX)
--this->selector.x;
if(Pokitto::Buttons::pressed(BTN_RIGHT))
if(this->selector.x < CellGrid::LastX)
++this->selector.x;
if(Pokitto::Buttons::pressed(BTN_UP))
if(this->selector.y > CellGrid::FirstY)
--this->selector.y;
if(Pokitto::Buttons::pressed(BTN_DOWN))
if(this->selector.y < CellGrid::LastY)
++this->selector.y;
if(Pokitto::Buttons::pressed(BTN_A))
if(this->grid.getItem(this->selector.x, this->selector.y) == Cell::None)
{
this->grid.getItem(this->selector.x, this->selector.y) = this->currentTurn;
switch(this->currentTurn)
{
case Cell::Nought:
this->currentTurn = Cell::Cross;
break;
case Cell::Cross:
this->currentTurn = Cell::Nought;
break;
default: break;
}
this->status = this->calculateStatus();
}
if(Pokitto::Buttons::held(BTN_B, 10) && (this->status != Status::Unfinished))
{
this->grid.fill(Cell::None);
this->status = Status::Unfinished;
}
}
void Game::draw(void)
{
drawGrid();
drawStatus();
}
std::pair<bool, Game::Cell> Game::getWinner(void) const
{
static const PointType winningSets[][3] =
{
// Vertical
{ PointType(0, 0), PointType(0, 1), PointType(0, 2) },
{ PointType(1, 0), PointType(1, 1), PointType(1, 2) },
{ PointType(2, 0), PointType(2, 1), PointType(2, 2) },
// Horizontal
{ PointType(0, 0), PointType(1, 0), PointType(2, 0) },
{ PointType(0, 1), PointType(1, 1), PointType(2, 1) },
{ PointType(0, 2), PointType(1, 2), PointType(2, 2) },
// Diagonal
{ PointType(0, 0), PointType(1, 1), PointType(2, 2) },
{ PointType(2, 0), PointType(1, 1), PointType(0, 2) },
};
for(auto const & line : winningSets)
{
const PointType start = line[0];
const Cell type = this->grid.getItem(start.x, start.y);
if(type == Cell::None)
continue;
bool success = true;
for(std::size_t i = 0; i < 3; ++i)
if(this->grid.getItem(line[i].x, line[i].y) != type)
{
success = false;
break;
}
if(success)
return std::make_pair(true, type);
}
return std::make_pair(false, Cell::None);
}
bool Game::hasAnyEmptyCells(void) const
{
for(std::size_t y = 0; y < CellGrid::Height; ++y)
for(std::size_t x = 0; x < CellGrid::Width; ++x)
if(this->grid.getItem(x, y) == Cell::None)
return true;
return false;
}
Game::Status Game::calculateStatus(void) const
{
const auto winner = getWinner();
const bool success = winner.first;
if(success)
{
const Cell type = winner.second;
switch(type)
{
case Cell::Nought:
return Status::NoughtsWins;
case Cell::Cross:
return Status::CrossesWins;
default:
return Status::Unfinished;
}
}
return (this->hasAnyEmptyCells()) ? Status::Unfinished : Status::Draw;
}
void Game::drawGrid(void)
{
// Drawing parameters for easy modification
constexpr std::int16_t xGap = 8;
constexpr std::int16_t yGap = 8;
constexpr std::int16_t cellWidth = 32;
constexpr std::int16_t cellHeight = 32;
//const int blackIndex = 0;
constexpr std::uint8_t whiteIndex = 1;
constexpr std::int16_t boardWidth = (cellWidth * CellGrid::Width) + (xGap * 2);
constexpr std::int16_t boardHeight = (cellHeight * CellGrid::Height) + (yGap * 2);
const std::int16_t xOffset = (Pokitto::Display::getWidth() - boardWidth) / 2;
const std::int16_t yOffset = (Pokitto::Display::getHeight() - boardHeight) / 2;
const std::int16_t leftLineX = xOffset + (cellWidth * 1) + (xGap * 0) + (xGap / 2);
const std::int16_t rightLineX = xOffset + (cellWidth * 2) + (xGap * 1) + (xGap / 2);
const std::int16_t topLineY = yOffset + (cellHeight * 1) + (yGap * 0) + (yGap / 2);
const std::int16_t bottomLineY = yOffset + (cellHeight * 2) + (yGap * 1) + (yGap / 2);
// Draw lines
Pokitto::Display::setColor(whiteIndex);
Pokitto::Display::drawLine(leftLineX, yOffset - 4, leftLineX, yOffset + boardHeight + 8);
Pokitto::Display::drawLine(rightLineX, yOffset - 4, rightLineX, yOffset + boardHeight + 8);
Pokitto::Display::drawLine(xOffset - 4, topLineY, xOffset + boardWidth + 8, topLineY);
Pokitto::Display::drawLine(xOffset - 4, bottomLineY, xOffset + boardWidth + 8, bottomLineY);
// Draw grid
for(std::size_t y = 0; y < CellGrid::Height; ++y)
for(std::size_t x = 0; x < CellGrid::Width; ++x)
{
const std::int16_t cellX = xOffset + ((cellWidth + xGap) * x);
const std::int16_t cellY = yOffset + ((cellHeight + yGap) * y);
// Draw symbol
switch(grid.getItem(x, y))
{
case Cell::Nought:
{
constexpr std::int16_t halfCellWidth = cellWidth / 2;
constexpr std::int16_t halfCellHeight = cellHeight / 2;
const std::int16_t cellCentreX = cellX + halfCellWidth;
const std::int16_t cellCentreY = cellY + halfCellHeight;
const std::int16_t radius = std::min(halfCellWidth, halfCellHeight) - 1;
Pokitto::Display::drawCircle(cellCentreX, cellCentreY, radius);
break;
}
case Cell::Cross:
{
const std::int16_t left = cellX + 1;
const std::int16_t top = cellY + 1;
const std::int16_t right = cellX + (cellWidth - 1);
const std::int16_t bottom = cellY + (cellHeight - 1);
Pokitto::Display::drawLine(left, top, right, bottom);
Pokitto::Display::drawLine(right, top, left, bottom);
break;
}
default:
break;
}
// Draw selector
if((x == this->selector.x) && (y == this->selector.y))
Pokitto::Display::drawRect(cellX, cellY, cellWidth, cellHeight);
}
}
void Game::drawStatus(void)
{
Pokitto::Display::setCursor(16, 8);
switch(this->status)
{
case Status::Unfinished:
{
switch(this->currentTurn)
{
case Cell::Nought:
{
Pokitto::Display::print("Player: Noughts");
break;
}
case Cell::Cross:
{
Pokitto::Display::print("Player: Crosses");
break;
}
case Cell::None:
{
Pokitto::Display::print("Error!?");
break;
}
}
break;
}
case Status::NoughtsWins:
{
Pokitto::Display::print("Noughts Wins!");
break;
}
case Status::CrossesWins:
{
Pokitto::Display::print("Crosses Wins!");
break;
}
case Status::Draw:
{
Pokitto::Display::print("Draw!");
break;
}
}
}