-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
262 lines (215 loc) · 5.68 KB
/
main.cpp
File metadata and controls
262 lines (215 loc) · 5.68 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
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
#include <unistd.h>
#include <stdio.h>
#include <curses.h>
#include <locale.h>
#include <signal.h>
#include <sys/time.h>
#include <time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include "state.h"
#include "output.h"
// Chrono
#include <chrono>
#include <thread>
int update_from_input(State &s, std::ostream &gamelog);
void finish(int sig);
/* Run the game */
void run (State &st) {
buf buf;
std::ostream gamelog (&buf); // game log
output(st, buf.data);
{ // Bot logic init
onStart(st.rows, st.cols, st.num, gamelog);
}
int k = 0;
int finished = 0;
while( !finished ) {
k++;
if (k>1) {
k=0;
// run the bot's logic and update the state
if (st.play) {
update(st, gamelog);
}
}
finished = update_from_input(st, gamelog);
output(st, buf.data);
std::this_thread::sleep_for(std::chrono::microseconds(10000));
}
}
/* Helper function, puts the value v in the interval [min, max] */
int put_in_range(int v, int min, int max) {
if (v < min)
return min;
else if (v > max)
return max;
else
return v;
}
double put_in_range_double(double v, double min, double max) {
if (v < min)
return min;
else if (v > max)
return max;
else
return v;
}
/* Main function. Mostly boilerplate code of setting up the terminal. */
int main(int argc, char* argv[]){
/* 1 2 3 4 5 6 */
/* ARGS: PART ROWS COLS NUM SEED FAST*/
/* Init the game */
char part = 'a';
int rows = 20;
int cols = 20;
int num = 1;
if (argc > 1) {
part = argv[1][0];
if (part < 'a') part = 'a';
if (part > 'c') part = 'a';
}
// Random seed
if (argc > 5 && (strcmp(argv[5], "-") != 0)) {
srand(atoi(argv[5]));
}
else {
srand(time(NULL));
rows += -2 + rand() % 5;
cols += -2 + rand() % 5;
}
switch(part) {
case 'a':
num = 1;
break;
case 'b':
num = 6;
break;
case 'c':
num = 6 + rand()%3;
break;
}
if (argc > 2 && (strcmp(argv[2], "-") != 0)) {rows = put_in_range(atoi(argv[2]), 10, 40);}
if (argc > 3 && (strcmp(argv[3], "-") != 0)) {cols = put_in_range(atoi(argv[3]), 10, 40);}
if (argc > 4 && (strcmp(argv[4], "-") != 0)) {num = put_in_range(atoi(argv[4]), 1, 10);}
// Skip the video simulation?
bool fast = (argc > 6) && (strcmp(argv[6], "fast") == 0);
// Init the game state
State st;
init(st, part, rows, cols, num);
if (!fast)
{
/* User interface initialization */
signal(SIGINT, finish);
/* ncurses initialization */
setlocale(LC_ALL, "");
initscr(); /* initialize the library and screen */
cbreak(); /* put terminal into non-blocking input mode */
nonl(); /* no NL -> CRNL on output */
noecho(); /* turn off echo */
start_color();
curs_set(0); /* hide the cursor */
timeout(0);
use_default_colors();
init_pair(0, COLOR_WHITE, COLOR_BLACK);
init_pair(1, COLOR_WHITE, COLOR_BLACK);
init_pair(2, COLOR_BLACK, COLOR_BLACK);
init_pair(3, COLOR_RED, COLOR_BLACK);
init_pair(4, COLOR_GREEN, COLOR_BLACK);
init_pair(5, COLOR_BLUE, COLOR_BLACK);
init_pair(6, COLOR_YELLOW, COLOR_BLACK);
init_pair(7, COLOR_MAGENTA, COLOR_BLACK);
init_pair(8, COLOR_CYAN, COLOR_BLACK);
init_pair(9, COLOR_BLACK, COLOR_GREEN);
init_pair(10, COLOR_WHITE, COLOR_GREEN);
init_pair(11, COLOR_RED, COLOR_GREEN);
init_pair(12, COLOR_CYAN, COLOR_BLUE);
color_set(0, NULL);
assume_default_colors(COLOR_WHITE, COLOR_BLACK);
attrset(A_NORMAL | COLOR_PAIR(0));
refresh();
clear();
/* Run the game */
run(st);
/* Restore the teminal state */
echo();
curs_set(1);
clear();
endwin();
}
else {
buf buf;
std::ostream gamelog (&buf); // game log
// init bot logic
onStart(st.rows, st.cols, st.num, gamelog);
// run the game until it's over
while(st.status == RUNNING) { update(st, gamelog); }
if (argc > 7) {
FILE *ff = fopen(argv[7], "w+");
if (st.status == SUCCESS)
fprintf(ff, "success \n");
else
fprintf(ff, "failure \n");
fprintf(ff, "Time: %i \n", st.time);
fprintf(ff, "Dwarves: %i / %i \n", count_place(st, DWARF), st.num);
fprintf(ff, "Structure: %i \n", largest_structure(st));
fprintf(ff, "Lumber: %i \n", st.lumber);
fprintf(ff, "Apples: %i \n", st.apples);
fprintf(ff, "Pumpkins: %i \n", st.pumpkins);
fprintf(ff, "\n");
fclose(ff);
}
else {
if (st.status == SUCCESS)
printf("success \n");
else
printf("failure \n");
printf("Time: %i \n", st.time);
printf("Dwarves: %i / %i \n", count_place(st, DWARF), st.num);
printf("Structure: %i \n", largest_structure(st));
printf("Lumber: %i \n", st.lumber);
printf("Apples: %i \n", st.apples);
printf("Pumpkins: %i \n", st.pumpkins);
printf("\n");
}
}
return 0;
}
int update_from_input(State &s, std::ostream &gamelog)
{
int c;
int finished=0;
while ( !finished && (c=getch()) != ERR ) {
switch(c){
case 'q': case 'Q':
finished = 1;
break;
case 'f': case 'F':
for(int i = 0; i< 60 * 3; ++i){
update(s, gamelog);
}
break;
case 's': case 'S':
s.play = false;
for(int i = 0; i<1; ++i){
update(s, gamelog);
}
break;
case 'p': case 'P':
s.play = !s.play;
break;
default:;
}
}
return finished;
}
/* SIGINT */
void finish(int sig)
{
echo();
curs_set(1);
clear();
endwin();
exit(0);
}