1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:57:44 +00:00

Chess: Fix signed/unsigned issues

Make everything signed so that we don't have to deal with silly casting
issues thoughout the Chess code. I am unsure if this affects the chess
AI negatively, it seems just as "intelligent" before and after this
change :^)
This commit is contained in:
Jean-Baptiste Boric 2021-05-16 14:55:20 +02:00 committed by Linus Groh
parent d0eb376520
commit 0262a99a1f
3 changed files with 25 additions and 27 deletions

View file

@ -152,12 +152,12 @@ Move Move::from_algebraic(const StringView& algebraic, const Color turn, const B
return IterationDecision::Break;
}
} else if (move_string.characters()[0] <= 57) {
if (square.rank == (unsigned)(move_string.characters()[0] - '0')) {
if (square.rank == (move_string.characters()[0] - '0')) {
move.from = square;
return IterationDecision::Break;
}
} else {
if (square.file == (unsigned)(move_string.characters()[0] - 'a')) {
if (square.file == (move_string.characters()[0] - 'a')) {
move.from = square;
return IterationDecision::Break;
}
@ -222,19 +222,19 @@ String Move::to_algebraic() const
Board::Board()
{
// Fill empty spaces.
for (unsigned rank = 2; rank < 6; ++rank) {
for (unsigned file = 0; file < 8; ++file) {
for (int rank = 2; rank < 6; ++rank) {
for (int file = 0; file < 8; ++file) {
set_piece({ rank, file }, EmptyPiece);
}
}
// Fill white pawns.
for (unsigned file = 0; file < 8; ++file) {
for (int file = 0; file < 8; ++file) {
set_piece({ 1, file }, { Color::White, Type::Pawn });
}
// Fill black pawns.
for (unsigned file = 0; file < 8; ++file) {
for (int file = 0; file < 8; ++file) {
set_piece({ 6, file }, { Color::Black, Type::Pawn });
}
@ -265,8 +265,8 @@ String Board::to_fen() const
// 1. Piece placement
int empty = 0;
for (unsigned rank = 0; rank < 8; rank++) {
for (unsigned file = 0; file < 8; file++) {
for (int rank = 0; rank < 8; rank++) {
for (int file = 0; file < 8; file++) {
const Piece p(get_piece({ 7 - rank, file }));
if (p.type == Type::None) {
empty++;
@ -328,15 +328,13 @@ String Board::to_fen() const
Piece Board::get_piece(const Square& square) const
{
VERIFY(square.rank < 8);
VERIFY(square.file < 8);
VERIFY(square.in_bounds());
return m_board[square.rank][square.file];
}
Piece Board::set_piece(const Square& square, const Piece& piece)
{
VERIFY(square.rank < 8);
VERIFY(square.file < 8);
VERIFY(square.in_bounds());
return m_board[square.rank][square.file] = piece;
}
@ -354,7 +352,7 @@ bool Board::is_legal_promotion(const Move& move, Color color) const
return false;
}
unsigned promotion_rank = (color == Color::White) ? 7 : 0;
int promotion_rank = (color == Color::White) ? 7 : 0;
if (move.to.rank != promotion_rank && move.promote_to != Type::None) {
// attempted promotion from invalid rank
@ -419,13 +417,13 @@ bool Board::is_legal_no_check(const Move& move, Color color) const
// attempted move of opponent's piece
return false;
if (move.to.rank > 7 || move.to.file > 7)
if (!move.to.in_bounds())
// attempted move outside of board
return false;
if (piece.type == Type::Pawn) {
int dir = (color == Color::White) ? +1 : -1;
unsigned start_rank = (color == Color::White) ? 1 : 6;
int start_rank = (color == Color::White) ? 1 : 6;
if (move.from.rank == start_rank && move.to.rank == move.from.rank + (2 * dir) && move.to.file == move.from.file
&& get_piece(move.to).type == Type::None && get_piece({ move.from.rank + dir, move.from.file }).type == Type::None) {
@ -443,8 +441,8 @@ bool Board::is_legal_no_check(const Move& move, Color color) const
}
if (move.to.file == move.from.file + 1 || move.to.file == move.from.file - 1) {
unsigned other_start_rank = (color == Color::White) ? 6 : 1;
unsigned en_passant_rank = (color == Color::White) ? 4 : 3;
int other_start_rank = (color == Color::White) ? 6 : 1;
int en_passant_rank = (color == Color::White) ? 4 : 3;
Move en_passant_last_move = { { other_start_rank, move.to.file }, { en_passant_rank, move.to.file } };
if (get_piece(move.to).color == opposing_color(color)) {
// Pawn capture.
@ -843,7 +841,7 @@ bool Board::is_promotion_move(const Move& move, Color color) const
if (color == Color::None)
color = turn();
unsigned promotion_rank = (color == Color::White) ? 7 : 0;
int promotion_rank = (color == Color::White) ? 7 : 0;
if (move.to.rank != promotion_rank)
return false;