~vbkaisetsu/+junk/termsweeper

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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * minesweeper.c
 * Copyright (C) Koichi Akabe 2011 <vbkaisetsu@gmail.com>
 * 
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * tertris is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Thanks Takeru Amano.
 */

#include <curses.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define NAME_MAX_LEN 10
#define RANKING_FILE ".terminesweeper"

typedef struct
{
	char name[NAME_MAX_LEN + 1];
	int time;
} gameranking;

void create_minefield(char m[], int size, int mines, int start_point)
{
	int i, j;
	char tmp;
	size--;
	srand((unsigned) time(NULL));
	for(i = 0; i < mines; i++)
		m[i] = 1;
	for(i = mines; i < size; i++)
		m[i] = 0;
	/* randomize (Fisher–Yates shuffle) */
	for(i = size-1; i >= 0; i--)
	{
		j = rand() % (i+1);
		tmp = m[i];
		m[i] = m[j];
		m[j] = tmp;
	}
	m[size] = m[start_point];
	m[start_point] = 0;
}

int draw_start_scr()
{
	if(initscr() == NULL)
	{
		fprintf(stderr, "initscr failure\n");
		return 1;
	}
	/* check screen size */
	if(COLS < 23 || LINES < 14)
	{
		endwin();
		fprintf(stderr, "screen size is too small (at least: %dx%d)\n", 23, 14);
		return 1;
	}
	mvprintw(0,  0, "+---------------------+");
	mvprintw(1,  0, "|                     |");
	mvprintw(2,  0, "|     Termsweeper     |");
	mvprintw(3,  0, "|                     |");
	mvprintw(4,  0, "|  a. 10x10 (10mines) |");
	mvprintw(5,  0, "|  b. 16x16 (40mines) |");
	mvprintw(6,  0, "|  c. 30x16 (99mines) |");
	mvprintw(7,  0, "|  q.    Q u i t      |");
	mvprintw(8,  0, "|                     |");
	mvprintw(9,  0, "|       RANKING       |");
	mvprintw(10, 0, "| 1:                  |");
	mvprintw(11, 0, "| 2:                  |");
	mvprintw(12, 0, "| 3:                  |");
	mvprintw(13, 0, "+---------------------+");
	refresh();
	return 0;
}

void update_field(char hints[], int width, int height)
{
	int i, j;
	for(i = 0; i < height; i++)
	{
		for(j = 0; j < width; j++)
		{
			switch(hints[i*width+j])
			{
			case -1:
				mvprintw(i+1, j*2+2, "."); break;
			case 0:
				mvprintw(i+1, j*2+2, " "); break;
			case 10:
				mvprintw(i+1, j*2+2, "X"); break;
			default:
				mvprintw(i+1, j*2+2, "%d", hints[i*width+j]); break;
			}
		}
	}
	refresh();
}

void show_mines(char m[], int width, int height)
{
	int i, j;
	for(i = 0; i < height; i++)
	{
		for(j = 0; j < width; j++)
		{
			if(m[i*width+j] == 1)
				mvprintw(i+1, j*2+2, "*");
		}
	}
	refresh();
}

int draw_game_scr(int width, int height)
{
	int i, j;
	if(initscr() == NULL)
	{
		fprintf(stderr, "initscr failure\n");
		return 1;
	}
	if(COLS < width * 2 + 3 || LINES < height + 4)
	{
		endwin();
		fprintf(stderr, "screen size is too small (at least: %dx%d)\n", width*2+3, height+4);
		return 1;
	}
	mvprintw(0, 0, "+-");			mvprintw(0, width*2+2, "+");
	mvprintw(height+1, 0, "+-");	mvprintw(height+1, width*2+2, "+");
	mvprintw(height+2, 0, "| ");	mvprintw(height+2, width*2+2, "|");
	mvprintw(height+3, 0, "+-");	mvprintw(height+3, width*2+2, "+");
	for(i = 0; i < width; i++)
	{
		mvprintw(0, i*2+2, "--");
		mvprintw(height+1, i*2+2, "--");
		mvprintw(height+2, i*2+2, "  ");
		mvprintw(height+3, i*2+2, "--");
	}
	for(i = 0; i < height; i++)
	{
		mvprintw(i+1, 0, "| ");
		mvprintw(i+1, width*2+2, "|");
		for(j = 0; j < width; j++)
		{
			mvprintw(i+1, j*2+2, ". ");
		}
	}
	mvprintw(height+2, 2, "Time:  0:00");
	refresh();
	return 0;
}

int save_ranking(gameranking *ranking)
{
	int error = 0;
	char *savepath, *homedir;
	homedir = getenv("HOME");
	if(homedir)
	{
		savepath = malloc(strlen(homedir) + strlen(RANKING_FILE) + 2);
		sprintf(savepath, "%s/"RANKING_FILE, homedir);
	}
	else
	{
		savepath = malloc(strlen(RANKING_FILE) + 1);
		strcpy(savepath, RANKING_FILE);
	}
	FILE *fp = fopen(savepath, "wb");
	if(fp)
	{
		if(fwrite(ranking, sizeof(gameranking), 9, fp) != 9)
			error = 1;
		fclose(fp);
	}
	else
		error = 1;
	free(savepath);
	return error;
}

int read_ranking(gameranking *ranking)
{
	int i, error = 0;
	char *savepath, *homedir;
	homedir = getenv("HOME");
	if(homedir)
	{
		savepath = malloc(strlen(homedir) + strlen(RANKING_FILE) + 2);
		sprintf(savepath, "%s/"RANKING_FILE, homedir);
	}
	else
	{
		savepath = malloc(strlen(RANKING_FILE) + 1);
		strcpy(savepath, RANKING_FILE);
	}
	FILE *fp = fopen(savepath, "rb");
	if(fp)
	{
		if(fread(ranking, sizeof(gameranking), 9, fp) != 9)
			error = 1;
		fclose(fp);
	}
	else
		error = 1;
	if(error)
	{
		for(i = 0; i < 9; i++)
		{
			ranking[i].name[0] = 0;
			ranking[i].time = -1;
		}
	}
	free(savepath);
	return error;
}

int open_minefield(char m[], char hints[], int tmp[], int width, int height, int point)
{
	int op_end = 1;
	int i = 0, x, y;
	char mcnt;
	if(m[point] == 1)
		return 1;
	tmp[0] = point;
	while(i < op_end)
	{
		mcnt = 0;
		x = tmp[i] % width;
		y = tmp[i] / width;
		/* get a number of mines around the area */
		if(x != 0)
		{
			mcnt = m[tmp[i] - 1];
			if(y != 0)
				mcnt += m[tmp[i] - width - 1];
			if(y != height - 1)
				mcnt += m[tmp[i] + width - 1];
		}
		if(y != 0)
			mcnt += m[tmp[i] - width];
		if(y != height - 1)
			mcnt += m[tmp[i] + width];
		if(x != width - 1)
		{
			mcnt += m[tmp[i] + 1];
			if(y != 0)
				mcnt += m[tmp[i] - width + 1];
			if(y != height - 1)
				mcnt += m[tmp[i] + width + 1];
		}
		hints[tmp[i]] = mcnt;
		if(mcnt == 0)
		{
			/* open areas around the selected one */
			if(x != 0)
			{
				if(hints[tmp[i] - 1] == -1)
				{
					tmp[op_end++] = tmp[i] - 1;
					hints[tmp[i] - 1] = -2;
				}
				if(y != 0)
				{
					if(hints[tmp[i] - width - 1] == -1)
					{
						tmp[op_end++] = tmp[i] - width - 1;
						hints[tmp[i] - width - 1] = -2;
					}
				}
				if(y != height - 1)
				{
					if(hints[tmp[i] + width - 1] == -1)
					{
						tmp[op_end++] = tmp[i] + width - 1;
						hints[tmp[i] + width - 1] = -2;
					}
				}
			}
			if(y != 0)
			{
				if(hints[tmp[i] - width] == -1)
				{
					tmp[op_end++] = tmp[i] - width;
					hints[tmp[i] - width] = -2;
				}
			}
			if(y != height - 1)
			{
				if(hints[tmp[i] + width] == -1)
				{
					tmp[op_end++] = tmp[i] + width;
					hints[tmp[i] + width] = -2;
				}
			}
			if(x != width - 1)
			{
				if(hints[tmp[i] + 1] == -1)
				{
					tmp[op_end++] = tmp[i] + 1;
					hints[tmp[i] + 1] = -2;
				}
				if(y != 0)
				{
					if(hints[tmp[i] - width + 1] == -1)
					{
						tmp[op_end++] = tmp[i] - width + 1;
						hints[tmp[i] - width + 1] = -2;
					}
				}
				if(y != height - 1)
				{
					if(hints[tmp[i] + width + 1] == -1)
					{
						tmp[op_end++] = tmp[i] + width + 1;
						hints[tmp[i] + width + 1] = -2;
					}
				}
			}
		}
		i++;
	}
	/* check completion */
	for(i = 0; i < width*height; i++)
	{
		if(m[i] == 0)
			if(hints[i] == -1)
				return 0;
	}
	return -1;
}

int ask_name(char name[], int play_time, int width, int height)
{
	int min, sec, i;
	min = play_time / 60;
	sec = play_time % 60;
	mvprintw(height/2-2, width-8, "+-----------------+");
	mvprintw(height/2-1, width-8, "|   High  Score   |");
	mvprintw(height/2  , width-8, "|   Time: %2d:%02d   |", min, sec);
	mvprintw(height/2+1, width-8, "| Name:           |");
	mvprintw(height/2+2, width-8, "|    < Enter >    |");
	mvprintw(height/2+3, width-8, "+-----------------+");
	move(height/2+1, width-1);
	i = 0;
	char name_tmp[NAME_MAX_LEN+1];
	name_tmp[0] = 0;
	int pressedkey;
	while(1)
	{
		pressedkey = getch();
		if(pressedkey == '\n')
			break;
		else if(0x20 <= pressedkey && pressedkey <= 0x7f && i < NAME_MAX_LEN)
		{
			mvprintw(height/2+1, width-1 + i, "%c", (char)pressedkey);
			name_tmp[i] = (char)pressedkey;
			name_tmp[i+1] = 0;
			i++;
		}
		else if(pressedkey == KEY_BACKSPACE && i > 0) /* back space */
		{
			i--;
			mvprintw(height/2+1, width-1 + i, " ");
			move(height/2+1, width-1 + i);
			name_tmp[i] = 0;
		}
	}
	if(i == 0)
		return 1;
	strcpy(name, name_tmp);
	return 0;
}

void start_game(int selected_game, gameranking *ranking)
{
	int width, height, mines;
	switch(selected_game)
	{
	case 0:
		width = height = 10;
		mines = 10;
		break;
	case 1:
		width = 16;
		height = 16;
		mines = 40;
		break;
	case 2:
		width = 30;
		height = 16;
		mines = 99;
		break;
	default:
		return;
	}
	endwin();
	/* initialize */
	if(draw_game_scr(width, height) != 0)
		return;
	noecho();
	keypad(stdscr, TRUE);
	char *field = NULL;
	char *hints = malloc(sizeof(char) * width * height);
	memset(hints, -1, width * height);
	int *tmp = malloc(sizeof(int) * width * height);
	int x = 0, y = 0, play_time = 0;
	time_t time_start, time_now;
	time_start = time(NULL);
	timeout(10);
	/* wait key */
	while(1)
	{
		move(y + 1, x * 2 + 2);
		int pressedkey = getch();
		switch(pressedkey)
		{
		case 'q':
			timeout(-1);
			free(hints);
			free(tmp);
			erase();
			endwin();
			return;
		case 'h':
		case KEY_LEFT:
			if(--x < 0)
				x = width - 1;
			break;
		case 'j':
		case KEY_DOWN:
			if(++y >= height)
				y = 0;
			break;
		case 'k':
		case KEY_UP:
			if(--y < 0)
				y = height - 1;
			break;
		case 'l':
		case KEY_RIGHT:
			if(++x >= width)
				x = 0;
			break;
		case '\n':
			if(field == NULL)
			{
				field = malloc(sizeof(char) * width * height);
				create_minefield(field, width*height, mines, width * y + x);
			}
			if(hints[width * y + x] != 10)
			{
				int r = open_minefield(field, hints, tmp, width, height, width * y + x);
				if(r == 1)
				{
					show_mines(field, width, height);
					timeout(-1);
					mvprintw(height + 2, 14, "GAMEOVER");
					move(height + 2, 14);
					getch();
					free(hints);
					free(tmp);
					erase();
					endwin();
					return;
				}
				update_field(hints, width, height);
				if(r == -1)
				{
					timeout(-1);
					mvprintw(height + 2, 14, "SUCCESS!");
					move(height + 2, 14);
					getch();
					int rank, i;
					char name[NAME_MAX_LEN+1];
					gameranking *ranking_t = ranking + 3*selected_game;
					for(rank = 0; rank < 3; rank++)
						if(ranking_t[rank].time > play_time || ranking_t[rank].time == -1)
							break;
					if(rank < 3)
					{
						if(ask_name(name, play_time, width, height) == 0)
						{
							for(i = 2; rank < i; i--)
							{
								strcpy(ranking_t[i].name, ranking_t[i - 1].name);
								ranking_t[i].time = ranking_t[i - 1].time;
							}
							strcpy(ranking_t[rank].name, name);
							ranking_t[rank].time = play_time;
							save_ranking(ranking);
						}
					}
					free(hints);
					free(tmp);
					erase();
					endwin();
					return;
				}
			}
			break;
		case ' ':
			if(hints[width * y + x] == -1)
				hints[width * y + x] = 10;
			else if(hints[width * y + x] == 10)
				hints[width * y + x] = -1;
			update_field(hints, width, height);
		default:
			break;
		}
		time_now = time(NULL);
		play_time = time_now - time_start;
		int min, sec;
		min = play_time / 60;
		sec = play_time % 60;
		if(min > 99)
		{
			/* force exit */
			show_mines(field, width, height);
			timeout(-1);
			mvprintw(height + 2, 14, "GAMEOVER");
			move(height + 2, 14);
			getch();
			free(hints);
			free(tmp);
			erase();
			endwin();
			return;
		}
		mvprintw(height + 2, 8, "%2d:%02d", min, sec);
	}
}

int main(int argc, char *argv[])
{
	int i;
	gameranking ranking[9];
	srand((unsigned) time(NULL));
	while(1)
	{
		int next_game = 0;
		/* draw game window */
		if(draw_start_scr() != 0)
			return 0;
		noecho();
		keypad(stdscr, TRUE);
		read_ranking(ranking);
		int selected_game = 0;
		/* wait key */
		while(1)
		{
			/* draw ranking */
			for(i = 0; i < 3; i++)
				mvprintw(10+i, 5, "           --:--");
			if(selected_game < 3)
			{
				for(i = 0; i < 3; i++)
				{
					if(ranking[i+selected_game*3].name[0] != 0)
					{
						mvprintw(10+i, 5, "%s", ranking[i+selected_game*3].name);
						int min, sec;
						min = ranking[i+selected_game*3].time / 60;
						sec = ranking[i+selected_game*3].time % 60;
						mvprintw(10+i, 16, "%2d:%02d", min, sec);
					}
				}
			}
			refresh();
			move(4+selected_game, 3);
			int pressedkey = getch();
			switch(pressedkey)
			{
			case 'a':
				selected_game = 0;
				break;
			case 'b':
				selected_game = 1;
				break;
			case 'c':
				selected_game = 2;
				break;
			case 'q':
				selected_game = 3;
				break;
			case 'k':
			case KEY_UP:
				if(--selected_game < 0)
					selected_game = 3;
				break;
			case 'j':
			case KEY_DOWN:
				if(++selected_game > 3)
					selected_game = 0;
				break;
			case '\n':
				if(selected_game == 3)
				{
					erase();
					refresh();
					endwin();
					return 0;
				}
				else
				{
					erase();
					endwin();
					start_game(selected_game, ranking);
					next_game = 1;
				}
			default:
				break;
			}
			if(next_game)
				break;
		}
	}
	return 0;
}