mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:27: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:
parent
d0eb376520
commit
0262a99a1f
3 changed files with 25 additions and 27 deletions
|
@ -37,7 +37,7 @@ void ChessWidget::paint_event(GUI::PaintEvent& event)
|
|||
|
||||
size_t tile_width = frame_inner_rect().width() / 8;
|
||||
size_t tile_height = frame_inner_rect().height() / 8;
|
||||
unsigned coord_rank_file = (side() == Chess::Color::White) ? 0 : 7;
|
||||
int coord_rank_file = (side() == Chess::Color::White) ? 0 : 7;
|
||||
|
||||
Chess::Board& active_board = (m_playback ? board_playback() : board());
|
||||
|
||||
|
@ -362,13 +362,13 @@ void ChessWidget::set_piece_set(const StringView& set)
|
|||
|
||||
Chess::Square ChessWidget::mouse_to_square(GUI::MouseEvent& event) const
|
||||
{
|
||||
unsigned tile_width = frame_inner_rect().width() / 8;
|
||||
unsigned tile_height = frame_inner_rect().height() / 8;
|
||||
int tile_width = frame_inner_rect().width() / 8;
|
||||
int tile_height = frame_inner_rect().height() / 8;
|
||||
|
||||
if (side() == Chess::Color::White) {
|
||||
return { (unsigned)(7 - (event.y() / tile_height)), (unsigned)(event.x() / tile_width) };
|
||||
return { 7 - (event.y() / tile_height), event.x() / tile_width };
|
||||
} else {
|
||||
return { (unsigned)(event.y() / tile_height), (unsigned)(7 - (event.x() / tile_width)) };
|
||||
return { event.y() / tile_height, 7 - (event.x() / tile_width) };
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -55,10 +55,10 @@ struct Piece {
|
|||
constexpr Piece EmptyPiece = { Color::None, Type::None };
|
||||
|
||||
struct Square {
|
||||
unsigned rank; // zero indexed;
|
||||
unsigned file;
|
||||
int rank; // zero indexed;
|
||||
int file;
|
||||
Square(const StringView& name);
|
||||
Square(const unsigned& rank, const unsigned& file)
|
||||
Square(const int& rank, const int& file)
|
||||
: rank(rank)
|
||||
, file(file)
|
||||
{
|
||||
|
@ -76,7 +76,7 @@ struct Square {
|
|||
}
|
||||
}
|
||||
|
||||
bool in_bounds() const { return rank < 8 && file < 8; }
|
||||
bool in_bounds() const { return rank >= 0 && file >= 0 && rank < 8 && file < 8; }
|
||||
bool is_light() const { return (rank % 2) != (file % 2); }
|
||||
String to_algebraic() const;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue