src/chess/knight.c

changeset 23
824c9522ce66
parent 19
6a26114297a1
child 47
d726e4b46c33
equal deleted inserted replaced
22:41bbfd4d17a3 23:824c9522ce66
36 int dy = abs(move->fromrow - move->torow); 36 int dy = abs(move->fromrow - move->torow);
37 37
38 return (dx == 2 && dy == 1) || (dx == 1 && dy == 2); 38 return (dx == 2 && dy == 1) || (dx == 1 && dy == 2);
39 } 39 }
40 40
41 static int knight_getloc_fixedrow(Board board, Move *move) { 41 static int knight_getloc_fixedrow(GameState *gamestate, Move *move) {
42 int d = 3 - abs(move->fromrow - move->torow); 42 int d = 3 - abs(move->fromrow - move->torow);
43 43
44 if (d == 1 || d == 2) { 44 if (d == 1 || d == 2) {
45 if (move->tofile < 6 && 45 if (move->tofile < 6 &&
46 board[move->fromrow][move->tofile + d] == move->piece) { 46 gamestate->board[move->fromrow][move->tofile + d] == move->piece) {
47 if (move->fromfile == POS_UNSPECIFIED) { 47 if (move->fromfile == POS_UNSPECIFIED) {
48 move->fromfile = move->tofile + d; 48 move->fromfile = move->tofile + d;
49 return VALID_MOVE_SYNTAX; 49 return VALID_MOVE_SYNTAX;
50 } else { 50 } else {
51 return AMBIGUOUS_MOVE; 51 return AMBIGUOUS_MOVE;
52 } 52 }
53 } 53 }
54 if (move->tofile > 1 && 54 if (move->tofile > 1 &&
55 board[move->fromrow][move->tofile - d] == move->piece) { 55 gamestate->board[move->fromrow][move->tofile - d] == move->piece) {
56 if (move->fromfile == POS_UNSPECIFIED) { 56 if (move->fromfile == POS_UNSPECIFIED) {
57 move->fromfile = move->tofile - d; 57 move->fromfile = move->tofile - d;
58 return VALID_MOVE_SYNTAX; 58 return VALID_MOVE_SYNTAX;
59 } else { 59 } else {
60 return AMBIGUOUS_MOVE; 60 return AMBIGUOUS_MOVE;
63 } 63 }
64 64
65 return INVALID_POSITION; 65 return INVALID_POSITION;
66 } 66 }
67 67
68 static int knight_getloc_fixedfile(Board board, Move *move) { 68 static int knight_getloc_fixedfile(GameState *gamestate, Move *move) {
69 int d = 3 - abs(move->fromfile - move->tofile); 69 int d = 3 - abs(move->fromfile - move->tofile);
70 70
71 if (d == 1 || d == 2) { 71 if (d == 1 || d == 2) {
72 if (move->torow < 6 && 72 if (move->torow < 6 &&
73 board[move->torow + d][move->fromfile] == move->piece) { 73 gamestate->board[move->torow + d][move->fromfile] == move->piece) {
74 if (move->fromrow == POS_UNSPECIFIED) { 74 if (move->fromrow == POS_UNSPECIFIED) {
75 move->fromrow = move->torow + d; 75 move->fromrow = move->torow + d;
76 return VALID_MOVE_SYNTAX; 76 return VALID_MOVE_SYNTAX;
77 } else { 77 } else {
78 return AMBIGUOUS_MOVE; 78 return AMBIGUOUS_MOVE;
79 } 79 }
80 } 80 }
81 if (move->torow > 1 && 81 if (move->torow > 1 &&
82 board[move->torow - d][move->fromfile] == move->piece) { 82 gamestate->board[move->torow - d][move->fromfile] == move->piece) {
83 if (move->fromrow == POS_UNSPECIFIED) { 83 if (move->fromrow == POS_UNSPECIFIED) {
84 move->fromrow = move->torow - d; 84 move->fromrow = move->torow - d;
85 return VALID_MOVE_SYNTAX; 85 return VALID_MOVE_SYNTAX;
86 } else { 86 } else {
87 return AMBIGUOUS_MOVE; 87 return AMBIGUOUS_MOVE;
90 } 90 }
91 91
92 return INVALID_POSITION; 92 return INVALID_POSITION;
93 } 93 }
94 94
95 int knight_getlocation(Board board, Move *move) { 95 int knight_getlocation(GameState *gamestate, Move *move) {
96 96
97 if (move->fromfile != POS_UNSPECIFIED) { 97 if (move->fromfile != POS_UNSPECIFIED) {
98 return knight_getloc_fixedfile(board, move); 98 return knight_getloc_fixedfile(gamestate, move);
99 } 99 }
100 100
101 if (move->fromrow != POS_UNSPECIFIED) { 101 if (move->fromrow != POS_UNSPECIFIED) {
102 return knight_getloc_fixedrow(board, move); 102 return knight_getloc_fixedrow(gamestate, move);
103 } 103 }
104 104
105 for (int x = -2 ; x <= 2 ; x++) { 105 for (int x = -2 ; x <= 2 ; x++) {
106 if (x == 0) { 106 if (x == 0) {
107 continue; 107 continue;
111 continue; 111 continue;
112 } 112 }
113 uint8_t cx = move->tofile + x; 113 uint8_t cx = move->tofile + x;
114 uint8_t cy = move->torow + y; 114 uint8_t cy = move->torow + y;
115 115
116 if (isidx(cx) && isidx(cy) && board[cy][cx] == move->piece) { 116 if (isidx(cx) && isidx(cy)
117 && gamestate->board[cy][cx] == move->piece) {
117 if (move->fromfile == POS_UNSPECIFIED 118 if (move->fromfile == POS_UNSPECIFIED
118 && move->fromrow == POS_UNSPECIFIED) { 119 && move->fromrow == POS_UNSPECIFIED) {
119 move->fromfile = cx; 120 move->fromfile = cx;
120 move->fromrow = cy; 121 move->fromrow = cy;
121 } else { 122 } else {

mercurial