/* 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.4 $ on $Date: 86/11/23 17:17:32 $
* $Source: /users/faustus/xchess/RCS/control.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.
* Deal with input from the user.
color nexttomove
= WHITE
;
static void screen_move();
button_pressed(event
, win
)
XKeyEvent
*ev
= (XKeyEvent
*) event
;
if (!oneboard
&& (win
->color
!= nexttomove
)) {
message_add(win
, "Wrong player!\n", true);
if (progflag
&& (nexttomove
== (blackflag
? WHITE
: BLACK
))) {
message_add(win
, "Wait for the computer...\n", true);
message_add(win
, "You'd better not do that now...\n", true);
/* Figure out what piece he is pointing at. */
x
= ev
->x
/ (SQUARE_WIDTH
+ BORDER_WIDTH
);
y
= ev
->y
/ (SQUARE_HEIGHT
+ BORDER_WIDTH
);
if ((x
< 0) || (x
>= SIZE
) || (y
< 0) || (y
>= SIZE
)) {
fprintf(stderr
, "Bad coords (%d, %d)\n", x
, y
);
if (oneboard
&& (chessboard
->square
[y
][x
].color
!= nexttomove
)) {
message_add(win
, "Wrong player!\n", true);
} else if (!oneboard
&& (chessboard
->square
[y
][x
].color
!=
message_add(win
, "Can't move that\n", true);
thismove
->piece
.color
= chessboard
->square
[y
][x
].color
;
thismove
->piece
.type
= chessboard
->square
[y
][x
].type
;
fprintf(stderr
, "%s selected his %s at (%d, %d)...\n",
colornames
[(int) thismove
->piece
.color
],
piecenames
[(int) thismove
->piece
.type
],
thismove
->fromy
, thismove
->fromx
);
button_released(event
, win
)
XKeyEvent
*ev
= (XKeyEvent
*) event
;
/* fprintf(stderr, "Error: button hasn't been pressed\n"); */
/* Figure out what piece he is pointing at. */
x
= ev
->x
/ (SQUARE_WIDTH
+ BORDER_WIDTH
);
y
= ev
->y
/ (SQUARE_HEIGHT
+ BORDER_WIDTH
);
if ((x
< 0) || (x
>= SIZE
) || (y
< 0) || (y
>= SIZE
)) {
fprintf(stderr
, "Bad coords (%d, %d)\n", x
, y
);
if ((thismove
->fromx
== x
) && (thismove
->fromy
== y
)) {
message_add(win
, "Hey, you touch it, you move it, buddy.\n",
if (chessboard
->square
[y
][x
].color
== thismove
->piece
.color
) {
message_add(win
, "Can't put one piece on top of another\n",
thismove
->taken
.color
= chessboard
->square
[y
][x
].color
;
thismove
->taken
.type
= chessboard
->square
[y
][x
].type
;
if (thismove
->taken
.color
!= NONE
)
thismove
->type
= CAPTURE
;
else if ((thismove
->piece
.type
== KING
) && (thismove
->fromx
== 4) &&
(thismove
->toy
== thismove
->fromy
))
thismove
->type
= KCASTLE
;
else if ((thismove
->piece
.type
== KING
) && (thismove
->tox
== 2) &&
(thismove
->fromx
== 4) &&
(thismove
->toy
== thismove
->fromy
))
thismove
->type
= QCASTLE
;
/* Now check the en-passant case... */
if ((thismove
->type
== MOVE
) && ((thismove
->tox
== thismove
->fromx
+ 1)
|| (thismove
->tox
== thismove
->fromx
- 1)) &&
(thismove
->piece
.type
== PAWN
) && lastmove
&&
(lastmove
->tox
== lastmove
->fromx
) && (lastmove
->fromx
== thismove
->tox
) && ((lastmove
->fromy
+ lastmove
->toy
)
thismove
->type
= CAPTURE
;
thismove
->enpassant
= true;
thismove
->taken
= lastmove
->piece
;
if (!valid_move(thismove
, chessboard
)) {
message_add(win
, "Invalid move.\n", true);
fprintf(stderr
, "\t... and moved it to (%d, %d), type %s\n",
thismove
->toy
, thismove
->tox
,
movetypenames
[(int) thismove
->type
]);
message_add(win1
, "Check.\n", true);
message_add(win2
, "Check.\n", true);
moves
= lastmove
= thismove
;
lastmove
= lastmove
->next
= thismove
;
nexttomove
= ((nexttomove
== WHITE
) ? BLACK
: WHITE
);
fprintf(stderr
, "program moves from (%d, %d) to (%d, %d)\n",
m
->fromy
, m
->fromx
, m
->toy
, m
->tox
);
lastmove
= lastmove
->next
= m
;
nexttomove
= ((nexttomove
== WHITE
) ? BLACK
: WHITE
);
/* Update the screen... */
/* Move the piece on the board... */
board_move(chessboard
, m
);
XBell(win1
->display
, 50);
XBell(win2
->display
, 50);
win_erasepiece(m
->fromy
, m
->fromx
, WHITE
);
win_drawpiece(&m
->piece
, m
->toy
, m
->tox
, WHITE
);
win_erasepiece(m
->toy
+ ((m
->piece
.color
== WHITE
) ?
win_erasepiece(m
->fromy
, m
->fromx
, BLACK
);
win_drawpiece(&m
->piece
, m
->toy
, m
->tox
, BLACK
);
win_erasepiece(m
->toy
+ ((m
->piece
.color
==
WHITE
) ? 1 : -1), m
->tox
, WHITE
);
if ((m
->piece
.type
== PAWN
) && (((m
->piece
.color
== BLACK
) &&
(m
->toy
== 7)) || ((m
->piece
.color
== WHITE
) &&
pp
.color
= m
->piece
.color
;
win_drawpiece(&pp
, m
->toy
, m
->tox
, WHITE
);
win_drawpiece(&m
->piece
, m
->toy
, m
->tox
, BLACK
);
if (m
->piece
.color
== WHITE
) {
win_erasepiece(7, 4, WHITE
);
win_erasepiece(7, 7, WHITE
);
win_drawpiece(&m
->piece
, 7, 6, WHITE
);
win_drawpiece(&chessboard
->square
[7][7], 7, 5, WHITE
);
win_erasepiece(7, 4, BLACK
);
win_erasepiece(7, 7, BLACK
);
win_drawpiece(&m
->piece
, 7, 6, BLACK
);
win_drawpiece(&chessboard
->square
[7][7], 7, 5,
win_erasepiece(0, 4, WHITE
);
win_erasepiece(0, 7, WHITE
);
win_drawpiece(&m
->piece
, 0, 6, WHITE
);
win_drawpiece(&chessboard
->square
[0][7], 0, 5, WHITE
);
win_erasepiece(0, 4, BLACK
);
win_erasepiece(0, 7, BLACK
);
win_drawpiece(&m
->piece
, 0, 6, BLACK
);
win_drawpiece(&chessboard
->square
[0][7], 0, 5,
if (m
->piece
.color
== WHITE
) {
win_erasepiece(7, 4, WHITE
);
win_erasepiece(7, 0, WHITE
);
win_drawpiece(&m
->piece
, 7, 2, WHITE
);
win_drawpiece(&chessboard
->square
[7][0], 7, 3, WHITE
);
win_erasepiece(7, 4, BLACK
);
win_erasepiece(7, 0, BLACK
);
win_drawpiece(&m
->piece
, 7, 2, BLACK
);
win_drawpiece(&chessboard
->square
[7][7], 7, 3,
win_erasepiece(0, 4, WHITE
);
win_erasepiece(0, 0, WHITE
);
win_drawpiece(&m
->piece
, 0, 2, WHITE
);
win_drawpiece(&chessboard
->square
[0][0], 0, 3, WHITE
);
win_erasepiece(0, 4, BLACK
);
win_erasepiece(0, 0, BLACK
);
win_drawpiece(&m
->piece
, 0, 2, BLACK
);
win_drawpiece(&chessboard
->square
[0][7], 0, 3,
fprintf(stderr
, "Bad move type %d\n", m
->type
);
/* Retract the last move made... */
memset(&bm
, 0, sizeof(bm
));
board_move(chessboard
, &bm
);
board_move(chessboard
, &bm
);
chessboard
->square
[m
->toy
][m
->tox
] = m
->taken
;
bm
.fromx
= bm
.tox
= m
->tox
;
bm
.fromy
= bm
.toy
= m
->toy
;
bm
.piece
.color
= m
->piece
.color
;
bm
.fromy
= bm
.toy
= (m
->piece
.color
== WHITE
) ? 7 : 0;
board_move(chessboard
, &bm
);
bm
.piece
.color
= m
->piece
.color
;
bm
.fromy
= bm
.toy
= (m
->piece
.color
== WHITE
) ? 7 : 0;
board_move(chessboard
, &bm
);
if (m
->piece
.color
== WHITE
)
chessboard
->white_cant_castle_k
= false;
chessboard
->black_cant_castle_k
= false;
bm
.piece
.color
= m
->piece
.color
;
bm
.fromy
= bm
.toy
= (m
->piece
.color
== WHITE
) ? 7 : 0;
board_move(chessboard
, &bm
);
bm
.piece
.color
= m
->piece
.color
;
bm
.fromy
= bm
.toy
= (m
->piece
.color
== WHITE
) ? 7 : 0;
board_move(chessboard
, &bm
);
if (m
->piece
.color
== WHITE
)
chessboard
->white_cant_castle_q
= false;
chessboard
->black_cant_castle_q
= false;
nexttomove
= ((nexttomove
== WHITE
) ? BLACK
: WHITE
);
for (m
= moves
; m
->next
; m
= m
->next
)
lastmove
->next
->next
= foremoves
;
foremoves
= lastmove
->next
;
/* Put back the last move undone. */
foremoves
= foremoves
->next
;
moves
= lastmove
= thismove
= NULL
;