src/rules/bishop.c

changeset 17
2aed5418e142
parent 16
a298c6637c30
child 18
6008840b859e
equal deleted inserted replaced
16:a298c6637c30 17:2aed5418e142
27 * 27 *
28 */ 28 */
29 29
30 #include "bishop.h" 30 #include "bishop.h"
31 #include "rules.h" 31 #include "rules.h"
32 #include <math.h>
32 33
33 _Bool bishop_chkrules(Move* move) { 34 _Bool bishop_chkrules(Move* move) {
34 // TODO: implement 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 TRUE;
50 }
51 } while (x != move->tofile && y != move->torow);
52
35 return FALSE; 53 return FALSE;
36 } 54 }
37 55
38 _Bool bishop_isblocked(Board board, Move *move) { 56 static int bishop_getloc_fixedfile(Board board, Move *move) {
39 // TODO: implement 57 uint8_t d = abs(move->fromfile - move->tofile);
40 return TRUE; 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;
41 } 86 }
42 87
43 int bishop_getlocation(Board board, Move *move) { 88 int bishop_getlocation(Board board, Move *move) {
44 // TODO: implement 89
45 return INVALID_MOVE_SYNTAX; 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 = FALSE;
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 = TRUE;
108 move->fromrow = row;
109 move->fromfile = file;
110 }
111 file = move->tofile - d;
112 if (isfile(file) && board[row][file] == move->piece) {
113 if (amb) {
114 return AMBIGUOUS_MOVE;
115 }
116 amb = TRUE;
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 }
46 } 128 }

mercurial