-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
172 lines (148 loc) · 4.97 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
#include "Game.h"
#include <iostream>
void Game::start_new() {
srand(time(nullptr));
int x= 35;
int y = 25;
Player player(x/2, y/2);
Field field(x, y);
Level_Gen level_gen;
field = level_gen.switch_mapgen_level(player, x, y);
ComandsMediator comands_mediator;
FileInput fileinput(&comands_mediator);
ComandsHandler comands_handler(&comands_mediator);
if(!fileinput.read()) comands_mediator.set_standart_comands();
else {
comands_mediator.set_comands_handler(&comands_handler);
comands_mediator.handle_comands();
}
CommandReader command_reader(&player, &field, &comands_mediator);
Drawfield drawfield(&field);
Logger logger;
Observer observer(&player, &logger);
Message message;
GameLog gameLog(&player, &field);
ErrorLog errorLog(&field, &player, &message);
InfoLog infoLog(&field, &player);
std::cout << "Output of logging: 1 - no logs, 2 - Only file, 3 - Only consolle, 4 - 2 and 3 \n";
char answer = getchar();
bool is_logs = true;
File file;
ConsoleOutput console;
switch (answer) {
case '1':
is_logs = false;
break;
case '2':
logger.add_output(&file);
break;
case '3':
logger.add_output(&console);
break;
case '4':
logger.add_output(&file);
logger.add_output(&console);
break;
default:
std::cout<<"Incorrect, only console will be used\n";
logger.add_output(&console);
break;
}
char temp = getchar();
if(is_logs){
std::cout << "Levels of logging: 1 - game status, 2 - model changes, 3 - Errors \n";
answer = getchar();
switch (answer) {
case '1':
observer.add_level(&gameLog);
logger.add_level(&gameLog);
observer.add_level(&infoLog);
logger.add_level(&infoLog);
observer.add_level(&errorLog);
logger.add_level(&errorLog);
break;
case '2':
observer.add_level(&infoLog);
logger.add_level(&infoLog);
observer.add_level(&errorLog);
logger.add_level(&errorLog);
break;
case '3':
observer.add_level(&errorLog);
logger.add_level(&errorLog);
break;
default:
std::cout<<"Incorrect, default logger will be used\n";
observer.add_level(&errorLog);
logger.add_level(&errorLog);
break;
}
observer.check_game_status();
}
sf::RenderWindow window(sf::VideoMode(drawfield.get_window_width(), drawfield.get_window_height()), "Lab", sf::Style::Close);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
command_reader.read_from_keyboard();
if(is_logs) observer.check_game();
if(player.get_end_of_game()){
window.close();
}
window.clear(sf::Color(183,212,168));
drawfield.draw_field(window);
window.display();
sf::sleep(sf::milliseconds(100));
}
if(is_logs) observer.check_game();
class SaveField saveField;
saveField.save_field(field);
}
int Game::load_game() {
Player player(5, 5);
Field field(10, 10);
class SaveField saveField;
class SavePlayer savePlayer;
savePlayer.load_player(player);
try{
field = saveField.load_field(player);
}
catch (std::invalid_argument)
{
return -1;
}
ComandsMediator comands_mediator;
FileInput fileinput(&comands_mediator);
ComandsHandler comands_handler(&comands_mediator);
if(!fileinput.read()) comands_mediator.set_standart_comands();
else {
comands_mediator.set_comands_handler(&comands_handler);
comands_mediator.handle_comands();
}
CommandReader command_reader(&player, &field, &comands_mediator);
Drawfield drawfield(&field);
sf::RenderWindow window(sf::VideoMode(drawfield.get_window_width(), drawfield.get_window_height()), "Lab", sf::Style::Close);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
command_reader.read_from_keyboard();
if(player.get_end_of_game()){
window.close();
}
window.clear(sf::Color(183,212,168));
drawfield.draw_field(window);
window.display();
sf::sleep(sf::milliseconds(100));
}
saveField.save_field(field);
savePlayer.save_player(player);
}