src/chess/bishop.c

changeset 47
d726e4b46c33
parent 23
824c9522ce66
child 55
54ea19938d57
equal deleted inserted replaced
46:4dcfb4c58b6d 47:d726e4b46c33
50 } 50 }
51 } 51 }
52 52
53 return 0; 53 return 0;
54 } 54 }
55
56 static int bishop_getloc_fixedfile(GameState *gamestate, Move *move) {
57 uint8_t d = abs(move->fromfile - move->tofile);
58 if (gamestate->board[move->torow - d][move->fromfile] == move->piece) {
59 move->fromrow = move->torow - d;
60 }
61 if (gamestate->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(GameState *gamestate, Move *move) {
73 uint8_t d = abs(move->fromrow - move->torow);
74 if (gamestate->board[move->fromrow][move->tofile - d] == move->piece) {
75 move->fromfile = move->tofile - d;
76 }
77 if (gamestate->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(GameState *gamestate, Move *move) {
89
90 if (move->fromfile != POS_UNSPECIFIED) {
91 return bishop_getloc_fixedfile(gamestate, move);
92 }
93
94 if (move->fromrow != POS_UNSPECIFIED) {
95 return bishop_getloc_fixedrow(gamestate, 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) && gamestate->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) && gamestate->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 }

mercurial