src/chess/pawn.c

changeset 169
9962f5d98764
parent 163
2a6d83f4677e
child 170
bde99d803caf
--- a/src/chess/pawn.c	Sun Aug 02 15:19:47 2026 +0200
+++ b/src/chess/pawn.c	Thu Aug 06 14:45:55 2026 +0200
@@ -77,3 +77,43 @@
             gamestate->board[(move->fromrow+move->torow)/2][move->tofile];
     }
 }
+
+size_t pawn_moves(GameState *gamestate, Color c, Row r, File f, Move *moves) {
+    size_t count = 0;
+    int rowdelta = c == WHITE ? 1 : -1;
+    int promotionrow = c == WHITE ? 7 : 0;
+    int startrow = c == WHITE ? 1 : 6;
+    const int targets[4][3] = {
+        {r + rowdelta,     f, 0},
+        {r + rowdelta, f - 1, 1},
+        {r + rowdelta, f + 1, 1},
+        {r + rowdelta * 2, f, 0}
+    };
+
+    for (size_t i = 0 ; i < 4 ; i++) {
+        int row = targets[i][0];
+        int file = targets[i][1];
+
+        if (i == 3 && r != startrow) {
+            continue;
+        }
+
+        if (isidx(row) && isidx(file)) {
+            moves[count] = (Move){0};
+            moves[count].piece = mkpiece(PAWN, c);
+            moves[count].fromrow = r;
+            moves[count].fromfile = f;
+            moves[count].torow = row;
+            moves[count].tofile = file;
+            moves[count].capture = targets[i][2];
+
+            if (row == promotionrow) {
+                moves[count].promotion = mkpiece(QUEEN, c);
+            }
+
+            count++;
+        }
+    }
+
+    return count;
+}

mercurial