Tue, 25 Aug 2026 18:59:18 +0200
rename Row to Rank
+ fix increase type safety where we overlooked it before
relates to #956
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2016 Mike Becker. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * */ #include "pawn.h" #include "rules.h" bool pawn_chkrules(const GameState *gamestate, const Move *move) { int8_t d = piece_color(move->piece) == WHITE ? -1 : 1; if (move->torank == (d < 0 ? 7 : 0)) { if (move->promotion) { unsigned ppiecetype = piece_type(move->promotion); if (!ppiecetype || ppiecetype == PAWN || ppiecetype == KING) { return false; } } else { return false; } } else { if (move->promotion) { return false; } } if (move->capture) { if (move->fromrank == move->torank + d && ( move->fromfile == move->tofile + 1 || move->fromfile == move->tofile - 1)) { return piece_at(gamestate, move->torank, move->tofile) || enpassant_threat_exists(gamestate, move->fromrank, move->tofile); } else { return false; } } else { if (move->fromfile == move->tofile) { return (move->fromrank == move->torank + d) || (move->fromrank == (d < 0 ? 1 : 6) && /* advanced first move */ move->fromrank == move->torank + d*2); } else { return false; } } } bool pawn_isblocked(const GameState *gamestate, const Move *move) { if (move->torank == move->fromrank + 1 || move->torank == move->fromrank - 1) { return piece_at(gamestate, move->torank, move->tofile) && !move->capture; } else { return piece_at(gamestate, move->torank, move->tofile) || gamestate->board[(move->fromrank + move->torank) / 2][move->tofile]; } } size_t pawn_moves(const GameState *gamestate, Color c, Rank r, File f, Move *moves) { (void) gamestate; /* no (theoretical) move depends on the game state */ size_t count = 0; int rankdelta = c == WHITE ? 1 : -1; int promotionrank = c == WHITE ? 7 : 0; int startrank = c == WHITE ? 1 : 6; const int targets[4][3] = { {r + rankdelta, f, 0}, {r + rankdelta, f - 1, 1}, {r + rankdelta, f + 1, 1}, {r + rankdelta * 2, f, 0} }; for (size_t i = 0 ; i < 4 ; i++) { Rank rank = targets[i][0]; File file = targets[i][1]; if (i == 3 && r != startrank) { continue; } if (isidx(rank) && isidx(file)) { moves[count] = (Move){0}; moves[count].piece = mkpiece(PAWN, c); moves[count].fromrank = r; moves[count].fromfile = f; moves[count].torank = rank; moves[count].tofile = file; moves[count].capture = targets[i][2]; if (rank == promotionrank) { moves[count].promotion = mkpiece(QUEEN, c); } count++; } } return count; }