summaryrefslogtreecommitdiffstats
path: root/clui.cpp
blob: 0f431619b51c0a6cb918bf4dfc984fe418c5dd43 (plain)
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
#include <ncurses.h>
#include <string.h>
#include <stdarg.h>

#include "GameField.hpp"

#define NC_MOVE_X 2
#define NC_MOVE_Y 2
#define SYMBOL_PLAYERONE 'x'
#define SYMBOL_PLAYERTWO 'o'

const char app_name[] = "Let's play gomoku!";
const char app_tips[] = "R - restart game; ESC - exit";

enum keys {
	key_restart = 'r',
	key_escape = 27,
	key_enter = 10
};

/* clui vars */
int screen_rows = 0, screen_cols = 0;
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)
{
	move(20, 0);
	clrtoeol(); // clear old
	mvprintw(20, 0, msg);
	updateCursor();  // return cursor back
}

void help_print(const char *msg, ...)
{
	move(max_y + 2, 0);
	clrtoeol();

	va_list args;
	va_start(args, msg);
	char *str = new char[screen_cols+1];
	vsprintf(str, msg, args);
	va_end(args);

	mvprintw(max_y + 2, (screen_cols - strlen(str)) / 2, str);
	updateCursor();
	delete[] str;
}

void drawGame(int rows, int cols)
{
	getmaxyx(stdscr, screen_rows, screen_cols);

	int used_row = rows * NC_MOVE_Y - 1;
	min_y = (screen_rows - used_row) / 2;
	max_y = min_y + used_row - 1;

	int used_col = cols * NC_MOVE_X - 1;
	min_x = (screen_cols - used_col) / 2;
	max_x = min_x + used_col - 1;

	int i, j, d;
	for(j = min_y, d = 0; j <= max_y; j++, d++) {
		move(j, min_x);
		if(d % 2 == 0) {
			addch('#');
			for(i = 1; i < cols; i++) {
				addch('|');
				addch('#');
			}
		}
		else {
			addch('-');
			for(i = 1; i < cols; i++) {
				addch('|');
				addch('-');
			}
		}
	}

	mvprintw(min_y - 3, (screen_cols - strlen(app_name)) / 2, app_name);
	mvprintw(min_y - 2, (screen_cols - strlen(app_tips)) / 2, app_tips);
	help_print("MOVE: %c", SYMBOL_PLAYERONE);

	cursor_y = min_y; // + (max_y - min_y) / NC_MOVE_Y;
	cursor_x = min_x; // + (max_x - min_x) / NC_MOVE_X;
	print_symbol = SYMBOL_PLAYERONE;
	updateCursor();
}

void changePlayer()
{
	if(player_symbol == SYMBOL_PLAYERONE)
		player_symbol = SYMBOL_PLAYERTWO;
	else
		player_symbol = SYMBOL_PLAYERONE;
	help_print("MOVE: %c", player_symbol);
}

void printMove(int y, int x, int symbol)
{
	move(y, x);
	addch(symbol);
	updateCursor();
}

void gameMove()
{
	int state = game_field->GetState();
	if(state != G_NONE)
		return;

	int game_y = (cursor_y - min_y) / NC_MOVE_Y;
	int game_x = (cursor_x - min_x) / NC_MOVE_X;

	if(!game_field->CanMove(game_y, game_x)) {
		dbgprint("yet 'x' or 'o'");
		return;
	}
	game_field->Move(game_y, game_x);
	printMove(cursor_y, cursor_x, player_symbol);
	changePlayer();

	state = game_field->GetState();
	if(state == G_NONE) {
		/* if play with AI:
		ai move */
	}
	else if(state == G_DRAW)
		help_print("DRAW!");
	else if(state == G_XPLAYER)
		help_print("WINNER: %c!", SYMBOL_PLAYERONE);
	else if(state == G_OPLAYER)
		help_print("WINNER: %c!", SYMBOL_PLAYERTWO);
}

void handleButtons()
{
	int ch;
	while((ch = getch()) != key_escape && ch != KEY_RESIZE)
	{
		switch(ch) {
		case KEY_LEFT:
			if(cursor_x > min_x) cursor_x -= NC_MOVE_X;
			else dbgprint("left border");
			break;
		case KEY_DOWN:
			if(cursor_y < max_y) cursor_y += NC_MOVE_Y;
			else dbgprint("down border");
			break;
		case KEY_UP:
			if(cursor_y > min_y) cursor_y -= NC_MOVE_Y;
			else dbgprint("up border");
			break;
		case KEY_RIGHT:
			if(cursor_x < max_x) cursor_x += NC_MOVE_X; 
			else dbgprint("right border");
			break;
		case key_enter: {
			gameMove();
			break;
		}
		case key_restart: {
			delete game_field;
			drawGame(gb_y, gb_x);
			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);
	drawGame(gb_y, gb_x);
	handleButtons();

	if(game_field)
		delete game_field;
	endwin();
	return 0;
}