src/chess/rules.c

Wed, 16 Apr 2014 21:46:34 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 16 Apr 2014 21:46:34 +0200
changeset 41
a8346dcf7bbf
parent 40
47162a7621da
child 47
d726e4b46c33
permissions
-rw-r--r--

faced struct alignment problems

19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2014 Mike Becker. All rights reserved.
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 *
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 #include "rules.h"
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 #include "chess.h"
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 #include <string.h>
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
33 #include <stdlib.h>
36
ebe0c961e9a6 reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
34 #include <sys/time.h>
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
35
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
36 void gamestate_cleanup(GameState *gamestate) {
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
37 MoveList *elem;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
38 elem = gamestate->movelist;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
39 while (elem) {
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
40 MoveList *cur = elem;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
41 elem = elem->next;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
42 free(cur);
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
43 };
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
44 }
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
45
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
46 static void addmove(GameState* gamestate, Move *move) {
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
47 MoveList *elem = malloc(sizeof(MoveList));
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
48 elem->next = NULL;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
49 elem->move = *move;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
50
40
47162a7621da fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
51 struct timeval curtimestamp;
47162a7621da fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
52 gettimeofday(&curtimestamp, NULL);
47162a7621da fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
53 elem->move.timestamp.tv_sec = curtimestamp.tv_sec;
47162a7621da fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
54 elem->move.timestamp.tv_usec = curtimestamp.tv_usec;
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
55
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
56 if (gamestate->lastmove) {
40
47162a7621da fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
57 struct movetimeval *lasttstamp = &(gamestate->lastmove->move.timestamp);
47162a7621da fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
58 uint64_t sec = curtimestamp.tv_sec - lasttstamp->tv_sec;
36
ebe0c961e9a6 reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
59 suseconds_t micros;
40
47162a7621da fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
60 if (curtimestamp.tv_usec < lasttstamp->tv_usec) {
47162a7621da fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
61 micros = 1e6L-(lasttstamp->tv_usec - curtimestamp.tv_usec);
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
62 sec--;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
63 } else {
40
47162a7621da fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
64 micros = curtimestamp.tv_usec - lasttstamp->tv_usec;
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
65 }
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
66
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
67 elem->move.movetime.tv_sec = sec;
36
ebe0c961e9a6 reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
68 elem->move.movetime.tv_usec = micros;
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
69
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
70 gamestate->lastmove->next = elem;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
71 gamestate->lastmove = elem;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
72 } else {
36
ebe0c961e9a6 reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
73 elem->move.movetime.tv_usec = 0;
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
74 elem->move.movetime.tv_sec = 0;
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
75 gamestate->movelist = gamestate->lastmove = elem;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
76 }
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
77 }
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 char getpiecechr(uint8_t piece) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 switch (piece & PIECE_MASK) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 case ROOK: return 'R';
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 case KNIGHT: return 'N';
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 case BISHOP: return 'B';
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 case QUEEN: return 'Q';
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 case KING: return 'K';
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 default: return '\0';
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 uint8_t getpiece(char c) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 switch (c) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 case 'R': return ROOK;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 case 'N': return KNIGHT;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 case 'B': return BISHOP;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 case 'Q': return QUEEN;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 case 'K': return KING;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 default: return 0;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
101 static int getlocation(GameState *gamestate, Move *move) {
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 uint8_t piece = move->piece & PIECE_MASK;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 switch (piece) {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
104 case PAWN: return pawn_getlocation(gamestate, move);
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
105 case ROOK: return rook_getlocation(gamestate, move);
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
106 case KNIGHT: return knight_getlocation(gamestate, move);
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
107 case BISHOP: return bishop_getlocation(gamestate, move);
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
108 case QUEEN: return queen_getlocation(gamestate, move);
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
109 case KING: return king_getlocation(gamestate, move);
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 default: return INVALID_MOVE_SYNTAX;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
114 void apply_move(GameState *gamestate, Move *move) {
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 uint8_t piece = move->piece & PIECE_MASK;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 uint8_t color = move->piece & COLOR_MASK;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 /* en passant capture */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 if (move->capture && piece == PAWN &&
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
120 mdst(gamestate->board, move) == 0) {
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
121 gamestate->board[move->fromrow][move->tofile] = 0;
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 /* remove old en passant threats */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 for (uint8_t file = 0 ; file < 8 ; file++) {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
126 gamestate->board[3][file] &= ~ENPASSANT_THREAT;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
127 gamestate->board[4][file] &= ~ENPASSANT_THREAT;
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 /* add new en passant threat */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 if (piece == PAWN && (
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 (move->fromrow == 1 && move->torow == 3) ||
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 (move->fromrow == 6 && move->torow == 4))) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 move->piece |= ENPASSANT_THREAT;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 /* move (and maybe capture or promote) */
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
138 msrc(gamestate->board, move) = 0;
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 if (move->promotion) {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
140 mdst(gamestate->board, move) = move->promotion;
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 } else {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
142 mdst(gamestate->board, move) = move->piece;
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 /* castling */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 if (piece == KING &&
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 move->fromfile == fileidx('e')) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 if (move->tofile == fileidx('g')) {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
150 gamestate->board[move->torow][fileidx('h')] = 0;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
151 gamestate->board[move->torow][fileidx('f')] = color|ROOK;
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 } else if (move->tofile == fileidx('c')) {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
153 gamestate->board[move->torow][fileidx('a')] = 0;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
154 gamestate->board[move->torow][fileidx('d')] = color|ROOK;
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 }
28
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
157
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
158 addmove(gamestate, move);
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
161 static _Bool validate_move_rules(GameState *gamestate, Move *move) {
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 /* validate indices (don't trust opponent) */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 if (!chkidx(move)) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164 return 0;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166
21
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
167 /* must move */
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
168 if (move->fromfile == move->tofile && move->fromrow == move->torow) {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
169 return 0;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
170 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
171
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 /* does piece exist */
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
173 if (msrc(gamestate->board, move) != move->piece) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
174 return 0;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
175 }
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 /* can't capture own pieces */
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
178 if ((mdst(gamestate->board, move) & COLOR_MASK)
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
179 == (move->piece & COLOR_MASK)) {
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 return 0;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 /* validate individual rules */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 switch (move->piece & PIECE_MASK) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 case PAWN:
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
186 return pawn_chkrules(gamestate, move) &&
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
187 !pawn_isblocked(gamestate, move);
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 case ROOK:
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
189 return rook_chkrules(move) &&
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
190 !rook_isblocked(gamestate, move);
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 case KNIGHT:
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
192 return knight_chkrules(move); /* knight is never blocked */
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 case BISHOP:
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
194 return bishop_chkrules(move) &&
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
195 !bishop_isblocked(gamestate, move);
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 case QUEEN:
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
197 return queen_chkrules(move) &&
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
198 !queen_isblocked(gamestate, move);
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 case KING:
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
200 return king_chkrules(gamestate, move) &&
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
201 !king_isblocked(gamestate, move);
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202 default:
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
203 return 0;
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204 }
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
205 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
206
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
207 _Bool validate_move(GameState *gamestate, Move *move) {
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
209 _Bool result = validate_move_rules(gamestate, move);
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
210
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
211 /* cancel processing to save resources */
25
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
212 if (!result) {
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
213 return 0;
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
214 }
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
215
28
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
216 /* find kings for check validation */
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
217 uint8_t piececolor = (move->piece & COLOR_MASK);
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
218
28
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
219 uint8_t mykingfile = 0, mykingrow = 0, opkingfile = 0, opkingrow = 0;
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
220 for (uint8_t row = 0 ; row < 8 ; row++) {
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
221 for (uint8_t file = 0 ; file < 8 ; file++) {
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
222 if (gamestate->board[row][file] ==
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
223 (piececolor == WHITE?WKING:BKING)) {
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
224 mykingfile = file;
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
225 mykingrow = row;
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
226 } else if (gamestate->board[row][file] ==
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
227 (piececolor == WHITE?BKING:WKING)) {
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
228 opkingfile = file;
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
229 opkingrow = row;
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
230 }
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
231 }
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
232 }
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
233
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
234 /* simulation move for check validation */
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
235 GameState simulation;
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
236 memcpy(&simulation, gamestate, sizeof(GameState));
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
237 apply_move(&simulation, move);
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
238
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
239 /* don't move into or stay in check position */
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
240 if (is_covered(&simulation, mykingrow, mykingfile,
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
241 opponent_color(piececolor))) {
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
242 return 0;
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
243 }
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
244
25
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
245 /* correct check and checkmate flags (move is still valid) */
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
246 Move threats[16];
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
247 uint8_t threatcount;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
248 move->check = get_threats(&simulation, opkingrow, opkingfile,
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
249 piececolor, threats, &threatcount);
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
250
28
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
251 if (move->check) {
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
252 /* determine possible escape fields */
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
253 _Bool canescape = 0;
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
254 for (int dr = -1 ; dr <= 1 && !canescape ; dr++) {
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
255 for (int df = -1 ; df <= 1 && !canescape ; df++) {
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
256 if (!(dr == 0 && df == 0) &&
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
257 isidx(opkingrow + dr) && isidx(opkingfile + df)) {
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
258
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
259 /* escape field neither blocked nor covered */
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
260 if ((simulation.board[opkingrow + dr][opkingfile + df]
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
261 & COLOR_MASK) != opponent_color(piececolor)) {
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
262 canescape |= !is_covered(&simulation,
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
263 opkingrow + dr, opkingfile + df, piececolor);
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
264 }
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
265 }
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
266 }
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
267 }
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
268 /* can't escape, can he capture? */
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
269 if (!canescape && threatcount == 1) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
270 canescape = is_attacked(&simulation, threats[0].fromrow,
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
271 threats[0].fromfile, opponent_color(piececolor));
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
272 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
273
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
274 /* can't capture, can he block? */
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
275 if (!canescape && threatcount == 1) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
276 Move *threat = &(threats[0]);
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
277 uint8_t threatpiece = threat->piece & PIECE_MASK;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
278
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
279 /* knight, pawns and the king cannot be blocked */
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
280 if (threatpiece == BISHOP || threatpiece == ROOK
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
281 || threatpiece == QUEEN) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
282 if (threat->fromrow == threat->torow) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
283 /* rook aspect (on row) */
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
284 int d = threat->tofile > threat->fromfile ? 1 : -1;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
285 uint8_t file = threat->fromfile;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
286 while (!canescape && file != threat->tofile - d) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
287 file += d;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
288 canescape |= is_protected(&simulation,
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
289 threat->torow, file, opponent_color(piececolor));
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
290 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
291 } else if (threat->fromfile == threat->tofile) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
292 /* rook aspect (on file) */
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
293 int d = threat->torow > threat->fromrow ? 1 : -1;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
294 uint8_t row = threat->fromrow;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
295 while (!canescape && row != threat->torow - d) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
296 row += d;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
297 canescape |= is_protected(&simulation,
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
298 row, threat->tofile, opponent_color(piececolor));
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
299 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
300 } else {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
301 /* bishop aspect */
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
302 int dr = move->torow > move->fromrow ? 1 : -1;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
303 int df = move->tofile > move->fromfile ? 1 : -1;
28
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
304
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
305 uint8_t row = move->fromrow;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
306 uint8_t file = move->fromfile;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
307 while (!canescape && file != move->tofile - df
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
308 && row != move->torow - dr) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
309 row += dr;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
310 file += df;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
311 canescape |= is_protected(&simulation, row, file,
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
312 opponent_color(piececolor));
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
313 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
314 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
315 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
316 }
28
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
317
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
318 if (!canescape) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
319 gamestate->checkmate = 1;
28
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
320 }
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
321 }
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
322
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
323 return 1;
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
324 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
325
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
326 int eval_move(GameState *gamestate, char *mstr, Move *move) {
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
327 memset(move, 0, sizeof(Move));
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
328 move->fromfile = POS_UNSPECIFIED;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
329 move->fromrow = POS_UNSPECIFIED;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
330
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
331 size_t len = strlen(mstr);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
332
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
333 /* evaluate check/checkmate flags */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
334 if (mstr[len-1] == '+') {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
335 len--; mstr[len] = '\0';
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
336 move->check = 1;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
337 } else if (mstr[len-1] == '#') {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
338 len--; mstr[len] = '\0';
27
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
339 /* ignore - validation should set game state */
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
340 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
341
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
342 /* evaluate promotion */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
343 if (len > 3 && mstr[len-2] == '=') {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
344 move->promotion = getpiece(mstr[len-1]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
345 if (!move->promotion) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
346 return INVALID_MOVE_SYNTAX;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
347 } else {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
348 move->promotion |= gamestate->mycolor;
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
349 len -= 2;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
350 mstr[len] = 0;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
351 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
352 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
353
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
354 if (len == 2) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
355 /* pawn move (e.g. "e4") */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
356 move->piece = PAWN;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
357 move->tofile = fileidx(mstr[0]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
358 move->torow = rowidx(mstr[1]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
359 } else if (len == 3) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
360 if (strcmp(mstr, "O-O") == 0) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
361 /* king side castling */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
362 move->piece = KING;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
363 move->fromfile = fileidx('e');
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
364 move->tofile = fileidx('g');
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
365 move->fromrow = move->torow = gamestate->mycolor == WHITE ? 0 : 7;
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
366 } else {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
367 /* move (e.g. "Nf3") */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
368 move->piece = getpiece(mstr[0]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
369 move->tofile = fileidx(mstr[1]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
370 move->torow = rowidx(mstr[2]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
371 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
372
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
373 } else if (len == 4) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
374 move->piece = getpiece(mstr[0]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
375 if (!move->piece) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
376 move->piece = PAWN;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
377 move->fromfile = fileidx(mstr[0]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
378 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
379 if (mstr[1] == 'x') {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
380 /* capture (e.g. "Nxf3", "dxe5") */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
381 move->capture = 1;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
382 } else {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
383 /* move (e.g. "Ndf3", "N2c3", "e2e4") */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
384 if (isfile(mstr[1])) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
385 move->fromfile = fileidx(mstr[1]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
386 if (move->piece == PAWN) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
387 move->piece = 0;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
388 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
389 } else {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
390 move->fromrow = rowidx(mstr[1]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
391 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
392 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
393 move->tofile = fileidx(mstr[2]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
394 move->torow = rowidx(mstr[3]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
395 } else if (len == 5) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
396 if (strcmp(mstr, "O-O-O") == 0) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
397 /* queen side castling "O-O-O" */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
398 move->piece = KING;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
399 move->fromfile = fileidx('e');
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
400 move->tofile = fileidx('c');
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
401 move->fromrow = move->torow = gamestate->mycolor == WHITE ? 0 : 7;
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
402 } else {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
403 move->piece = getpiece(mstr[0]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
404 if (mstr[2] == 'x') {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
405 move->capture = 1;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
406 if (move->piece) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
407 /* capture (e.g. "Ndxf3") */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
408 move->fromfile = fileidx(mstr[1]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
409 } else {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
410 /* long notation capture (e.g. "e5xf6") */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
411 move->piece = PAWN;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
412 move->fromfile = fileidx(mstr[0]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
413 move->fromrow = rowidx(mstr[1]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
414 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
415 } else {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
416 /* long notation move (e.g. "Nc5a4") */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
417 move->fromfile = fileidx(mstr[1]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
418 move->fromrow = rowidx(mstr[2]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
419 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
420 move->tofile = fileidx(mstr[3]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
421 move->torow = rowidx(mstr[4]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
422 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
423 } else if (len == 6) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
424 /* long notation capture (e.g. "Nc5xf3") */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
425 if (mstr[3] == 'x') {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
426 move->capture = 1;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
427 move->piece = getpiece(mstr[0]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
428 move->fromfile = fileidx(mstr[1]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
429 move->fromrow = rowidx(mstr[2]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
430 move->tofile = fileidx(mstr[4]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
431 move->torow = rowidx(mstr[5]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
432 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
433 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
434
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
435
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
436 if (move->piece) {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
437 if (move->piece == PAWN
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
438 && move->torow == (gamestate->mycolor==WHITE?7:0)
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
439 && !move->promotion) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
440 return NEED_PROMOTION;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
441 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
442
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
443 move->piece |= gamestate->mycolor;
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
444 if (move->fromfile == POS_UNSPECIFIED
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
445 || move->fromrow == POS_UNSPECIFIED) {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
446 return getlocation(gamestate, move);
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
447 } else {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
448 return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
449 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
450 } else {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
451 return INVALID_MOVE_SYNTAX;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
452 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
453 }
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
454
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
455 _Bool get_threats(GameState *gamestate, uint8_t row, uint8_t file,
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
456 uint8_t color, Move *threats, uint8_t *threatcount) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
457 Move candidates[16];
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
458 int candidatecount = 0;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
459 for (uint8_t r = 0 ; r < 8 ; r++) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
460 for (uint8_t f = 0 ; f < 8 ; f++) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
461 if ((gamestate->board[r][f] & COLOR_MASK) == color) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
462 memset(&(candidates[candidatecount]), 0, sizeof(Move));
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
463 candidates[candidatecount].piece = gamestate->board[r][f];
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
464 candidates[candidatecount].fromrow = r;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
465 candidates[candidatecount].fromfile = f;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
466 candidates[candidatecount].torow = row;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
467 candidates[candidatecount].tofile = file;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
468 candidatecount++;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
469 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
470 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
471 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
472
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
473 if (threatcount) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
474 *threatcount = 0;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
475 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
476
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
477
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
478 _Bool result = 0;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
479
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
480 for (int i = 0 ; i < candidatecount ; i++) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
481 if (validate_move_rules(gamestate, &(candidates[i]))) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
482 result = 1;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
483 if (threats && threatcount) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
484 threats[(*threatcount)++] = candidates[i];
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
485 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
486 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
487 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
488
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
489 return result;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
490 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
491
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
492 _Bool get_real_threats(GameState *gamestate, uint8_t row, uint8_t file,
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
493 uint8_t color, Move *threats, uint8_t *threatcount) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
494
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
495 Move candidates[16];
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
496 uint8_t candidatecount;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
497 if (get_threats(gamestate, row, file, color, candidates, &candidatecount)) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
498
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
499 if (threatcount) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
500 *threatcount = 0;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
501 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
502 _Bool result = 0;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
503 uint8_t kingfile = 0, kingrow = 0;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
504 for (uint8_t row = 0 ; row < 8 ; row++) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
505 for (uint8_t file = 0 ; file < 8 ; file++) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
506 if ((gamestate->board[row][file] & COLOR_MASK) == color) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
507 kingfile = file;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
508 kingrow = row;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
509 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
510 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
511 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
512
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
513 for (uint8_t i = 0 ; i < candidatecount ; i++) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
514 GameState simulation;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
515 memcpy(&simulation, gamestate, sizeof(GameState));
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
516 apply_move(&simulation, &(candidates[i]));
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
517 if (!is_covered(&simulation, kingrow, kingfile,
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
518 opponent_color(color))) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
519 result = 1;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
520 if (threats && threatcount) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
521 threats[(*threatcount)++] = candidates[i];
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
522 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
523 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
524 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
525
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
526 return result;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
527 } else {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
528 return 0;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
529 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
530 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
531
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
532 _Bool is_protected(GameState *gamestate, uint8_t row, uint8_t file,
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
533 uint8_t color) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
534
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
535 Move threats[16];
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
536 uint8_t threatcount;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
537 if (get_real_threats(gamestate, row, file, color, threats, &threatcount)) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
538 for (int i = 0 ; i < threatcount ; i++) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
539 if (threats[i].piece != (color|KING)) {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
540 return 1;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
541 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
542 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
543 return 0;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
544 } else {
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
545 return 0;
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
546 }
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
547 }
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
548
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
549 uint16_t remaining_movetime(GameInfo *gameinfo, GameState *gamestate,
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
550 uint8_t color) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
551 if (!gameinfo->timecontrol) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
552 return 0;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
553 }
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
554
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
555 if (gamestate->movelist) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
556 uint16_t time = gameinfo->time;
36
ebe0c961e9a6 reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
557 suseconds_t micros = 0;
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
558
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
559 MoveList *movelist = color == WHITE ?
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
560 gamestate->movelist : gamestate->movelist->next;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
561
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
562 while (movelist) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
563 time += gameinfo->addtime;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
564
40
47162a7621da fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
565 struct movetimeval *movetime = &(movelist->move.movetime);
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
566 if (movetime->tv_sec >= time) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
567 return 0;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
568 }
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
569
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
570 time -= movetime->tv_sec;
36
ebe0c961e9a6 reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
571 micros += movetime->tv_usec;
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
572
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
573 movelist = movelist->next ? movelist->next->next : NULL;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
574 }
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
575
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
576 time_t sec;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
577 movelist = gamestate->lastmove;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
578 if ((movelist->move.piece & COLOR_MASK) != color) {
40
47162a7621da fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
579 struct movetimeval *lastmovetstamp = &(movelist->move.timestamp);
36
ebe0c961e9a6 reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
580 struct timeval currenttstamp;
ebe0c961e9a6 reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
581 gettimeofday(&currenttstamp, NULL);
ebe0c961e9a6 reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
582 micros += currenttstamp.tv_usec - lastmovetstamp->tv_usec;
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
583 sec = currenttstamp.tv_sec - lastmovetstamp->tv_sec;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
584 if (sec >= time) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
585 return 0;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
586 }
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
587
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
588 time -= sec;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
589 }
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
590
36
ebe0c961e9a6 reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
591 sec = micros / 1e6L;
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
592
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
593 if (sec >= time) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
594 return 0;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
595 }
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
596
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
597 time -= sec;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
598
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
599 return time;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
600 } else {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
601 return gameinfo->time;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
602 }
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
603 }

mercurial