summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean-Baptiste Boric <jblbeurope@gmail.com>2021-05-16 14:55:20 +0200
committerLinus Groh <mail@linusgroh.de>2021-05-17 18:14:05 +0100
commit0262a99a1f0b0c07de4a5742d05338133b18c3ef (patch)
tree902f78f9fd5bfb1b3e5c8c107fdd7919f885b3c5
parentd0eb376520db8fc2f95547f272fe4727f247d0fa (diff)
downloadserenity-0262a99a1f0b0c07de4a5742d05338133b18c3ef.zip
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 :^)
-rw-r--r--Userland/Games/Chess/ChessWidget.cpp10
-rw-r--r--Userland/Libraries/LibChess/Chess.cpp34
-rw-r--r--Userland/Libraries/LibChess/Chess.h8
3 files changed, 25 insertions, 27 deletions
diff --git a/Userland/Games/Chess/ChessWidget.cpp b/Userland/Games/Chess/ChessWidget.cpp
index eb17457682..92921bb6be 100644
--- a/Userland/Games/Chess/ChessWidget.cpp
+++ b/Userland/Games/Chess/ChessWidget.cpp
@@ -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) };
}
}
diff --git a/Userland/Libraries/LibChess/Chess.cpp b/Userland/Libraries/LibChess/Chess.cpp
index 071f1f95cf..5b21d2dc29 100644
--- a/Userland/Libraries/LibChess/Chess.cpp
+++ b/Userland/Libraries/LibChess/Chess.cpp
@@ -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;
diff --git a/Userland/Libraries/LibChess/Chess.h b/Userland/Libraries/LibChess/Chess.h
index 7ffccf36a8..ac227ea287 100644
--- a/Userland/Libraries/LibChess/Chess.h
+++ b/Userland/Libraries/LibChess/Chess.h
@@ -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;
};