src/chess/bishop.c

changeset 23
824c9522ce66
parent 21
2e5846019b4f
child 47
d726e4b46c33
equal deleted inserted replaced
22:41bbfd4d17a3 23:824c9522ce66
33 33
34 _Bool bishop_chkrules(Move* move) { 34 _Bool bishop_chkrules(Move* move) {
35 return abs(move->torow-move->fromrow) == abs(move->tofile-move->fromfile); 35 return abs(move->torow-move->fromrow) == abs(move->tofile-move->fromfile);
36 } 36 }
37 37
38 _Bool bishop_isblocked(Board board, Move *move) { 38 _Bool bishop_isblocked(GameState *gamestate, Move *move) {
39 int dy = move->torow > move->fromrow ? 1 : -1; 39 int dy = move->torow > move->fromrow ? 1 : -1;
40 int dx = move->tofile > move->fromfile ? 1 : -1; 40 int dx = move->tofile > move->fromfile ? 1 : -1;
41 41
42 uint8_t y = move->fromrow; 42 uint8_t y = move->fromrow;
43 uint8_t x = move->fromfile; 43 uint8_t x = move->fromfile;
44 44
45 while (x != move->tofile-dx && y != move->torow-dy) { 45 while (x != move->tofile-dx && y != move->torow-dy) {
46 x += dx; 46 x += dx;
47 y += dy; 47 y += dy;
48 if (board[y][x]) { 48 if (gamestate->board[y][x]) {
49 return 1; 49 return 1;
50 } 50 }
51 } 51 }
52 52
53 return 0; 53 return 0;
54 } 54 }
55 55
56 static int bishop_getloc_fixedfile(Board board, Move *move) { 56 static int bishop_getloc_fixedfile(GameState *gamestate, Move *move) {
57 uint8_t d = abs(move->fromfile - move->tofile); 57 uint8_t d = abs(move->fromfile - move->tofile);
58 if (board[move->torow - d][move->fromfile] == move->piece) { 58 if (gamestate->board[move->torow - d][move->fromfile] == move->piece) {
59 move->fromrow = move->torow - d; 59 move->fromrow = move->torow - d;
60 } 60 }
61 if (board[move->torow + d][move->fromfile] == move->piece) { 61 if (gamestate->board[move->torow + d][move->fromfile] == move->piece) {
62 if (move->fromrow == POS_UNSPECIFIED) { 62 if (move->fromrow == POS_UNSPECIFIED) {
63 move->fromrow = move->torow + d; 63 move->fromrow = move->torow + d;
64 } else { 64 } else {
65 return AMBIGUOUS_MOVE; /* rare situation after promotion */ 65 return AMBIGUOUS_MOVE; /* rare situation after promotion */
66 } 66 }
67 } 67 }
68 return move->fromrow == POS_UNSPECIFIED ? 68 return move->fromrow == POS_UNSPECIFIED ?
69 INVALID_POSITION : VALID_MOVE_SYNTAX; 69 INVALID_POSITION : VALID_MOVE_SYNTAX;
70 } 70 }
71 71
72 static int bishop_getloc_fixedrow(Board board, Move *move) { 72 static int bishop_getloc_fixedrow(GameState *gamestate, Move *move) {
73 uint8_t d = abs(move->fromrow - move->torow); 73 uint8_t d = abs(move->fromrow - move->torow);
74 if (board[move->fromrow][move->tofile - d] == move->piece) { 74 if (gamestate->board[move->fromrow][move->tofile - d] == move->piece) {
75 move->fromfile = move->tofile - d; 75 move->fromfile = move->tofile - d;
76 } 76 }
77 if (board[move->fromrow][move->tofile + d] == move->piece) { 77 if (gamestate->board[move->fromrow][move->tofile + d] == move->piece) {
78 if (move->fromfile == POS_UNSPECIFIED) { 78 if (move->fromfile == POS_UNSPECIFIED) {
79 move->fromfile = move->tofile + d; 79 move->fromfile = move->tofile + d;
80 } else { 80 } else {
81 return AMBIGUOUS_MOVE; /* rare situation after promotion */ 81 return AMBIGUOUS_MOVE; /* rare situation after promotion */
82 } 82 }
83 } 83 }
84 return move->fromfile == POS_UNSPECIFIED ? 84 return move->fromfile == POS_UNSPECIFIED ?
85 INVALID_POSITION : VALID_MOVE_SYNTAX; 85 INVALID_POSITION : VALID_MOVE_SYNTAX;
86 } 86 }
87 87
88 int bishop_getlocation(Board board, Move *move) { 88 int bishop_getlocation(GameState *gamestate, Move *move) {
89 89
90 if (move->fromfile != POS_UNSPECIFIED) { 90 if (move->fromfile != POS_UNSPECIFIED) {
91 return bishop_getloc_fixedfile(board, move); 91 return bishop_getloc_fixedfile(gamestate, move);
92 } 92 }
93 93
94 if (move->fromrow != POS_UNSPECIFIED) { 94 if (move->fromrow != POS_UNSPECIFIED) {
95 return bishop_getloc_fixedrow(board, move); 95 return bishop_getloc_fixedrow(gamestate, move);
96 } 96 }
97 97
98 _Bool amb = 0; 98 _Bool amb = 0;
99 for (int d = -7 ; d < 8 ; d++) { 99 for (int d = -7 ; d < 8 ; d++) {
100 uint8_t row = move->torow + d; 100 uint8_t row = move->torow + d;
101 if (isidx(row)) { 101 if (isidx(row)) {
102 uint8_t file = move->tofile + d; 102 uint8_t file = move->tofile + d;
103 if (isidx(file) && board[row][file] == move->piece) { 103 if (isidx(file) && gamestate->board[row][file] == move->piece) {
104 if (amb) { 104 if (amb) {
105 return AMBIGUOUS_MOVE; 105 return AMBIGUOUS_MOVE;
106 } 106 }
107 amb = 1; 107 amb = 1;
108 move->fromrow = row; 108 move->fromrow = row;
109 move->fromfile = file; 109 move->fromfile = file;
110 } 110 }
111 file = move->tofile - d; 111 file = move->tofile - d;
112 if (isidx(file) && board[row][file] == move->piece) { 112 if (isidx(file) && gamestate->board[row][file] == move->piece) {
113 if (amb) { 113 if (amb) {
114 return AMBIGUOUS_MOVE; 114 return AMBIGUOUS_MOVE;
115 } 115 }
116 amb = 1; 116 amb = 1;
117 move->fromrow = row; 117 move->fromrow = row;

mercurial