2020-08-10 12:54:32 -06:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-08-12 14:41:35 -06:00
|
|
|
#include <AK/HashMap.h>
|
2020-08-11 14:09:20 -06:00
|
|
|
#include <AK/IterationDecision.h>
|
2020-08-11 21:13:48 -06:00
|
|
|
#include <AK/Optional.h>
|
2020-08-10 12:54:32 -06:00
|
|
|
#include <AK/StringView.h>
|
|
|
|
#include <AK/Traits.h>
|
2020-08-19 17:53:50 -06:00
|
|
|
#include <AK/Vector.h>
|
2020-08-10 12:54:32 -06:00
|
|
|
|
2020-08-18 15:02:53 -06:00
|
|
|
namespace Chess {
|
|
|
|
|
|
|
|
enum class Type {
|
|
|
|
Pawn,
|
|
|
|
Knight,
|
|
|
|
Bishop,
|
|
|
|
Rook,
|
|
|
|
Queen,
|
|
|
|
King,
|
|
|
|
None,
|
|
|
|
};
|
2020-08-10 12:54:32 -06:00
|
|
|
|
2020-08-18 15:02:53 -06:00
|
|
|
String char_for_piece(Type type);
|
2020-08-19 14:59:31 -06:00
|
|
|
Chess::Type piece_for_char_promotion(const StringView& str);
|
2020-08-10 12:54:32 -06:00
|
|
|
|
2020-08-18 15:02:53 -06:00
|
|
|
enum class Colour {
|
|
|
|
White,
|
|
|
|
Black,
|
|
|
|
None,
|
|
|
|
};
|
2020-08-18 14:29:27 -06:00
|
|
|
|
2020-08-18 15:02:53 -06:00
|
|
|
Colour opposing_colour(Colour colour);
|
|
|
|
|
|
|
|
struct Piece {
|
2020-08-21 13:46:07 +02:00
|
|
|
constexpr Piece()
|
|
|
|
: colour(Colour::None)
|
|
|
|
, type(Type::None)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
constexpr Piece(Colour c, Type t)
|
|
|
|
: colour(c)
|
|
|
|
, type(t)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
Colour colour : 4;
|
|
|
|
Type type : 4;
|
2020-08-18 15:02:53 -06:00
|
|
|
bool operator==(const Piece& other) const { return colour == other.colour && type == other.type; }
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr Piece EmptyPiece = { Colour::None, Type::None };
|
|
|
|
|
|
|
|
struct Square {
|
|
|
|
unsigned rank; // zero indexed;
|
|
|
|
unsigned file;
|
|
|
|
Square(const StringView& name);
|
|
|
|
Square(const unsigned& rank, const unsigned& file)
|
|
|
|
: rank(rank)
|
|
|
|
, file(file)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
bool operator==(const Square& other) const { return rank == other.rank && file == other.file; }
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
static void for_each(Callback callback)
|
|
|
|
{
|
|
|
|
for (int rank = 0; rank < 8; ++rank) {
|
|
|
|
for (int file = 0; file < 8; ++file) {
|
|
|
|
if (callback(Square(rank, file)) == IterationDecision::Break)
|
|
|
|
return;
|
2020-08-11 14:09:20 -06:00
|
|
|
}
|
|
|
|
}
|
2020-08-18 15:02:53 -06:00
|
|
|
}
|
2020-08-11 14:09:20 -06:00
|
|
|
|
2020-08-18 15:02:53 -06:00
|
|
|
bool in_bounds() const { return rank < 8 && file < 8; }
|
|
|
|
bool is_light() const { return (rank % 2) != (file % 2); }
|
|
|
|
String to_algebraic() const;
|
|
|
|
};
|
2020-08-10 12:54:32 -06:00
|
|
|
|
2020-12-10 17:34:06 +01:00
|
|
|
class Board;
|
|
|
|
|
2020-08-18 15:02:53 -06:00
|
|
|
struct Move {
|
|
|
|
Square from;
|
|
|
|
Square to;
|
|
|
|
Type promote_to;
|
2020-12-04 14:26:26 +01:00
|
|
|
Piece piece;
|
|
|
|
bool is_check = false;
|
|
|
|
bool is_mate = false;
|
|
|
|
bool is_capture = false;
|
|
|
|
bool is_ambiguous = false;
|
|
|
|
Square ambiguous { 50, 50 };
|
2020-12-10 17:34:06 +01:00
|
|
|
Move(const StringView& long_algebraic);
|
2020-08-18 15:02:53 -06:00
|
|
|
Move(const Square& from, const Square& to, const Type& promote_to = Type::None)
|
|
|
|
: from(from)
|
|
|
|
, to(to)
|
|
|
|
, promote_to(promote_to)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
bool operator==(const Move& other) const { return from == other.from && to == other.to && promote_to == other.promote_to; }
|
2020-08-18 14:29:27 -06:00
|
|
|
|
2020-12-10 17:34:06 +01:00
|
|
|
static Move from_algebraic(const StringView& algebraic, const Colour turn, const Board& board);
|
2020-08-18 15:02:53 -06:00
|
|
|
String to_long_algebraic() const;
|
2020-12-04 14:26:26 +01:00
|
|
|
String to_algebraic() const;
|
2020-08-18 15:02:53 -06:00
|
|
|
};
|
2020-08-10 12:54:32 -06:00
|
|
|
|
2020-08-18 15:02:53 -06:00
|
|
|
class Board {
|
|
|
|
public:
|
|
|
|
Board();
|
2020-08-10 12:54:32 -06:00
|
|
|
|
|
|
|
Piece get_piece(const Square&) const;
|
|
|
|
Piece set_piece(const Square&, const Piece&);
|
|
|
|
|
2020-08-11 14:09:20 -06:00
|
|
|
bool is_legal(const Move&, Colour colour = Colour::None) const;
|
|
|
|
bool in_check(Colour colour) const;
|
2020-08-10 12:54:32 -06:00
|
|
|
|
2020-08-11 22:53:11 -06:00
|
|
|
bool is_promotion_move(const Move&, Colour colour = Colour::None) const;
|
|
|
|
|
2020-08-11 14:09:20 -06:00
|
|
|
bool apply_move(const Move&, Colour colour = Colour::None);
|
2020-08-11 21:13:48 -06:00
|
|
|
const Optional<Move>& last_move() const { return m_last_move; }
|
2020-08-10 12:54:32 -06:00
|
|
|
|
2020-12-10 16:44:38 +01:00
|
|
|
String to_fen() const;
|
|
|
|
|
2020-08-11 14:10:39 -06:00
|
|
|
enum class Result {
|
|
|
|
CheckMate,
|
|
|
|
StaleMate,
|
2020-12-04 13:17:00 +01:00
|
|
|
WhiteResign,
|
|
|
|
BlackResign,
|
2020-08-11 14:10:39 -06:00
|
|
|
FiftyMoveRule,
|
2020-08-12 14:41:35 -06:00
|
|
|
SeventyFiveMoveRule,
|
2020-10-02 22:14:37 +01:00
|
|
|
ThreeFoldRepetition,
|
|
|
|
FiveFoldRepetition,
|
2020-08-12 14:41:35 -06:00
|
|
|
InsufficientMaterial,
|
2020-08-11 14:10:39 -06:00
|
|
|
NotFinished,
|
|
|
|
};
|
|
|
|
|
2020-12-04 14:26:26 +01:00
|
|
|
static String result_to_string(Result, Colour turn);
|
|
|
|
static String result_to_points(Result, Colour turn);
|
2020-12-04 13:17:00 +01:00
|
|
|
|
2020-08-11 14:10:39 -06:00
|
|
|
template<typename Callback>
|
|
|
|
void generate_moves(Callback callback, Colour colour = Colour::None) const;
|
2020-08-20 17:04:08 -06:00
|
|
|
Move random_move(Colour colour = Colour::None) const;
|
2020-08-11 14:10:39 -06:00
|
|
|
Result game_result() const;
|
2020-08-20 17:04:08 -06:00
|
|
|
Colour game_winner() const;
|
|
|
|
int game_score() const;
|
|
|
|
bool game_finished() const;
|
2020-12-04 13:17:00 +01:00
|
|
|
void set_resigned(Colour);
|
2020-08-20 17:04:08 -06:00
|
|
|
int material_imbalance() const;
|
2020-08-10 12:54:32 -06:00
|
|
|
|
2020-08-19 17:53:50 -06:00
|
|
|
Colour turn() const { return m_turn; }
|
|
|
|
const Vector<Move>& moves() const { return m_moves; }
|
2020-08-11 14:10:39 -06:00
|
|
|
|
2020-08-18 15:02:53 -06:00
|
|
|
bool operator==(const Board& other) const;
|
2020-08-12 14:41:35 -06:00
|
|
|
|
2020-08-10 12:54:32 -06:00
|
|
|
private:
|
2020-08-11 14:09:20 -06:00
|
|
|
bool is_legal_no_check(const Move&, Colour colour) const;
|
2020-10-30 06:39:44 +00:00
|
|
|
bool is_legal_promotion(const Move&, Colour colour) const;
|
2020-08-11 14:09:20 -06:00
|
|
|
bool apply_illegal_move(const Move&, Colour colour);
|
|
|
|
|
2020-08-10 12:54:32 -06:00
|
|
|
Piece m_board[8][8];
|
|
|
|
Colour m_turn { Colour::White };
|
2020-12-04 13:17:00 +01:00
|
|
|
Colour m_resigned { Colour::None };
|
2020-08-11 21:13:48 -06:00
|
|
|
Optional<Move> m_last_move;
|
2020-08-12 14:41:35 -06:00
|
|
|
int m_moves_since_capture { 0 };
|
2020-12-10 16:44:38 +01:00
|
|
|
int m_moves_since_pawn_advance { 0 };
|
2020-08-11 14:09:20 -06:00
|
|
|
|
|
|
|
bool m_white_can_castle_kingside { true };
|
|
|
|
bool m_white_can_castle_queenside { true };
|
|
|
|
bool m_black_can_castle_kingside { true };
|
|
|
|
bool m_black_can_castle_queenside { true };
|
2020-08-12 14:41:35 -06:00
|
|
|
|
2020-08-18 15:02:53 -06:00
|
|
|
HashMap<Board, int> m_previous_states;
|
2020-08-19 17:53:50 -06:00
|
|
|
Vector<Move> m_moves;
|
2020-08-18 15:02:53 -06:00
|
|
|
friend struct AK::Traits<Board>;
|
2020-08-12 14:41:35 -06:00
|
|
|
};
|
|
|
|
|
2020-08-11 14:10:39 -06:00
|
|
|
template<typename Callback>
|
2020-08-18 15:02:53 -06:00
|
|
|
void Board::generate_moves(Callback callback, Colour colour) const
|
2020-08-11 14:10:39 -06:00
|
|
|
{
|
|
|
|
if (colour == Colour::None)
|
|
|
|
colour = turn();
|
|
|
|
|
|
|
|
auto try_move = [&](Move m) {
|
|
|
|
if (is_legal(m, colour)) {
|
|
|
|
if (callback(m) == IterationDecision::Break)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
Square::for_each([&](Square sq) {
|
|
|
|
auto piece = get_piece(sq);
|
|
|
|
if (piece.colour != colour)
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
|
|
|
bool keep_going = true;
|
|
|
|
if (piece.type == Type::Pawn) {
|
2020-08-18 15:02:53 -06:00
|
|
|
for (auto& piece : Vector({ Type::None, Type::Knight, Type::Bishop, Type::Rook, Type::Queen })) {
|
|
|
|
keep_going = try_move({ sq, { sq.rank + 1, sq.file }, piece })
|
|
|
|
&& try_move({ sq, { sq.rank + 2, sq.file }, piece })
|
|
|
|
&& try_move({ sq, { sq.rank - 1, sq.file }, piece })
|
|
|
|
&& try_move({ sq, { sq.rank - 2, sq.file }, piece })
|
|
|
|
&& try_move({ sq, { sq.rank + 1, sq.file + 1 }, piece })
|
|
|
|
&& try_move({ sq, { sq.rank + 1, sq.file - 1 }, piece })
|
|
|
|
&& try_move({ sq, { sq.rank - 1, sq.file + 1 }, piece })
|
|
|
|
&& try_move({ sq, { sq.rank - 1, sq.file - 1 }, piece });
|
2020-08-17 15:40:55 -06:00
|
|
|
}
|
2020-08-11 14:10:39 -06:00
|
|
|
} else if (piece.type == Type::Knight) {
|
2020-08-18 15:02:53 -06:00
|
|
|
keep_going = try_move({ sq, { sq.rank + 2, sq.file + 1 } })
|
|
|
|
&& try_move({ sq, { sq.rank + 2, sq.file - 1 } })
|
|
|
|
&& try_move({ sq, { sq.rank + 1, sq.file + 2 } })
|
|
|
|
&& try_move({ sq, { sq.rank + 1, sq.file - 2 } })
|
|
|
|
&& try_move({ sq, { sq.rank - 2, sq.file + 1 } })
|
|
|
|
&& try_move({ sq, { sq.rank - 2, sq.file - 1 } })
|
|
|
|
&& try_move({ sq, { sq.rank - 1, sq.file + 2 } })
|
|
|
|
&& try_move({ sq, { sq.rank - 1, sq.file - 2 } });
|
2020-08-11 14:10:39 -06:00
|
|
|
} else if (piece.type == Type::Bishop) {
|
|
|
|
for (int dr = -1; dr <= 1; dr += 2) {
|
|
|
|
for (int df = -1; df <= 1; df += 2) {
|
|
|
|
for (Square to = sq; to.in_bounds(); to = { to.rank + dr, to.file + df }) {
|
|
|
|
if (!try_move({ sq, to }))
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (piece.type == Type::Rook) {
|
|
|
|
for (int dr = -1; dr <= 1; dr++) {
|
|
|
|
for (int df = -1; df <= 1; df++) {
|
|
|
|
if ((dr == 0) != (df == 0)) {
|
|
|
|
for (Square to = sq; to.in_bounds(); to = { to.rank + dr, to.file + df }) {
|
|
|
|
if (!try_move({ sq, to }))
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (piece.type == Type::Queen) {
|
|
|
|
for (int dr = -1; dr <= 1; dr++) {
|
|
|
|
for (int df = -1; df <= 1; df++) {
|
|
|
|
if (dr != 0 || df != 0) {
|
|
|
|
for (Square to = sq; to.in_bounds(); to = { to.rank + dr, to.file + df }) {
|
|
|
|
if (!try_move({ sq, to }))
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (piece.type == Type::King) {
|
|
|
|
for (int dr = -1; dr <= 1; dr++) {
|
|
|
|
for (int df = -1; df <= 1; df++) {
|
|
|
|
if (!try_move({ sq, { sq.rank + dr, sq.file + df } }))
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Castling moves.
|
|
|
|
if (sq == Square("e1")) {
|
|
|
|
keep_going = try_move({ sq, Square("c1") }) && try_move({ sq, Square("g1") });
|
|
|
|
} else if (sq == Square("e8")) {
|
|
|
|
keep_going = try_move({ sq, Square("c8") }) && try_move({ sq, Square("g8") });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keep_going) {
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
} else {
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-08-18 15:02:53 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct AK::Traits<Chess::Piece> : public GenericTraits<Chess::Piece> {
|
|
|
|
static unsigned hash(Chess::Piece piece)
|
|
|
|
{
|
|
|
|
return pair_int_hash(static_cast<u32>(piece.colour), static_cast<u32>(piece.type));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct AK::Traits<Chess::Board> : public GenericTraits<Chess::Board> {
|
|
|
|
static unsigned hash(Chess::Board chess)
|
|
|
|
{
|
|
|
|
unsigned hash = 0;
|
|
|
|
hash = pair_int_hash(hash, static_cast<u32>(chess.m_white_can_castle_queenside));
|
|
|
|
hash = pair_int_hash(hash, static_cast<u32>(chess.m_white_can_castle_kingside));
|
|
|
|
hash = pair_int_hash(hash, static_cast<u32>(chess.m_black_can_castle_queenside));
|
|
|
|
hash = pair_int_hash(hash, static_cast<u32>(chess.m_black_can_castle_kingside));
|
|
|
|
|
|
|
|
hash = pair_int_hash(hash, static_cast<u32>(chess.m_black_can_castle_kingside));
|
|
|
|
|
|
|
|
Chess::Square::for_each([&](Chess::Square sq) {
|
|
|
|
hash = pair_int_hash(hash, Traits<Chess::Piece>::hash(chess.get_piece(sq)));
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
};
|