-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
207 lines (166 loc) · 6.34 KB
/
main.cpp
File metadata and controls
207 lines (166 loc) · 6.34 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
200
201
202
203
204
205
206
#include <SFML/Graphics.hpp>
#include <iostream>
#include "classes/Game.h"
#include "classes/Menu.h"
int main()
{
/* MENU */
//Build window
sf::RenderWindow menu(sf::VideoMode(650, 900), "Projekt CPP", sf::Style::Close | sf::Style::Titlebar);
//Init menu
Menu main_menu;
while (menu.isOpen())
{
//Check if game started
if (main_menu.is_start_pressed()) {
menu.close();
}
sf::Event event;
while (menu.pollEvent(event))
{
if (event.type == sf::Event::Closed)
menu.close();
if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) {
main_menu.action(menu);
}
}
menu.clear(sf::Color::White);
//render objects
main_menu.draw(menu);
menu.display();
}
/* GAME */
if (!main_menu.is_start_pressed()) {
return 0;
}
//Build window
sf::RenderWindow window(sf::VideoMode(650, 900), "Projekt CPP", sf::Style::Close | sf::Style::Titlebar);
//Load fonts
sf::Font comici;
comici.loadFromFile("fonts/comici.ttf");
//Load header
sf::Text header;
header.setCharacterSize(40);
header.setFillColor(sf::Color::Black);
header.setStyle(sf::Text::Bold);
header.setFont(comici);
header.setString("Kolej na gracza:");
header.setPosition({ 50, 80 });
//Load X and O textures
sf::Texture sign_x, sign_o;
sign_x.loadFromFile("images/x.png");
sign_o.loadFromFile("images/o.png");
//Round indicator
sf::RectangleShape round_sign;
round_sign.setSize({ 80, 80 });
round_sign.setPosition({ 380, 70 });
round_sign.setTexture(&sign_o);
//Init Game object
int size = main_menu.get_size();
int win = main_menu.get_win();
int difficulty = main_menu.get_difficulty();
bool is_ai = main_menu.get_players_count() == 1 ? true : false;
Game gra(size, win, is_ai);
char current_sign = 'o';
bool game_finished = false;
Player* current_player = gra.get_player_by_sign(current_sign);
Field* ai_movement;
//Main loop
while (window.isOpen())
{
//Check if win or full
if (gra.look_for_win() || gra.get_board()->is_full()) {
game_finished = true;
if (gra.look_for_win()) {
header.setString("Wygrana gracza: ");
if (gra.get_winner()->get_symbol() == 'o') round_sign.setTexture(&sign_o);
else round_sign.setTexture(&sign_x);
}
else {
header.setString("Gra zakonczona remisem");
round_sign.setTexture(NULL);
round_sign.setFillColor(sf::Color(0, 0, 0, 0));
}
}
//Event handler
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
//Mouse click
if (event.type == sf::Event::MouseButtonPressed)
{
if (event.mouseButton.button == sf::Mouse::Left && !current_player->is_ai())
{
//Get clicked field
Field *clicked = gra.get_board()->get_clicked_field(window);
if (clicked != nullptr) {
std::cout << "Pole: " << clicked->get_x() << ", " << clicked->get_y() << " | ";
//Change owner
if (clicked->get_owner() == nullptr && !game_finished) {
std::cout << "Zmieniam wlasciciela.";
gra.set_field_owner(clicked->get_x(), clicked->get_y(), current_sign);
if (current_sign == 'x') {
current_sign = 'o';
round_sign.setTexture(&sign_o);
}
else {
current_sign = 'x';
round_sign.setTexture(&sign_x);
}
current_player = gra.get_player_by_sign(current_sign);
}
std::cout << std::endl;
continue;
}
}
}
}
//AI move
if (current_player->is_ai() && !gra.get_board()->is_full()) {
std::cout << "Inicjuje ruch AI - ";
if (difficulty == 1) {
ai_movement = gra.random_empty_field();
if (ai_movement != nullptr) {
std::cout << "Wylosowane pole: (" << ai_movement->get_x() << ", " << ai_movement->get_y() << ")" << std::endl;
gra.set_field_owner(ai_movement->get_x(), ai_movement->get_y(), current_sign);
if (current_sign == 'x') {
current_sign = 'o';
round_sign.setTexture(&sign_o);
}
else {
current_sign = 'x';
round_sign.setTexture(&sign_x);
}
current_player = gra.get_player_by_sign(current_sign);
}
}
else {
ai_movement = gra.get_ai_move(difficulty);
if (ai_movement != nullptr) {
std::cout << "Wybrane pole: (" << ai_movement->get_x() << ", " << ai_movement->get_y() << ")" << std::endl;
gra.set_field_owner(ai_movement->get_x(), ai_movement->get_y(), current_sign);
if (current_sign == 'x') {
current_sign = 'o';
round_sign.setTexture(&sign_o);
}
else {
current_sign = 'x';
round_sign.setTexture(&sign_x);
}
current_player = gra.get_player_by_sign(current_sign);
}
}
std::cout << std::endl;
continue;
}
//render windows elements
window.clear(sf::Color::White);
window.draw(header);
window.draw(round_sign);
gra.get_board()->draw_grid(window);
window.display();
}
return 0;
}