mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:27:35 +00:00
Chess: Add serialization of moves and squares
This commit is contained in:
parent
f69b419c05
commit
ffece9cfba
2 changed files with 43 additions and 0 deletions
|
@ -27,9 +27,30 @@
|
|||
#include "Chess.h"
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/LogStream.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
String Chess::char_for_piece(Chess::Type type)
|
||||
{
|
||||
switch (type) {
|
||||
case Type::Knight:
|
||||
return "N";
|
||||
case Type::Bishop:
|
||||
return "B";
|
||||
case Type::Rook:
|
||||
return "R";
|
||||
case Type::Queen:
|
||||
return "Q";
|
||||
case Type::King:
|
||||
return "K";
|
||||
case Type::Pawn:
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
Chess::Square::Square(const StringView& name)
|
||||
{
|
||||
ASSERT(name.length() == 2);
|
||||
|
@ -51,6 +72,23 @@ Chess::Square::Square(const StringView& name)
|
|||
}
|
||||
}
|
||||
|
||||
String Chess::Square::to_algebraic() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
builder.append(file - 'a');
|
||||
builder.append(rank - '1');
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
String Chess::Move::to_long_algebraic() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
builder.append(from.to_algebraic());
|
||||
builder.append(to.to_algebraic());
|
||||
builder.append(char_for_piece(promote_to).to_lowercase());
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
Chess::Chess()
|
||||
{
|
||||
// Fill empty spaces.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue