src/chess/rook.c

Thu, 17 Apr 2014 11:41:02 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 17 Apr 2014 11:41:02 +0200
changeset 44
1891d88cbd10
parent 23
824c9522ce66
child 47
d726e4b46c33
permissions
-rw-r--r--

surrender is now resign

10
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2014 Mike Becker. All rights reserved.
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1347e4dabac0 prepared code base for implementing rules
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
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 *
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 */
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29
16
a298c6637c30 introduced status codes for get_location to produce detailed error messages + added knight rules
Mike Becker <universe@uap-core.de>
parents: 12
diff changeset
30 #include "rules.h"
10
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 #include "rook.h"
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32
16
a298c6637c30 introduced status codes for get_location to produce detailed error messages + added knight rules
Mike Becker <universe@uap-core.de>
parents: 12
diff changeset
33 _Bool rook_chkrules(Move *move) {
21
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
34 return move->torow == move->fromrow || move->tofile == move->fromfile;
10
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 }
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
37 _Bool rook_isblocked(GameState *gamestate, Move *move) {
21
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
38
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
39 if (move->torow == move->fromrow) {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
40 int d = move->tofile > move->fromfile ? 1 : -1;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
41 uint8_t f = move->fromfile;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
42 while (f != move->tofile-d) {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
43 f += d;
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
44 if (gamestate->board[move->fromrow][f]) {
21
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
45 return 1;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
46 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
47 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
48 } else {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
49 int d = move->torow > move->fromrow ? 1 : -1;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
50 uint8_t r = move->fromrow;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
51 while (r != move->torow - d) {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
52 r += d;
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
53 if (gamestate->board[r][move->fromfile]) {
21
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
54 return 1;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
55 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
56 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
57 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
58
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
59 return 0;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
60 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
61
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
62 static int rook_getloc_fixedrow(GameState *gamestate, Move *move) {
21
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
63 uint8_t file = POS_UNSPECIFIED;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
64 for (uint8_t f = 0 ; f < 8 ; f++) {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
65 if (gamestate->board[move->fromrow][f] == move->piece) {
21
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
66 if (file == POS_UNSPECIFIED) {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
67 file = f;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
68 } else {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
69 return AMBIGUOUS_MOVE;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
70 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
71 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
72 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
73 if (file == POS_UNSPECIFIED) {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
74 return INVALID_POSITION;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
75 } else {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
76 move->fromfile = file;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
77 return VALID_MOVE_SYNTAX;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
78 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
79 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
80
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
81 static int rook_getloc_fixedfile(GameState *gamestate, Move *move) {
21
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
82 uint8_t row = POS_UNSPECIFIED;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
83 for (uint8_t r = 0 ; r < 8 ; r++) {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
84 if (gamestate->board[r][move->fromfile] == move->piece) {
21
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
85 if (row == POS_UNSPECIFIED) {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
86 row = r;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
87 } else {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
88 return AMBIGUOUS_MOVE;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
89 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
90 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
91 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
92 if (row == POS_UNSPECIFIED) {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
93 return INVALID_POSITION;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
94 } else {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
95 move->fromrow = row;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
96 return VALID_MOVE_SYNTAX;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
97 }
10
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 }
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
100 int rook_getlocation(GameState *gamestate, Move *move) {
21
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
101
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
102 if (move->fromfile != POS_UNSPECIFIED) {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
103 if (move->fromfile == move->tofile) {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
104 return rook_getloc_fixedfile(gamestate, move);
21
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
105 } else {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
106 if (gamestate->board[move->torow][move->fromfile] == move->piece) {
21
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
107 move->fromrow = move->torow;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
108 return VALID_MOVE_SYNTAX;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
109 } else {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
110 return INVALID_POSITION;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
111 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
112 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
113 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
114
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
115 if (move->fromrow != POS_UNSPECIFIED) {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
116 if (move->fromrow == move->torow) {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
117 return rook_getloc_fixedrow(gamestate, move);
21
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
118 } else {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
119 if (gamestate->board[move->fromrow][move->tofile] == move->piece) {
21
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
120 move->fromfile = move->tofile;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
121 return VALID_MOVE_SYNTAX;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
122 } else {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
123 return INVALID_POSITION;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
124 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
125 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
126 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
127
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
128 Move chkrowmove = *move, chkfilemove = *move;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
129
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
130 chkrowmove.fromrow = move->torow;
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
131 int chkrow = rook_getloc_fixedrow(gamestate, &chkrowmove);
21
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
132
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
133 chkfilemove.fromfile = move->tofile;
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
134 int chkfile = rook_getloc_fixedfile(gamestate, &chkfilemove);
21
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
135
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
136 if ((chkrow == VALID_MOVE_SYNTAX && chkfile == VALID_MOVE_SYNTAX) ||
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
137 chkrow == AMBIGUOUS_MOVE || chkfile == AMBIGUOUS_MOVE) {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
138 return AMBIGUOUS_MOVE;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
139 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
140
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
141 if (chkrow == VALID_MOVE_SYNTAX) {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
142 *move = chkrowmove;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
143 return VALID_MOVE_SYNTAX;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
144 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
145
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
146 if (chkfile == VALID_MOVE_SYNTAX) {
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
147 *move = chkfilemove;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
148 return VALID_MOVE_SYNTAX;
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
149 }
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
150
2e5846019b4f implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
151 return INVALID_POSITION;
10
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 }

mercurial