-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
271 lines (241 loc) · 4.25 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
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
#include "pch.h"
#include <iostream>
#include <vector>
#include "Game.h"
#include "Button.h"
using namespace std;
using namespace sf;
Game::Game(int x,int y,string name = "Void")
{
window.create(VideoMode(x, y), name);
window.setVerticalSyncEnabled(true);
window.setFramerateLimit(60);
}
void Game::initializeButtons()
{
Vector2f size(96, 96);
Button sample(size);
sample.setFillColor(Color::Black);
for (int i = 0; i < 9; i++)
{
if (i < 3)
{
sample.setPosition(i * 103, 0);
}
else
if (i < 6)
{
sample.setPosition((i - 3) * 103, 103);
}
else
{
sample.setPosition((i - 6) * 103, 206);
}
buttons[i] = sample;
}
}
void Game::initializeLines()
{
Vector2f wie(3, 300);
Vector2f ww(300, 3);
RectangleShape line;
line.setFillColor(Color::Green);
for (int i = 0; i < 4; i++)
{
if (i < 2)
line.setSize(ww);
else
line.setSize(wie);
switch (i) {
case 0:
line.setPosition(0, 100);
break;
case 1:
line.setPosition(0, 200);
break;
case 2:
line.setPosition(100, 0);
break;
case 3:
line.setPosition(200, 0);
break;
}
lines[i] = line;
}
}
void Game::initializeTexture()
{
tixx.loadFromFile("tic.png");
toxx.loadFromFile("toe.png");
tixx.setSmooth(true);
toxx.setSmooth(true);
}
void Game::initializeText()
{
komunikat = "Wygraly: ";
cour.loadFromFile("cour.ttf");
if (checkWin() > 0)
{
if (turn)
{
komunikat += "kolka";
}
else
{
komunikat += "krzyzyki";
}
}
else
komunikat = "Remis";
for (int i = 0; i < komunikat.size(); i++)
{
Text literka;
literka.setFont(cour);
//literka.setColor(Color(0,0,255));
literka.setPosition(Vector2f( 50 + (i*20), 100 ));
}
//TODO
}
void Game::initializeAll()
{
initializeButtons();
initializeLines();
initializeTexture();
//initializeText();
}
void Game::createSprite(int i)
{
Sprite sp;
if (turn)
{
sp.setTexture(tixx);
}
else
{
sp.setTexture(toxx);
}
switch (i)
{
case 0:
sp.setPosition(Vector2f(0, 0));
break;
case 1:
sp.setPosition(Vector2f(103, 0));
break;
case 2:
sp.setPosition(Vector2f(206, 0));
break;
case 3:
sp.setPosition(Vector2f(0, 103));
break;
case 4:
sp.setPosition(Vector2f(103, 103));
break;
case 5:
sp.setPosition(Vector2f(206, 103));
break;
case 6:
sp.setPosition(Vector2f(0, 206));
break;
case 7:
sp.setPosition(Vector2f(103, 206));
break;
case 8:
sp.setPosition(Vector2f(206, 206));
break;
}
arr.push_back(sp);
}
int Game::checkWin()
{
for (int i = 0; i < 3; i++ )
{
if (tab[i*3] == tab[i * 3 + 1] && tab[i * 3] == tab[i * 3 + 2] && tab[i * 3] != 0)
{
return tab[i * 3];
}
}
for (int i = 0; i < 3; i++)
{
if (tab[i] == tab[i + 3] && tab[i] == tab[i + 6] && tab[i] != 0)
{
return tab[i];
}
}
if (tab[0] == tab[4] && tab[0] == tab[8] && tab[0] != 0)
return tab[0];
if (tab[2] == tab[4] && tab[2] == tab[6] && tab[2] != 0)
return tab[0];
return 0;
}
void Game::drawEnd()
{
window.clear(Color::White);
//TODO
}
void Game::endGame()
{
initializeText();
//TODO
}
void Game::drawAll()
{
window.clear();
for (int i = 0; i < 9; i++)
window.draw(buttons[i]);
for (int i = 0; i < 4; i++)
window.draw(lines[i]);
for (int i = 0; i < arr.size(); i++)
window.draw(arr[i]);
window.display();
}
void Game::running() // main method
{
while (window.isOpen())
{
if (arr.size() == 9) end = true;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
window.close();
if(end == false)
if (event.type == Event::MouseButtonPressed) //Works well
{
do
{
} while (event.type == Event::MouseButtonReleased);
for (int i = 0; i < 9; i++)
if (buttons[i].updateState(sf::Mouse::getPosition(window)))
{
//cout << "button number: " << i << ". \n";
if (tab[i] == 0)
{
if (turn)
{
tab[i] = 1;
//cout << "1 \n";
createSprite(i);
turn = false;
}
else
{
tab[i] = 2;
// cout << "2 \n";
createSprite(i);
turn = true;
}
if (checkWin() != 0)
{
cout << "end of game \n";
cout << "win team: " << checkWin();
end = true;
endGame();
break;
}
}
}
}
}
drawAll();
}
}