src/chess/pawn.c

changeset 169
9962f5d98764
parent 163
2a6d83f4677e
child 170
bde99d803caf
equal deleted inserted replaced
168:663676cfef6e 169:9962f5d98764
75 } else { 75 } else {
76 return piece_at(gamestate, mdst(move)) || 76 return piece_at(gamestate, mdst(move)) ||
77 gamestate->board[(move->fromrow+move->torow)/2][move->tofile]; 77 gamestate->board[(move->fromrow+move->torow)/2][move->tofile];
78 } 78 }
79 } 79 }
80
81 size_t pawn_moves(GameState *gamestate, Color c, Row r, File f, Move *moves) {
82 size_t count = 0;
83 int rowdelta = c == WHITE ? 1 : -1;
84 int promotionrow = c == WHITE ? 7 : 0;
85 int startrow = c == WHITE ? 1 : 6;
86 const int targets[4][3] = {
87 {r + rowdelta, f, 0},
88 {r + rowdelta, f - 1, 1},
89 {r + rowdelta, f + 1, 1},
90 {r + rowdelta * 2, f, 0}
91 };
92
93 for (size_t i = 0 ; i < 4 ; i++) {
94 int row = targets[i][0];
95 int file = targets[i][1];
96
97 if (i == 3 && r != startrow) {
98 continue;
99 }
100
101 if (isidx(row) && isidx(file)) {
102 moves[count] = (Move){0};
103 moves[count].piece = mkpiece(PAWN, c);
104 moves[count].fromrow = r;
105 moves[count].fromfile = f;
106 moves[count].torow = row;
107 moves[count].tofile = file;
108 moves[count].capture = targets[i][2];
109
110 if (row == promotionrow) {
111 moves[count].promotion = mkpiece(QUEEN, c);
112 }
113
114 count++;
115 }
116 }
117
118 return count;
119 }

mercurial