-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.c
108 lines (97 loc) · 3.16 KB
/
control.c
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
#include "control.h"
#define LONGBUF_SIZE 6
void main_loop(Conf *conf, Cell **table, CellState **states)
{
char buf[4];
static char longbuf[LONGBUF_SIZE];
char ok;
unsigned i;
while(1)
{
ok = 0;
/* Billentyuparancsok beolvasasa */
if (!fgets(buf, 5, stdin)) continue;
/* Harom karakteres kodok */
buf[3] = 0;
if (!strcmp(buf, "\033[A")) /* Felfele nyil */
{
conf->cursor.y = (int) conf->cursor.y - 1 >= 0 ? conf->cursor.y - 1
: conf->height - 1;
ok = 1;
}
else if (!strcmp(buf, "\033[B")) /* Lefele nyil */
{
conf->cursor.y = (conf->cursor.y + 1) % conf->height;
ok = 1;
}
else if (!strcmp(buf, "\033[C")) /* Jobbra nyil */
{
conf->cursor.x = (conf->cursor.x + 1) % conf->width;
ok = 1;
}
else if (!strcmp(buf, "\033[D")) /* Balra nyil */
{
conf->cursor.x = (int) conf->cursor.x - 1 >= 0 ? conf->cursor.x - 1
: conf->width - 1;
ok = 1;
}
/* Ket karakteres kodok */
buf[2] = 0;
/* Egy karakteres kodok */
if (buf[0] == ' ')
{
if (conf->first)
{
conf->first = 0;
states[conf->cursor.y][conf->cursor.x] = KNOWN;
/* Elso lepesnel generaljuk a tablat, h biztos ne lepjunk aknara */
gen_table(conf, table);
}
else states[conf->cursor.y][conf->cursor.x] = KNOWN;
reveal(conf, table, states, conf->cursor);
ok = 1;
}
else if (buf[0] == 'f')
{
if (states[conf->cursor.y][conf->cursor.x] == UNKNOWN)
{ /* Lerakjuk a zaszlot */
states[conf->cursor.y][conf->cursor.x] = FLAGGED;
conf->flags--;
}
else if (states[conf->cursor.y][conf->cursor.x] == FLAGGED)
{ /* Felszedjuk a zaszlot */
states[conf->cursor.y][conf->cursor.x] = UNKNOWN;
conf->flags++;
}
if (conf->flags == 0) check(conf, table, states);
}
else if (buf[0] == 'S')
{
conf->saved = save_state(conf, table, states);
if (conf->saved == 1)
{
render(conf, table, states);
return;
}
}
else if (buf[0] == 'Q') return; /* Kilepes */
if (!ok) /* Ha nem sikerult ertelmezni, betesszuk az elso karaktert
longbufba */
{
/* Elshifteljuk eggyel longbufot, es az utolso karakter helyere
beszurjuk */
for (i = 0; i < LONGBUF_SIZE - 2; i++)
longbuf[i] = longbuf[i + 1];
longbuf[LONGBUF_SIZE - 2] = buf[0];
longbuf[LONGBUF_SIZE - 1] = 0;
/* Ellenorizzuk a tobb betus kodokat */
if (!strcmp(longbuf, "iddqd"))
{
conf->iddqd = !conf->iddqd;
ok = 1;
}
}
render(conf, table, states);
if (conf->win || conf->dead) return;
}
}