summaryrefslogtreecommitdiffstats
path: root/src/player/iface.c
blob: 19056f29a2859c8f5c9d42ad06a1cafa9d3cc3e9 (plain) (blame)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/******************************************************************************
 *	Copyright (C) 2015	Alejandro Colomar Andrés		      *
 ******************************************************************************/


/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
#include "mine-sweeper/player/iface.h"

#include <stddef.h>
#include <stdio.h>

#include <libalx/base/compiler/unused.h>
#include <libalx/base/stdio/printf/sbprintf.h>

#include "mine-sweeper/game/iface.h"
#include "mine-sweeper/player/clui.h"
#include "mine-sweeper/player/tui.h"


/******************************************************************************
 ******* macros ***************************************************************
 ******************************************************************************/
#define TITLE_SIZE	(20)


/******************************************************************************
 ******* variables ************************************************************
 ******************************************************************************/
/* Global --------------------------------------------------------------------*/
	int	player_iface_mode;
/* Static --------------------------------------------------------------------*/
static	struct Player_Iface_Position	player_iface_position;
static	int				player_action;


/******************************************************************************
 ******* static functions *****************************************************
 ******************************************************************************/
	/* Actions */
static	void	player_iface_act	(struct Game_Iface_In	*in);

	/* Actions:  game iface */
static	void	player_iface_act_start	(void);
static	void	player_iface_act_play	(struct Game_Iface_In	*in);
static	void	player_iface_act_game	(struct Game_Iface_In	*in);

	/* Actions:  player iface */
static	void	player_iface_move_up	(void);
static	void	player_iface_move_down	(void);
static	void	player_iface_move_right	(void);
static	void	player_iface_move_left	(void);
static	void	highlight_cursor	(void);


/******************************************************************************
 ******* global functions *****************************************************
 ******************************************************************************/
void	player_iface_init	(ptrdiff_t rows, ptrdiff_t cols)
{

	player_iface_position.rows	= rows;
	player_iface_position.cols	= cols;

	switch (player_iface_mode) {
	case PLAYER_IFACE_CLUI:
		break;
	case PLAYER_IFACE_TUI:
		player_tui_init(rows, cols);
		break;
	}
}

int	player_iface_start	(ptrdiff_t *row, ptrdiff_t *col)
{
	char	title[TITLE_SIZE];
	char	subtitle[TITLE_SIZE];
	int	fail;

	UNUSED(alx_sbprintf(title, NULL, "Start:"));
	UNUSED(alx_sbprintf(subtitle, NULL, "00:00 | 0"));

	/* Start position */
	player_iface_position.row	= 0;
	player_iface_position.col	= 0;
	player_iface_position.highlight	= false;

	/* Loop until first step */
	do {
		switch (player_iface_mode) {
		case PLAYER_IFACE_CLUI:
			player_clui_start(&player_iface_position,
					title, subtitle, &player_action);
			break;
		case PLAYER_IFACE_TUI:
			player_tui_start(&player_iface_position,
					title, subtitle, &player_action);
			break;
		}

		player_iface_act_start();
	} while ((player_action != PLAYER_IFACE_ACT_STEP)  &&
				(player_action != PLAYER_IFACE_ACT_QUIT));

	*row	= player_iface_position.row;
	*col	= player_iface_position.col;

	switch (player_action) {
	case PLAYER_IFACE_ACT_STEP:
		fail	= 0;
		break;

	case PLAYER_IFACE_ACT_QUIT:
		fail	= -1;
		break;

	default:
		fail	= -2;
		break;
	}
	return	fail;
}

void	player_iface		(const	struct Game_Iface_Out	*out,
				const	struct Game_Iface_Score	*score,
					struct Game_Iface_In	*in)
{
	char	title[TITLE_SIZE];
	char	subtitle[TITLE_SIZE];
	int	hours;
	int	mins;
	int	secs;

	/* Title */
	switch (out->state) {
	case GAME_IFACE_STATE_XYZZY:
	case GAME_IFACE_STATE_CHEATED:
	case GAME_IFACE_STATE_PLAYING:
	case GAME_IFACE_STATE_PAUSE:
		UNUSED(alx_sbprintf(title, NULL, "Mines: %i/%i",
						out->flags, out->mines));
		break;

	case GAME_IFACE_STATE_GAMEOVER:
		UNUSED(alx_sbprintf(title, NULL, "GAME OVER"));
		break;

	case GAME_IFACE_STATE_SAFE:
		UNUSED(alx_sbprintf(title, NULL, "You win!"));
		break;
	}
	/* Subtitle */
	if (score->time != CHEATED) {
		hours	= ((int)score->time / 3600);
		mins	= (((int)score->time % 3600) / 60);
		secs	= ((int)score->time % 60);

		if (score->time >= 3600) {
			UNUSED(alx_sbprintf(subtitle,NULL,"%02i:%02i:%02i | %i",
					hours, mins, secs, score->clicks));
		} else {
			UNUSED(alx_sbprintf(subtitle, NULL, "%02i:%02i | %i",
					mins, secs, score->clicks));
		}
	} else {
		UNUSED(alx_sbprintf(subtitle, NULL, "N/A"));
	}

	/* Request player action */
	switch (player_iface_mode) {
	case PLAYER_IFACE_CLUI:
		player_clui(out, &player_iface_position, title, subtitle,
					&player_action);
		break;
	case PLAYER_IFACE_TUI:
		player_tui(out, &player_iface_position, title, subtitle,
					&player_action);
		break;
	}

	player_iface_act(in);
}

void	player_iface_save_name	(const char *fpath,
				char fname[static restrict FILENAME_MAX])
{

	switch (player_iface_mode) {
	case PLAYER_IFACE_CLUI:
		player_clui_save_name(fpath, fname);
		break;
	case PLAYER_IFACE_TUI:
		player_tui_save_name(fpath, fname);
		break;
	}
}

void	player_iface_score_name	(char player_name[static restrict BUFSIZ])
{

	switch (player_iface_mode) {
	case PLAYER_IFACE_CLUI:
		player_clui_score_name(player_name);
		break;
	case PLAYER_IFACE_TUI:
		player_tui_score_name(player_name);
		break;
	}
}

void	player_iface_cleanup	(void)
{

	switch (player_iface_mode) {
	case PLAYER_IFACE_CLUI:
		break;
	case PLAYER_IFACE_TUI:
		player_tui_cleanup();
		break;
	}
	fflush(stdout);
}


/******************************************************************************
 ******* static functions *****************************************************
 ******************************************************************************/
/*	*	*	*	*	*	*	*	*	*
 *	*	* Actions	*	*	*	*	*	*
 *	*	*	*	*	*	*	*	*	*/
static	void	player_iface_act	(struct Game_Iface_In	*in)
{

	switch (player_action) {
	case PLAYER_IFACE_ACT_STEP:
	case PLAYER_IFACE_ACT_FLAG:
	case PLAYER_IFACE_ACT_FLAG_POSSIBLE:
	case PLAYER_IFACE_ACT_RM_FLAG:
		player_iface_act_play(in);
		break;
	case PLAYER_IFACE_ACT_PAUSE:
	case PLAYER_IFACE_ACT_SAVE:
	case PLAYER_IFACE_ACT_XYZZY_ON:
	case PLAYER_IFACE_ACT_XYZZY_OFF:
	case PLAYER_IFACE_ACT_XYZZY_LIN:
	case PLAYER_IFACE_ACT_XYZZY_P:
	case PLAYER_IFACE_ACT_XYZZY_NP:
	case PLAYER_IFACE_ACT_QUIT:
		player_iface_act_game(in);
		break;
	case PLAYER_IFACE_ACT_MOVE_UP:
		player_iface_move_up();
		break;
	case PLAYER_IFACE_ACT_MOVE_DOWN:
		player_iface_move_down();
		break;
	case PLAYER_IFACE_ACT_MOVE_RIGHT:
		player_iface_move_right();
		break;
	case PLAYER_IFACE_ACT_MOVE_LEFT:
		player_iface_move_left();
		break;
	case PLAYER_IFACE_ACT_HIGHLIGHT:
		highlight_cursor();
		break;
	}
}

static	void	player_iface_act_start	(void)
{

	switch (player_action) {
	case PLAYER_IFACE_ACT_STEP:
	case PLAYER_IFACE_ACT_QUIT:
		break;
	case PLAYER_IFACE_ACT_MOVE_UP:
		player_iface_move_up();
		break;
	case PLAYER_IFACE_ACT_MOVE_DOWN:
		player_iface_move_down();
		break;
	case PLAYER_IFACE_ACT_MOVE_RIGHT:
		player_iface_move_right();
		break;
	case PLAYER_IFACE_ACT_MOVE_LEFT:
		player_iface_move_left();
		break;
	case PLAYER_IFACE_ACT_HIGHLIGHT:
		highlight_cursor();
		break;
	}
}

/*	*	*	*	*	*	*	*	*	*
 *	*	* Actions:  game iface	*	*	*	*	*
 *	*	*	*	*	*	*	*	*	*/
static	void	player_iface_act_play	(struct Game_Iface_In	*in)
{
	const int	r = player_iface_position.row;
	const int	c = player_iface_position.col;

	switch (player_action) {
	case PLAYER_IFACE_ACT_STEP:
		in->act_game[r][c]	= GAME_IFACE_GAME_ACT_STEP;
		break;
	case PLAYER_IFACE_ACT_FLAG:
		in->act_game[r][c]	= GAME_IFACE_GAME_ACT_FLAG;
		break;
	case PLAYER_IFACE_ACT_FLAG_POSSIBLE:
		in->act_game[r][c]	= GAME_IFACE_GAME_ACT_FLAG_POSSIBLE;
		break;
	case PLAYER_IFACE_ACT_RM_FLAG:
		in->act_game[r][c]	= GAME_IFACE_GAME_ACT_RM_FLAG;
		break;
	}

	in->action	= GAME_IFACE_ACT_PLAY;
}

static	void	player_iface_act_game	(struct Game_Iface_In	*in)
{

	switch (player_action) {
	case PLAYER_IFACE_ACT_PAUSE:
		in->action	= GAME_IFACE_ACT_PAUSE;
		break;
	case PLAYER_IFACE_ACT_SAVE:
		in->action	= GAME_IFACE_ACT_SAVE;
		break;
	case PLAYER_IFACE_ACT_XYZZY_ON:
		in->action	= GAME_IFACE_ACT_XYZZY_ON;
		break;
	case PLAYER_IFACE_ACT_XYZZY_OFF:
		in->action	= GAME_IFACE_ACT_XYZZY_OFF;
		break;
	case PLAYER_IFACE_ACT_XYZZY_LIN:
		in->action	= GAME_IFACE_ACT_XYZZY_LIN;
		break;
	case PLAYER_IFACE_ACT_XYZZY_P:
		in->action	= GAME_IFACE_ACT_XYZZY_P;
		break;
	case PLAYER_IFACE_ACT_XYZZY_NP:
		in->action	= GAME_IFACE_ACT_XYZZY_NP;
		break;
	case PLAYER_IFACE_ACT_QUIT:
		in->action	= GAME_IFACE_ACT_QUIT;
		break;
	}
}

/*	*	*	*	*	*	*	*	*	*
 *	*	* Actions:  player iface	*	*	*	*
 *	*	*	*	*	*	*	*	*	*/
static	void	player_iface_move_up	(void)
{

	if (player_iface_position.row)
		(player_iface_position.row)--;
	else
		player_iface_position.row	= player_iface_position.rows - 1;
}

static	void	player_iface_move_down	(void)
{

	if (player_iface_position.row != player_iface_position.rows - 1)
		(player_iface_position.row)++;
	else
		player_iface_position.row	= 0;
}

static	void	player_iface_move_right	(void)
{

	if (player_iface_position.col != player_iface_position.cols - 1)
		(player_iface_position.col)++;
	else
		player_iface_position.col	= 0;
}

static	void	player_iface_move_left	(void)
{

	if (player_iface_position.col)
		(player_iface_position.col)--;
	else
		player_iface_position.col	= player_iface_position.cols - 1;
}

static	void	highlight_cursor	(void)
{

	player_iface_position.highlight	= !player_iface_position.highlight;
}


/******************************************************************************
 ******* end of file **********************************************************
 ******************************************************************************/