summaryrefslogtreecommitdiffstats
path: root/lib/libncurses/TESTS/knight.c
blob: 73f7b42f9d9334cfba0be5909d1683b192aae874 (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
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
/* Knights Tour - a brain game */

#include <ncurses.h>
#include <signal.h>
#include <ctype.h>
#include <stdlib.h>

#ifdef __FreeBSD__
#define srand48 srandom
#define lrand48 random
#endif

short   board [64];     /* the squares */
char    row, column;    /* input characters */
int     rw,col;         /* numeric equivalent of row and column */
int     curow,curcol;   /* current row and column integers */
int     rdif, cdif;     /* difference between input and current */
int     j;              /* index into board */

char	script[]={"'_)//,/(-)/__/__(_<_(__),|/|/_///_/_<//_/_)__o__o'______///_(--_(_)___,(_/,_/__(_\0"};

int	ypos[] ={1,0,1,2,3,0,1,2,2,3,3,2,2,3,2,2,3,3,3,3,3,2,3,3,2,4,5,5,
	4,3,3,2,1,2,3,3,3,2,1,3,3,2,3,3,3,2,1,1,0,1,4,4,4,4,4,4,5,6,7,7,
	7,6,6,6,7,7,7,6,6,6,6,7,7,7,6,7,7,6,6,7,7};

int	xpos[]={0,1,2,1,0,5,4,3,2,4,6,7,8,8,9,10,10,11,12,13,14,15,15,16,
	16,16,15,14,15,17,18,19,20,20,20,21,22,23,24,23,25,26,27,26,28,
	13,23,25,27,27,2,3,4,5,6,7,4,3,2,1,0,1,2,5,4,5,6,6,7,8,9,8,9,10,
	11,11,12,13,14,14,15};

static char *instructions[] =
{
"     Knight's Tour is a board game for one player.   It is played on",
"an eight by eight board and is based on the allowable moves that a knight",
"can make in the game of chess.  For those who are unfamiliar with the",
"game, a knight may move either on a row or a column but not diagonally.",
"He may move one square in any direction and two squares in a perpendicular",
"direction >or< two squares in any direction and one square in a",
"perpendicular direction.  He may not, of course, move off the board.",
"",
"     At the beginning of a game you will be asked to either choose a",
"starting square or allow the computer to select a random location.",
"Squares are designated by a letter-number combination where the row is",
"specified by a letter A-H and the numbers 1-8 define a column.  Invalid",
"entries are ignored and illegal moves produce a beep at the terminal.",
"",
"     The objective is to visit every square on the board.  When you claim",
"a square a marker is placed on it to show where you've been.  You may",
"not revisit a square that you've landed on before.",
"",
"     After each move the program checks to see if you have any legal",
"moves left.  If not, the game ends and your squares are counted.  If",
"you've made all the squares you win the game.  Otherwise, you are told",
"the number of squares you did make.",
"END"
};

void init(void);
int play(void);
void drawboard(void);
void dosquares(void);
void getfirst(void);
void getrc(void);
void putstars(void);
int evalmove(void);
int chkmoves(void);
int endgame(void);
int chksqr(int, int);
void instruct(void);
void title(int, int);

int
main ()
{
	init ();
	for (;;)
		if (!play ()) {
			endwin ();
			exit (0);
		}
}

void
init ()
{

	srand48 (getpid());
	initscr ();
	cbreak ();              /* immediate char return */
	noecho ();              /* no immediate echo */
	title (1,23);
	mvaddstr (23, 25, "Would you like instructions? ");
	refresh();
	if ((toupper(getch())) == 'Y')
		instruct();
	clear ();
}

int
play ()
{
	drawboard ();           /* clear screen and drawboard */
	for (j = 0; j < 64; j++) board[j]=0;
	getfirst ();            /* get the starting square */
	for (;;) {
		getrc();
		if (evalmove()) {
			putstars ();
			if (!chkmoves())
				return (endgame ());
		}
		else beep();
	}
}

void
drawboard ()
{
	erase ();
	dosquares ();
	refresh ();
	mvaddstr (0, 7, "1   2   3   4   5   6   7   8");
	for (j = 0; j < 8; j++) mvaddch (2*j+2, 3, j + 'A');
	refresh ();
	mvaddstr (20,  5, "ROW:");
	mvaddstr (20, 27, "COLUMN:");
	mvaddstr (14, 49, "CURRENT ROW");
	mvaddstr (16, 49, "CURRENT COL");
	mvaddstr (22,  5, "A - H or Q to quit");
	mvaddstr (22, 27, "1 - 8 or ESC to cancel row");
	refresh ();
	title (1,40);
}

void
dosquares ()
{
	mvaddstr (1, 6, "-------------------------------");
	for (j = 1; j < 9; j++){
		mvaddstr (2*j, 5,  "|   |   |   |   |   |   |   |   |");
		mvaddstr (2*j+1, 6, "-------------------------------");
	}
}

void
getfirst ()                             /* get first square */
{
	mvaddstr (23, 25, "(S)elect or (R)andom "); refresh ();
	do {
		row = toupper(getch());
	} while ((row != 'S') && (row != 'R'));
	if (row == 'R') {
		rw = lrand48() % 8;
		col = lrand48() % 8;
		j = 8* rw + col;
		row = rw + 'A';
		column = col + '1';
	}
	else {
		mvaddstr (23, 25, "Enter starting row and column");
		refresh ();
		getrc();                        /* get row and column */
	}
	putstars ();
	move (23, 0);
	clrtobot();
}

void
getrc ()                                /* get row and column */
{
	noecho ();
	do {
		mvaddstr (20, 35, "  ");
		refresh ();
		do {
			mvaddch (20, 11, ' ');
			move (20, 11);
			refresh ();
			row=toupper(getch());
			if (row == 'Q') {
				endwin ();
				exit (1);
			}
		} while ((row < 'A') || (row > 'H'));
		addch (row);
		move (20, 35);
		refresh ();
		do {
			column=getch();
			if (column == '\033') break;
		} while ((column < '1') || (column > '8'));
		if (column != '\033') addch (column);
	} while (column == '\033');
	refresh();
	rw = row - 'A';
	col= column - '1';
	j = 8 * rw + col;
}

void
putstars ()                     /* place the stars, update board & currents */
{
	mvaddch (2*curow+2, 38, ' ');
	mvaddch (2*rw+2, 38, '<');
	mvaddch (18, curcol*4+7, ' ');
	mvaddch (18, col*4+7, '^');
	curow = rw;
	curcol= col;
	mvaddstr (2 * rw + 2, 4*col+6, "***");
	mvaddch (14, 61, row);
	mvaddch (16, 61, column);
	refresh ();
	board[j] = 1;
}

int
evalmove()                      /* convert row and column to integers */
		                /* and evaluate move */
{
	rdif = rw - curow;
	cdif = col - curcol;
	rdif = abs(rw  - curow);
	cdif = abs(col - curcol);
	refresh ();
	if ((rdif == 1) && (cdif == 2)) if (board [j] == 0) return (1);
	if ((rdif == 2) && (cdif == 1)) if (board [j] == 0) return (1);
	return (0);
}

int
chkmoves ()                     /* check to see if valid moves are available */
{
	if (chksqr(2,1))   return (1);
	if (chksqr(2,-1))  return (1);
	if (chksqr(-2,1))  return (1);
	if (chksqr(-2,-1)) return (1);
	if (chksqr(1,2))   return (1);
	if (chksqr(1,-2))  return (1);
	if (chksqr(-1,2))  return (1);
	if (chksqr(-1,-2)) return (1);
	return (0);
}

int
endgame ()                      /* check for filled board or not */
{
	rw = 0;
	for (j = 0; j < 64; j++) if (board[j] != 0) rw+=1;
	if (rw == 64) mvaddstr (20, 20, "Congratulations !! You got 'em all");
		else mvprintw (20, 20, "You have ended up with %2d squares",rw);
	mvaddstr (21, 25, "Play again ? (y/n) ");
	refresh ();
	if ((row=tolower(getch())) == 'y') return (1);
		else return (0);
}

#ifndef abs
abs(num)
int	num;
{
	if (num < 0) return (-num);
		else return (num);
}
#endif

int
chksqr (int n1, int n2)
{
int	r1, c1;

	r1 = rw + n1;
	c1 = col + n2;
	if ((r1<0) || (r1>7)) return (0);
	if ((c1<0) || (c1>7)) return (0);
	if (board[r1*8+c1] == 0) return (1);
		else return (0);
}

void
instruct()
{
int i;

	clear ();
	for (i=0;;i++) {
		if ((strcmp(instructions[i],"END"))) mvaddstr (i, 0, instructions[i]);
			else {
				mvaddstr (23, 25, "Ready to play ? (y/n) ");
				refresh();
				if (toupper(getch()) == 'Y') {
					clear ();
					return;
				} else {
					clear ();
					refresh ();
					endwin ();
					exit (0);
				}
			}
	}
}

void
title (y,x)
int	y,x;
{
char c;

	j = 0;
	do {
		c = script[j];
		if (c == 0) break ;
		mvaddch (ypos[j]+y, xpos[j]+x, c);
		j++;
	} while (c != 0);
	refresh ();
}


OpenPOWER on IntegriCloud