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
|
#include <ncurses.h>
#include "GameField.hpp"
#define SYMBOL_PLAYERONE 'x'
#define SYMBOL_PLAYERTWO 'o'
enum keys {
key_restart = 'r',
key_escape = 27,
key_enter = 10
};
/* clui vars */
int min_y, max_y, min_x, max_x;
/* game vars */
GameField *game_field;
/* players vars */
int cursor_y, cursor_x;
char player_symbol;
/* command line options */
int gb_y = 3;
int gb_x = 3;
int gb_lwin = 3;
int gb_symbol = SYMBOL_PLAYERONE;
void updateCursor()
{
move(cursor_y, cursor_x);
refresh();
}
void dbgprint(const char *msg)
{
mvprintw(10, 0, " "); // clear old
mvprintw(10, 0, msg);
updateCursor(); // return cursor back
refresh();
}
void drawGame(int cols, int rows, int symbol)
{
/* MAKE: draw for custom cols and rows */
mvprintw(0, 0, "012");
mvprintw(1, 0, "345");
mvprintw(2, 0, "678");
min_y = 0;
max_y = cols-1;
min_x = 0;
max_x = rows-1;
cursor_y = max_y / 2;
cursor_x = max_x / 2;
player_symbol = symbol;
updateCursor();
}
void changePlayer()
{
if(player_symbol == SYMBOL_PLAYERONE)
player_symbol = SYMBOL_PLAYERTWO;
else
player_symbol = SYMBOL_PLAYERONE;
}
void printMove(int y, int x, int symbol)
{
move(y, x);
addch(symbol);
updateCursor();
}
void handleMove()
{
if(!game_field->CanMove(cursor_y, cursor_x)) {
dbgprint("yet 'x' or 'o'");
return;
}
game_field->Move(cursor_y, cursor_x);
printMove(cursor_y, cursor_x, player_symbol);
changePlayer();
int state = game_field->GetState();
if(state == G_NONE) {
/* if play with AI:
ai move */
}
else if(state == G_DRAW)
dbgprint("DRAW!");
else if(state == G_XPLAYER)
dbgprint("PLAYER 'X' WIN!");
else if(state == G_OPLAYER)
dbgprint("PLAYER 'O' WIN!");
}
void handleButtons()
{
int ch;
while((ch = getch()) != key_escape)
{
switch(ch)
{
case KEY_LEFT:
if(cursor_x != min_x) {
cursor_x -= 1;
}
else dbgprint("left border");
break;
case KEY_DOWN:
if(cursor_y != max_y) {
cursor_y += 1;
}
else dbgprint("down border");
break;
case KEY_UP:
if(cursor_y != min_y) {
cursor_y -= 1;
}
else dbgprint("up border");
break;
case KEY_RIGHT:
if(cursor_x != max_x) {
cursor_x += 1;
}
else dbgprint("right border");
break;
case key_enter: {
handleMove();
break;
}
case key_restart: {
delete game_field;
drawGame(gb_y, gb_x, gb_symbol);
game_field = new GameField(gb_y, gb_x, gb_lwin);
// no break
}
default:
continue;
}
updateCursor();
}
}
int main(int argc, char *argv[])
{
/* ncurses settings */
initscr();
keypad(stdscr, TRUE);
noecho();
/* command line options */
/* ... */
game_field = new GameField(gb_y, gb_x, gb_lwin);
drawGame(gb_y, gb_x, gb_symbol);
handleButtons();
if(game_field)
delete game_field;
endwin();
return 0;
}
|