29 |
29 |
30 #include "rules.h" |
30 #include "rules.h" |
31 #include "rook.h" |
31 #include "rook.h" |
32 |
32 |
33 _Bool rook_chkrules(Move *move) { |
33 _Bool rook_chkrules(Move *move) { |
34 // TODO: implement |
34 return move->torow == move->fromrow || move->tofile == move->fromfile; |
|
35 } |
|
36 |
|
37 _Bool rook_isblocked(Board board, Move *move) { |
|
38 |
|
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 (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 (board[r][move->fromfile]) { |
|
54 return 1; |
|
55 } |
|
56 } |
|
57 } |
|
58 |
35 return 0; |
59 return 0; |
36 } |
60 } |
37 |
61 |
38 _Bool rook_isblocked(Board board, Move *move) { |
62 static int rook_getloc_fixedrow(Board board, Move *move) { |
39 // TODO: implement |
63 uint8_t file = POS_UNSPECIFIED; |
40 return 1; |
64 for (uint8_t f = 0 ; f < 8 ; f++) { |
|
65 if (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 } |
|
80 |
|
81 static int rook_getloc_fixedfile(Board board, Move *move) { |
|
82 uint8_t row = POS_UNSPECIFIED; |
|
83 for (uint8_t r = 0 ; r < 8 ; r++) { |
|
84 if (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 } |
41 } |
98 } |
42 |
99 |
43 int rook_getlocation(Board board, Move *move) { |
100 int rook_getlocation(Board board, Move *move) { |
44 // TODO: implement |
101 |
45 return INVALID_MOVE_SYNTAX; |
102 if (move->fromfile != POS_UNSPECIFIED) { |
|
103 if (move->fromfile == move->tofile) { |
|
104 return rook_getloc_fixedfile(board, move); |
|
105 } else { |
|
106 if (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 } |
|
114 |
|
115 if (move->fromrow != POS_UNSPECIFIED) { |
|
116 if (move->fromrow == move->torow) { |
|
117 return rook_getloc_fixedrow(board, move); |
|
118 } else { |
|
119 if (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 } |
|
127 |
|
128 Move chkrowmove = *move, chkfilemove = *move; |
|
129 |
|
130 chkrowmove.fromrow = move->torow; |
|
131 int chkrow = rook_getloc_fixedrow(board, &chkrowmove); |
|
132 |
|
133 chkfilemove.fromfile = move->tofile; |
|
134 int chkfile = rook_getloc_fixedfile(board, &chkfilemove); |
|
135 |
|
136 if ((chkrow == VALID_MOVE_SYNTAX && chkfile == VALID_MOVE_SYNTAX) || |
|
137 chkrow == AMBIGUOUS_MOVE || chkfile == AMBIGUOUS_MOVE) { |
|
138 return AMBIGUOUS_MOVE; |
|
139 } |
|
140 |
|
141 if (chkrow == VALID_MOVE_SYNTAX) { |
|
142 *move = chkrowmove; |
|
143 return VALID_MOVE_SYNTAX; |
|
144 } |
|
145 |
|
146 if (chkfile == VALID_MOVE_SYNTAX) { |
|
147 *move = chkfilemove; |
|
148 return VALID_MOVE_SYNTAX; |
|
149 } |
|
150 |
|
151 return INVALID_POSITION; |
46 } |
152 } |