summaryrefslogtreecommitdiffstats
path: root/gnu/games/chess/Xchess/program.c
blob: 876f29cb466c7afed479824ba85d17c05d988c3b (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

/* This file contains code for X-CHESS.
   Copyright (C) 1986 Free Software Foundation, Inc.

This file is part of X-CHESS.

X-CHESS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.  No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing.  Refer to the X-CHESS General Public
License for full details.

Everyone is granted permission to copy, modify and redistribute
X-CHESS, but only under the conditions described in the
X-CHESS General Public License.   A copy of this license is
supposed to have been given to you along with X-CHESS so you
can know your rights and responsibilities.  It should be in a
file named COPYING.  Among other things, the copyright notice
and this notice must be preserved on all copies.  */


/* RCS Info: $Revision: 1.2 $ on $Date: 86/11/23 17:18:10 $
 *           $Source: /users/faustus/xchess/RCS/program.c,v $
 * Copyright (c) 1986 Wayne A. Christopher, U. C. Berkeley CAD Group
 *	Permission is granted to do anything with this code except sell it
 *	or remove this message.
 *
 * The interface to whichever chess playing program we are using...
 */

#include "xchess.h"
#include <signal.h>
#include <sys/time.h>

static int pid;
static FILE *from;
static FILE *to;
static bool easy = 1;

bool
program_init(name)
	char *name;
{
	int toprog[2], fromprog[2];
	char buf[BSIZE];
	char time[10];
	char moves[10];

	pipe(toprog);
	pipe(fromprog);

	if (!(pid = fork())) {
		/* Start up the program. */
		dup2(toprog[0], 0);
		dup2(fromprog[1], 1);
		close(toprog[0]);
		close(toprog[1]);
		close(fromprog[0]);
		close(fromprog[1]);
		sprintf (time, "%d", timeunit/60);
		sprintf (moves, "%d", movesperunit);
		if (proghost)
			execl("/usr/ucb/rsh", "rsh", proghost, name,
					moves, time, 
					(char *) NULL);
		else
			execl(name, name, moves, time, (char *) NULL);
		perror(name);
		exit(1);
	}

	close(toprog[0]);
	close(fromprog[1]);

	from = fdopen(fromprog[0], "r");
	setbuf(from, NULL);
	to = fdopen(toprog[1], "w");
	setbuf(to, NULL);

	/* Get the first line... */
	fgets(buf, BSIZE, from);
	if (debug)
		fprintf(stderr, "program says %s", buf);
	if (blackflag) {
		fputs("switch\n", to);
		fflush(to);
		fgets(buf, BSIZE, from);
		if (debug)
			fprintf(stderr, "program says %s", buf);
		message_add(win1, "GNU Chess playing white\n", false);
	} else
		message_add(win1, "GNU Chess playing black\n", false);

	return (true);
}

void
program_end()
{
	fclose(from);
	fclose(to);
	kill(pid, SIGTERM);
	return;
}

void
program_send(m)
	move *m;
{
	char buf[BSIZE];

	if ((m->type == MOVE) || (m->type == CAPTURE))
		sprintf(buf, "%c%d%c%d\n", 'a' + m->fromx, SIZE - m->fromy,
				'a' + m->tox, SIZE - m->toy);
	else if (m->type == KCASTLE)
		strcpy(buf, (m->piece.color == WHITE) ? "e1g1\n" : "e8g8\n");
	else if (m->type == QCASTLE)
		strcpy(buf, (m->piece.color == WHITE) ? "e1c1\n" : "e8c8\n");

	if (debug)
		fprintf(stderr, "sending program %s", buf);
	if (!easy)
		kill (pid, SIGINT);

	fputs(buf, to);
	fflush(to);

	/* One junk line... */
	fgets(buf, BSIZE, from);
	if (debug)
		fprintf(stderr, "program says %s", buf);
	return;
}

move *
program_get()
{
	int rfd = (1 << fileno(from)), wfd = 0, xfd = 0;
	static struct timeval notime = { 0, 0 };
	char buf[BSIZE], *s;
	move *m;
	int i;

	/* Do a poll... */

	if (!(i = select(32, &rfd, &wfd, &xfd, &notime)) &&
			!from->_cnt) {		/* Bad stuff... */
		if (debug)
			fprintf(stderr, "poll: nothing\n");
		return (NULL);
	}
	if (i == -1) {
		perror("select");
		return (NULL);
	}

	fgets(buf, BSIZE, from);
	if (*buf == '\n' || *buf == '\0') {
	        message_add(win1, "program died", false);
		return (NULL);
	}

	if (debug)
		fprintf(stderr, "got from program %s", buf);

	for (s = buf; !isalpha(*s); s++)
		;
	m = parse_imove(chessboard, s, nexttomove);
	if (m == NULL)
	 	return (NULL);

	if (!valid_move(m, chessboard)) {
		fprintf(stderr, "Error: move %s is invalid!!\n", buf);
		return (NULL);
	}

	/*
	fgets(buf, BSIZE, from);
	if (debug)
		fprintf(stderr, "program says %s", buf);
	*/
	message_add(win1, buf, false);
	return (m);
}

void
program_undo()
{
	fputs("undo\n", to);
	return;
}
void
program_easy (mode)
	bool mode;

{
	fputs("easy\n", to);
	easy = mode;
}
OpenPOWER on IntegriCloud