|
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 */ |
|
29 |
|
30 #include "bishop.h" |
|
31 #include "rules.h" |
|
32 #include <stdlib.h> |
|
33 |
|
34 _Bool bishop_chkrules(Move* move) { |
|
35 return abs(move->torow-move->fromrow) == abs(move->tofile-move->fromfile); |
|
36 } |
|
37 |
|
38 _Bool bishop_isblocked(Board board, Move *move) { |
|
39 int dy = move->torow > move->fromrow ? 1 : -1; |
|
40 int dx = move->tofile > move->fromfile ? 1 : -1; |
|
41 |
|
42 uint8_t y = move->fromrow; |
|
43 uint8_t x = move->fromfile; |
|
44 |
|
45 do { |
|
46 x += dx; |
|
47 y += dy; |
|
48 if (board[y][x]) { |
|
49 return 1; |
|
50 } |
|
51 } while (x != move->tofile && y != move->torow); |
|
52 |
|
53 return 0; |
|
54 } |
|
55 |
|
56 static int bishop_getloc_fixedfile(Board board, Move *move) { |
|
57 uint8_t d = abs(move->fromfile - move->tofile); |
|
58 if (board[move->torow - d][move->fromfile] == move->piece) { |
|
59 move->fromrow = move->torow - d; |
|
60 } |
|
61 if (board[move->torow + d][move->fromfile] == move->piece) { |
|
62 if (move->fromrow == POS_UNSPECIFIED) { |
|
63 move->fromrow = move->torow + d; |
|
64 } else { |
|
65 return AMBIGUOUS_MOVE; /* rare situation after promotion */ |
|
66 } |
|
67 } |
|
68 return move->fromrow == POS_UNSPECIFIED ? |
|
69 INVALID_POSITION : VALID_MOVE_SYNTAX; |
|
70 } |
|
71 |
|
72 static int bishop_getloc_fixedrow(Board board, Move *move) { |
|
73 uint8_t d = abs(move->fromrow - move->torow); |
|
74 if (board[move->fromrow][move->tofile - d] == move->piece) { |
|
75 move->fromfile = move->tofile - d; |
|
76 } |
|
77 if (board[move->fromrow][move->tofile + d] == move->piece) { |
|
78 if (move->fromfile == POS_UNSPECIFIED) { |
|
79 move->fromfile = move->tofile + d; |
|
80 } else { |
|
81 return AMBIGUOUS_MOVE; /* rare situation after promotion */ |
|
82 } |
|
83 } |
|
84 return move->fromfile == POS_UNSPECIFIED ? |
|
85 INVALID_POSITION : VALID_MOVE_SYNTAX; |
|
86 } |
|
87 |
|
88 int bishop_getlocation(Board board, Move *move) { |
|
89 |
|
90 if (move->fromfile != POS_UNSPECIFIED) { |
|
91 return bishop_getloc_fixedfile(board, move); |
|
92 } |
|
93 |
|
94 if (move->fromrow != POS_UNSPECIFIED) { |
|
95 return bishop_getloc_fixedrow(board, move); |
|
96 } |
|
97 |
|
98 _Bool amb = 0; |
|
99 for (int d = -7 ; d < 8 ; d++) { |
|
100 uint8_t row = move->torow + d; |
|
101 if (isidx(row)) { |
|
102 uint8_t file = move->tofile + d; |
|
103 if (isidx(file) && board[row][file] == move->piece) { |
|
104 if (amb) { |
|
105 return AMBIGUOUS_MOVE; |
|
106 } |
|
107 amb = 1; |
|
108 move->fromrow = row; |
|
109 move->fromfile = file; |
|
110 } |
|
111 file = move->tofile - d; |
|
112 if (isidx(file) && board[row][file] == move->piece) { |
|
113 if (amb) { |
|
114 return AMBIGUOUS_MOVE; |
|
115 } |
|
116 amb = 1; |
|
117 move->fromrow = row; |
|
118 move->fromfile = file; |
|
119 } |
|
120 } |
|
121 } |
|
122 |
|
123 if (move->fromrow == POS_UNSPECIFIED || move->fromfile == POS_UNSPECIFIED) { |
|
124 return INVALID_POSITION; |
|
125 } else { |
|
126 return VALID_MOVE_SYNTAX; |
|
127 } |
|
128 } |