File: play.c

package info (click to toggle)
freesweep 0.88-4.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 404 kB
  • ctags: 267
  • sloc: ansic: 3,813; sh: 2,460; makefile: 112
file content (572 lines) | stat: -rw-r--r-- 11,118 bytes parent folder | download | duplicates (2)
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
/**********************************************************************
*  This source code is copyright 1999 by Gus Hartmann & Peter Keller  *
*  It may be distributed under the terms of the GNU General Purpose   *
*  License, version 2 or above; see the file COPYING for more         *
*  information.                                                       *
*                                                                     *
*  $Id: play.c,v 1.38 2000/11/02 03:48:14 hartmann Exp $
*                                                                     *
**********************************************************************/

#include "sweep.h"

static int LastNum;
static int LastKey;

int GetInput(GameStats* Game)
{
	unsigned char RetVal;
	int Multiplier=1;
	int UserInput=0;
	char *pathname = NULL;

#ifdef SWEEP_MOUSE
	MEVENT MouseInput;
#endif /* SWEEP_MOUSE */

	UserInput=getch();
	
	/* Check to see if it's a number; if so build up the multiplier. */
	/* '0' isn't a valid first digit, since it means "go to the origin" */
	/* '0' will be valid once there is a different first digit. */
	if ((UserInput > '0')&&(UserInput <= '9'))
	{
		Multiplier=(UserInput-'0');
		UserInput=getch();
		while ((UserInput >= '0')&&(UserInput <= '9'))
		{
			Multiplier=((Multiplier*10)+(UserInput-'0'));
			UserInput=getch();
		}
	}

	if (UserInput=='.')
	{
		UserInput=LastKey;
		Multiplier=(Multiplier*LastNum);
	}
	else
	{
		LastKey=UserInput;
		LastNum=Multiplier;
	}

	switch (UserInput)
	{
		/* Going Left? */
		case 'h':
		case KEY_LEFT:
			MoveLeft(Game,Multiplier);
			break;

		/* Going Down? */
		case 'j':
		case KEY_DOWN:
			MoveDown(Game,Multiplier);
			break;

		/* Going Up? */
		case 'k':
		case KEY_UP:
			MoveUp(Game,Multiplier);
			break;

		/* Going Right? */
		case 'l':
		case KEY_RIGHT:
			MoveRight(Game,Multiplier);
			break;


		/* Diagonals. */
		case KEY_A1:
			MoveUp(Game,Multiplier);
			MoveLeft(Game,Multiplier);
			break;

		case KEY_A3:
			MoveUp(Game,Multiplier);
			MoveRight(Game,Multiplier);

			break;

		case KEY_C1:
			MoveDown(Game,Multiplier);
			MoveLeft(Game,Multiplier);
			break;

		case KEY_C3:
			MoveDown(Game,Multiplier);
			MoveRight(Game,Multiplier);
			break;

		/* The non-motion keys. */
		/* The accepted values for flagging a space as a mine. */
		case 'f':
		case KEY_B2:
			if (Multiplier!=1)
			{
				SweepError("Can only mark the mine once.");
				Multiplier=1;
			}
			GetMine(Game->CursorX,Game->CursorY,RetVal);
			switch (RetVal)
			{
				case MINE:
					SetMine(Game->CursorX,Game->CursorY,MARKED);
					Game->MarkedMines++;
					break;
				case MARKED:
					SetMine(Game->CursorX,Game->CursorY,MINE);
					Game->MarkedMines--;
					break;
				case BAD_MARK:
					SetMine(Game->CursorX,Game->CursorY,UNKNOWN);
					Game->BadMarkedMines--;
					break;
				case UNKNOWN:
					SetMine(Game->CursorX,Game->CursorY,BAD_MARK);
					Game->BadMarkedMines++;
					break;
				default:
					SweepError("Cannot mark as a mine.");
					break;
			}

			if (Game->MarkedMines == Game->NumMines && 
				Game->BadMarkedMines == 0)
			{
				StopTimer();
/*				YouWin();*/
				Game->Status = WIN;
#ifdef DEBUG_LOG
				fprintf(DebugLog, "Num %d, Marked %d, Bad %d\n", 
					Game->NumMines, Game->MarkedMines, Game->BadMarkedMines);
				fflush(DebugLog);
#endif /* DEBUG_LOG */
				pathname = FPTBTF();
				UpdateBestTimesFile(Game, pathname);
				free(pathname);

				#ifdef USE_GROUP_BEST_FILE
					pathname = FPTGBTF();
					UpdateBestTimesFile(Game, pathname);
					free(pathname);
				#endif

			}
			StartTimer();
			break;
		
		/* the accepted key to make a 'new' game */
		case 'n':
			if (Multiplier != 1)
			{
				SweepError("Can only make a new game once.");
				Multiplier=1;
			}
			Game->Status = ABORT;
			break;
		
		/* The accepted key to make a reconfigure if the game */
		case 'x':
			if (Multiplier != 1)
			{
				SweepError("Can only reconfigure game once.");
				Multiplier=1;
			}
			Game->Status = RECONF;
			break;

		/* The accepted key to open the save gui */
		case 'w':
			if (Multiplier != 1)
			{
				SweepError("Can only save game once.");
				Multiplier=1;
			}
			FSGUI();
		break;

		/* The accepted keys to expose a space. */
		case ' ':
		case KEY_IC:
			if (Multiplier!=1)
			{
				SweepError("Can only expose the square once.");
				Multiplier=1;
			}
			GetMine(Game->CursorX,Game->CursorY,RetVal);
			switch (RetVal)
			{
				case BAD_MARK:
				case MARKED:
					SweepError("Cannot expose a flagged mine.");
					break;
				case MINE:
					StopTimer();
					/* BOOM! */
/*					Boom();*/
					SetMine(Game->CursorX,Game->CursorY,DETONATED);
					CharSet.FalseMark='x';
					CharSet.Mine='o';
					Game->Status=LOSE;
#ifdef DEBUG_LOG
					fprintf(DebugLog,"Mine exposed! Setting Status to LOSE.\n");
					fflush(DebugLog);
#endif /* DEBUG_LOG */
/*					StartTimer();*/
					break;
				case UNKNOWN:
					Clear(Game);
					break;
				case EMPTY:
					SweepError("Square already exposed.");
					break;
				case 1: case 2: case 3: case 4:
				case 5: case 6: case 7: case 8:
					/* Double-click */
					SuperClear(Game);

					break;
				default:
					break;
			}
			break;

		/* The accepted keys to redraw the screen. */
		case 'r':
		case KEY_REFRESH:
		case '\f':
			if (Multiplier!=1)
			{
				SweepError("Can only refresh the display once.");
				Multiplier=1;
			}
			PrintInfo();
			RedrawStatsWin();
			RedrawErrorWin();
			touchwin(Game->Border);
			touchwin(Game->Board);

			noutrefresh();

			break;

		/* The accepted keys to display the help screen. */
		case '?':
		case KEY_HELP:
			if (Multiplier!=1)
			{
				SweepError("Can only display help screen once.");
				Multiplier=1;
			}
			StopTimer();
			Help();
			PrintInfo();
			RedrawErrorWin();
			RedrawStatsWin();
			StartTimer();
			break;

		/* The accepted keys to display the license screen. */
		case 'g':
			if (Multiplier!=1)
			{
				SweepError("Can only display GNU GPL once.");
				Multiplier=1;
			}
			PrintGPL();
			PrintInfo();
			RedrawErrorWin();
			RedrawStatsWin();
			break;

		/* The accepted keys to display the best times screen. */
		case 'b':
			if (Multiplier!=1)
			{
				SweepError("Can only display best times once.");
				Multiplier=1;
			}
			PrintBestTimes(NULL);
			PrintInfo();
			RedrawErrorWin();
			RedrawStatsWin();
			noutrefresh();
			doupdate();
			break;
		
		/* The accepted values to center the cursor on board */
		case 'c':
			if (Multiplier!=1)
			{
				SweepError("Can only center cursor once.");
				Multiplier=1;
			}
			Game->CursorX=Game->Width/2;
			Game->CursorY=Game->Height/2;
			break;

		/* A test key. Sort of feature-of-the-day. */
		case 't':
			if (Multiplier!=1)
			{
				SweepError("Can only <feature> once.");
				Multiplier=1;
			}
			SweepMessage("_________0_________0(%u, %u)_________0_________0", Game->CursorX, Game->CursorY);
			break;

		/* The accepted values to suspend the game. */
		case KEY_SUSPEND:
		case 'z':
			if (Multiplier!=1)
			{
				SweepError("Can only suspend the program once.");
				Multiplier=1;
			}

			break;

		case 'q':
			if (Multiplier!=1)
			{
				SweepError("Can only quit once.");
				Multiplier=1;
			}

#ifdef DEBUG_LOG
			fprintf(DebugLog,"Quitting quietly.\n========================================\n");
			fclose(DebugLog);
#endif /* DEBUG_LOG */
			clear();
			refresh();
			endwin();
			exit(EXIT_SUCCESS);
			break;

		case '0':
			if (Multiplier!=1)
			{
				SweepError("Can only return to the origin once.");
				Multiplier=1;
			}
			Game->CursorY=0;
			Game->CursorX=0;
			break;

		case '$':
			if (Multiplier!=1)
			{
				SweepError("Can only go to the end once.");
				Multiplier=1;
			}
			Game->CursorY=Game->Height-1;
			Game->CursorX=Game->Width-1;
			break;

		case 'a':
			/* FOO - needs to redraw everything. */
			if (Multiplier!=1)
			{
				if ((Multiplier%2)!=0)
				{
					SweepMessage("Toggling character set.");
					SwitchCharSet(Game);
					PrintInfo();
					RedrawErrorWin();
					RedrawStatsWin();
				}
				else
				{
					SweepMessage("Not toggling character set.");
				}
			}
			else
			{
				SweepMessage("Toggling character set.");
				SwitchCharSet(Game);
				PrintInfo();
				RedrawErrorWin();
				RedrawStatsWin();
			}
			break;

		case 'd':
				DumpGame(Game);
			break;

#ifdef SWEEP_MOUSE
		case KEY_MOUSE:
			if (NCURSES_MOUSE_VERSION==1)
			{
				if (getmouse(&MouseInput)==ERR)
				{

				}

			}
			break;
#endif /* SWEEP_MOUSE */

#ifdef DEBUG_LOG
		case '|':
			fprintf(DebugLog,"Entering cheat mode.\n");
			CharSet.Mine='+';
			break;
#endif /* DEBUG_LOG */

		/* XXX Save a game */
		case 'p':
			SaveGame(Game, "./foo.svg");
			SweepError("Done Saving");
			break;

		/* XXX load a game */
		case 'o':
			werase(Game->Board);
			wnoutrefresh(Game->Board);
			werase(Game->Border);
			wnoutrefresh(Game->Border);
			delwin(Game->Board);
			delwin(Game->Border);
			free(Game->Field);
			free(Game);
			Game = LoadGame("./foo.svg");
			SweepError("Done Loading");
			break;

		case 'i':
			SaveGameImage(Game, "foo.ppm");
			break;

		case ERR:
			return 1;
			break;

		default:
#ifdef DEBUG_LOG
			fprintf(DebugLog,"Got unknown keystroke: %c\n", UserInput);
#endif /* DEBUG_LOG */
			LastNum=1;
			LastKey=ERR;
			SweepError("Bad Input.");
			return 1;
			break;
	}

	LastNum=Multiplier;
	LastKey=UserInput;

	return 0;
}

void MoveLeft(GameStats* Game,int Num)
{
	if (Game->CursorX >= Num)
	{
		Game->CursorX-=Num;
	}
	else
	{
		SweepError("Cannot move past left side of board.");
		Game->CursorX=0;
	}
	return;
}

void MoveRight(GameStats* Game,int Num)
{
	if ((Game->CursorX + Num) < Game->Width)
	{
		Game->CursorX+=Num;
	}
	else
	{
		SweepError("Cannot move past right side of board.");
		Game->CursorX=(Game->Width-1);
	}
	return;
}

void MoveUp(GameStats* Game,int Num)
{
	if (Game->CursorY >= Num)
	{
		Game->CursorY-=Num;
	}
	else
	{
		SweepError("Cannot move past top of board.");
		Game->CursorY=0;
	}
	return;
}

void MoveDown(GameStats* Game,int Num)
{
	if ((Game->CursorY + Num) < Game->Height)
	{
		Game->CursorY+=Num;
	}
	else
	{
		SweepError("Cannot move past bottom of board.");
		Game->CursorY=(Game->Height-1);
	}
	return;
}

void Boom(void)
{
	WINDOW* BoomWin;

	if ((BoomWin=newwin(LINES/3,COLS/3,LINES/3,COLS/3))==NULL)
	{
		perror("Boom::Alloc Window");
		return;
	}

	if (wborder(BoomWin,CharSet.Mark,CharSet.Mark,CharSet.Mark,CharSet.Mark,CharSet.Mark,CharSet.Mark,CharSet.Mark,CharSet.Mark)!=OK)
	{
		perror("Boom::Draw Border");
		return;
	}

	mvwprintw(BoomWin,(LINES/6)-1,(COLS-15)/6,"Boom!");

	wrefresh(BoomWin);
	werase(BoomWin);
	wnoutrefresh(BoomWin);
	delwin(BoomWin);

	return;
}

void YouWin(void)
{
	WINDOW* YouWin;

	if ((YouWin=newwin(LINES/3,COLS/3,LINES/3,COLS/3))==NULL)
	{
		perror("YouWin::Alloc Window");
		return;
	}

	if (wborder(YouWin,CharSet.Mark,CharSet.Mark,CharSet.Mark,CharSet.Mark,CharSet.Mark,CharSet.Mark,CharSet.Mark,CharSet.Mark)!=OK)
	{
		perror("YouWin::Draw Border");
		return;
	}

	mvwprintw(YouWin,(LINES/6)-1,(COLS-15)/6,"You Win!");

	wrefresh(YouWin);
	napms(1000);
	werase(YouWin);
	wnoutrefresh(YouWin);
	delwin(YouWin);

	return;
}