-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
107 lines (94 loc) · 3.49 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
#include "Game.h"
Game::Game()
{
//Подгружаем шрифт для отрисовки элементов
font.loadFromFile("calibri.ttf");
Init();
}
void Game::Init()
{
//Заполняем массив плашек
for (int i = 0; i < ARRAY_SIZE - 1; i++)elements[i] = i + 1;
//Пустая ячейка- в последнем элементе массива
empty_index = ARRAY_SIZE - 1;
elements[empty_index] = 0; //Пустая плашка имеет значение = 0
solved = true;
}
bool Game::Check()
{
//Проверка собранности головоломки
for (unsigned int i = 0; i < ARRAY_SIZE; i++)
{
if (elements[i] > 0 && elements[i] != i + 1) return false;
}
return true;
}
void Game::Move(Direction direction)
{
//вычисляем строку и колонку пустой плашки
int col = empty_index % SIZE;
int row = empty_index / SIZE;
//проверка на возможность перемещения и вычисления индекса перемещаемой плашки
int move_index = -1;
if (direction == Direction::Left && col < (SIZE - 1)) move_index = empty_index + 1;
if (direction == Direction::Right && col > 0) move_index = empty_index - 1;
if (direction == Direction::Up && row < (SIZE - 1)) move_index = empty_index + SIZE;
if (direction == Direction::Down && row > 0) move_index = empty_index - SIZE;
//перемещение плашки на место пустой
if (empty_index >= 0 && move_index >= 0)
{
int tmp = elements[empty_index];
elements[empty_index] = elements[move_index];
elements[move_index] = tmp;
empty_index = move_index;
}
solved = Check();
}
void Game::draw(RenderTarget & target, RenderStates states) const
{
states.transform *= getTransform();
Color color = Color(200, 100, 200);
//рисуем рамки игрового поля
RectangleShape shape(Vector2f(FIELD_SIZE, FIELD_SIZE));
shape.setOutlineThickness(2.f);
shape.setOutlineColor(color);
shape.setFillColor(Color::Transparent);
target.draw(shape, states);
//подготовка рамок для отрисовки всех плашек
shape.setSize(Vector2f(CELL_SIZE - 2, CELL_SIZE - 2));
shape.setOutlineThickness(2.f);
shape.setOutlineColor(color);
shape.setFillColor(Color::Transparent);
//подготовка текстовой заготовки для отрисовки номеров плашек
Text text(" ", font, 52);
for (unsigned int i = 0; i < ARRAY_SIZE; i++)
{
shape.setOutlineColor(color);
text.setFillColor(color);
text.setString(to_string(elements[i]));
if (solved)
{
//решенную головоломку выделяем другим цветом
shape.setOutlineColor(Color::Cyan);
text.setFillColor(Color::Cyan);
}
else if (elements[i] == i + 1)
{
//номера плашек на своих местах выделяем другим цветом
text.setFillColor(Color::Green);
}
//рисуем все плашки, кроме пустой
if (elements[i] > 0)
{
//вычисление позиции плашки для отрисовки
Vector2f position(i % SIZE * CELL_SIZE + 10.f, i / SIZE * CELL_SIZE + 10.f);
shape.setPosition(position);
//отрисовываем текст
text.setPosition(position.x + 30.f + (elements[i] < 10 ? 15.f : 0.f), position.y + 25.f);
//отрисовываем рамку плашки
target.draw(shape, states);
//отрисовываем номер плашки
target.draw(text, states);
}
}
}