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