-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckers.cpp
More file actions
199 lines (165 loc) · 5.59 KB
/
Checkers.cpp
File metadata and controls
199 lines (165 loc) · 5.59 KB
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
// TODO:
/*
* - Capture multiple checkers
* - Add game ending
* - Add game restarting
*/
#include "../Include/defGameEngine.hpp"
constexpr int nRows = 3;
class Checkers : public def::GameEngine
{
public:
Checkers()
{
GetWindow()->SetTitle("Checkers");
}
struct Cell
{
enum class Colour
{
Black,
White
};
bool bOccupied = false;
Colour colCell;
Colour colChecker;
};
def::Vector2i vFieldSize;
def::Vector2i vCellSize;
std::vector<Cell> vecCells;
struct Target
{
Cell* ptr = nullptr;
int nIndex = -1;
};
Target target;
public:
bool OnUserCreate() override
{
vCellSize = { 32, 32 };
vFieldSize = GetWindow()->GetScreenSize() / vCellSize;
vecCells.resize(vFieldSize.x * vFieldSize.y);
for (int x = 0; x < vFieldSize.x; x++)
for (int y = 0; y < vFieldSize.y; y++)
{
int i = x + y * vFieldSize.x;
vecCells[i].colCell = Cell::Colour((i + y + 1) % 2);
vecCells[i].bOccupied = false;
}
for (int y = 0; y < nRows; y++)
for (int x = 0; x < vFieldSize.x; x++)
{
int t = y * vFieldSize.x + x;
int b = (vFieldSize.y - y - 1) * vFieldSize.x + x;
if (vecCells[t].colCell == Cell::Colour::Black)
{
vecCells[t].bOccupied = true;
vecCells[t].colChecker = Cell::Colour::White;
}
if (vecCells[b].colCell == Cell::Colour::Black)
{
vecCells[b].bOccupied = true;
vecCells[b].colChecker = Cell::Colour::Black;
}
}
return true;
}
bool OnUserUpdate(float fElapsedTime) override
{
if (GetInput()->GetButtonState(def::Button::LEFT).pressed)
{
def::Vector2i vMouse = GetInput()->GetMousePosition() / vCellSize;
if (vMouse.x >= 0 && vMouse.y >= 0 && vMouse.x < vFieldSize.x && vMouse.y < vFieldSize.y)
{
int i = vMouse.y * vFieldSize.x + vMouse.x;
if (target.ptr == nullptr)
{
if (vecCells[i].bOccupied)
{
target.ptr = &vecCells[i];
target.nIndex = i;
}
}
else
{
if (vecCells[i].bOccupied)
{
if (target.ptr == &vecCells[i])
target.ptr = nullptr;
else
{
if (target.ptr->colChecker == vecCells[i].colChecker)
{
target.ptr = &vecCells[i];
target.nIndex = i;
}
}
}
else
{
int dx = vMouse.x - target.nIndex % vFieldSize.x;
int dy = vMouse.y - target.nIndex / vFieldSize.x;
bool bAllowMove = false;
if (abs(dx) == 1 && abs(dy) == 1)
bAllowMove = true;
else if (abs(dx) == 2 && abs(dy) == 2)
{
int bx = vMouse.x - dx / 2;
int by = vMouse.y - dy / 2;
int j = by * vFieldSize.x + bx;
if (vecCells[j].bOccupied && vecCells[j].colChecker != target.ptr->colChecker)
{
vecCells[j].bOccupied = false;
bAllowMove = true;
}
}
if (bAllowMove)
{
vecCells[i].bOccupied = true;
vecCells[i].colChecker = target.ptr->colChecker;
target.ptr->bOccupied = false;
target.ptr = nullptr;
}
}
}
}
}
def::Vector2i p;
for (p.x = 0; p.x < vFieldSize.x; p.x++)
for (p.y = 0; p.y < vFieldSize.y; p.y++)
{
def::Vector2i coord = p * vCellSize;
int i = p.y * vFieldSize.x + p.x;
switch (vecCells[i].colCell)
{
case Cell::Colour::Black:
FillRectangle(coord, vCellSize, def::BLACK);
break;
case Cell::Colour::White:
FillRectangle(coord, vCellSize, def::WHITE);
break;
}
if (vecCells[i].bOccupied)
{
int radius = std::min(vCellSize.x, vCellSize.y) * 0.25f;
switch (vecCells[i].colChecker)
{
case Cell::Colour::Black:
FillCircle(coord + vCellSize / 2, radius, def::DARK_RED);
break;
case Cell::Colour::White:
FillCircle(coord + vCellSize / 2, radius, def::WHITE);
break;
}
}
}
return true;
}
};
int main()
{
Checkers demo;
if (demo.Construct(256, 256, 2, 2))
demo.Run();
return 0;
}