Skip to content

Commit

Permalink
rename Ply to Move (remove old Move), Board fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokartikey committed Sep 1, 2023
1 parent 9c35073 commit 16b7335
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 163 deletions.
1 change: 0 additions & 1 deletion src/chess++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ add_library(
constants.h constants.cc
move.h move.cc
piece.h piece.cc
ply.h ply.cc
square.h square.cc
)

Expand Down
1 change: 0 additions & 1 deletion src/chess++/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "constants.h"
#include "move.h"
#include "piece.h"
#include "ply.h"
#include "square.h"

#endif
57 changes: 49 additions & 8 deletions src/chess++/fmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,64 @@ std::string format_as(piece::color color) {
} // namespace chess::piece

namespace chess {
std::string format_as(Move move) {
return fmt::format("{} {}", move.ply(piece::black), move.ply(piece::white));
std::string format_as(Board board) {
std::string files = "ABCDEFGH";
std::string head = " ";
for (int i = 0; i < 8; i++) {
head += files[i];
head += " ";
}
head += "\n";

std::string hr = "+";
for (int i = 0; i < 8; i++) {
hr += "---+";
}

std::string f = head + hr + "\n";

for (int i = 0; i < 8; i++) {
f += "|";
for (int j = 0; j < 8; j++) {
square::name sq = square::name(i * 8 + j);
if (board.square(sq).has_piece()) {
f += fmt::format(" {} |", board.square(sq).piece());
} else {
f += " |";
}
}
f += fmt::format(" {}\n", board.square(square::name(i * 8)).rank());
f += hr + "\n";
}

return f;
}

std::string format_as(Piece piece) {
std::array<std::string, 2> rep = {"PNBRQK", "pnbrqk"};
return fmt::format("{}", rep[piece.color()][piece.type()]);
}

std::string format_as(Ply ply) {
std::string format_as(Move move) {
std::array<std::string, 2> capture = {"", "x"};
return fmt::format("{}{}{}{}", ply.piece(), ply.from(),
capture[ply.capture()], ply.to());
return fmt::format("{}{}{}{}", move.piece(), move.from(),
capture[move.capture()], move.to());
}

std::string format_as(Square sq) {
return fmt::format(" {}\n+---+\n| {} | {}\n+---+", sq.file(), sq.rank(),
sq.piece());
std::string format_as(Square square) {
std::string f = fmt::format(" {}\n", square.file());
f += "+---+\n";
if (square.has_piece()) {
f += fmt::format("| {} | {}\n", square.piece(), square.rank());
} else {
f += fmt::format("| | {}\n", square.rank());
}
f += "+---+";

return f;
}
} // namespace chess

namespace chess::types {
std::string format_as(nullpiece np) { return " "; }
} // namespace chess::types
8 changes: 6 additions & 2 deletions src/chess++/fmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#include <array>
#include <string>

#include "board.h"
#include "constants.h"
#include "move.h"
#include "piece.h"
#include "ply.h"
#include "square.h"

namespace chess::square {
Expand All @@ -22,10 +22,14 @@ std::string format_as(piece::color color);
} // namespace chess::piece

namespace chess {
std::string format_as(Board board);
std::string format_as(Move move);
std::string format_as(Piece piece);
std::string format_as(Ply ply);
std::string format_as(Square square);
} // namespace chess

namespace chess::types {
std::string format_as(nullpiece np);
}

#endif
35 changes: 28 additions & 7 deletions src/chess++/move.cc
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
#include "move.h"

namespace chess {
Move::Move() {}

Move::Move(Ply& white, Ply& black) {
ply_[piece::white] = white;
ply_[piece::black] = black;
Move::Move(Piece& piece, move::type type, square::name from, square::name to)
: piece_(piece) {
type_ = type;
from_ = from;
to_ = to;
}

void Move::ply(piece::color color, Ply& ply) { ply_[color] = ply; }
const move::type Move::type() const { return type_; }

const piece::color Move::color() const { return piece_.color(); }

const piece::type Move::piece_type() const { return piece_.type(); }

const Piece& Move::piece() const { return piece_; }

const square::name Move::from() const { return from_; }

void Move::from(square::name square) { from_ = square; }

const Ply& Move::ply(piece::color color) const { return ply_[color].value(); }
const square::name Move::to() const { return to_; }

void Move::to(square::name square) { to_ = square; }

const bool Move::promotion() const { return (type_ & 0b1000) >> 3; }

const bool Move::capture() const { return (type_ & 0b0100) >> 2; }

Move& Move::operator=(Move& move) {
*this = move;
return *this;
}
} // namespace chess
75 changes: 60 additions & 15 deletions src/chess++/move.h
Original file line number Diff line number Diff line change
@@ -1,36 +1,81 @@
#ifndef CHESSPP_MOVE_H
#define CHESSPP_MOVE_H

#include <array>
#include <optional>
#include <string_view>

#include "constants.h"
#include "ply.h"
#include "piece.h"

/**API Example
* Move move;
* Move move(ply_w, ply_b);
*
* Get particular ply
* move.ply(piece::white);
*
* Set particular ply
* move.ply(piece::black, ply);
* Move move(e_pawn, move::double_pawn_push, square::E2, square::E4);
*
* // Get move type
* move.type();
*
* // Get player color
* move.color();
*
* // Get piece type
* move.piece_type();
*
* // Get piece object (const ref)
* move.piece()
*
* // Starting square
* move.from();
* move.from(E2);
*
* // Ending square
* move.to();
* move.to(E4);
*
* // Check if promotion
* move.promotion();
*
* // Check if capture
* move.capture();
*/

namespace chess {
class Move {
public:
Move();
Move(Ply& white, Ply& black);
Move(Piece& piece, move::type type, square::name from, square::name to);

const move::type type() const;

void ply(piece::color color, Ply& ply);
const Ply& ply(piece::color color) const;
const piece::color color() const;

const piece::type piece_type() const;

const Piece& piece() const;

const square::name from() const;
void from(square::name square);

const square::name to() const;
void to(square::name square);

const bool promotion() const;
const bool capture() const;

Move& operator=(Move& move);

private:
std::array<types::PlyOptional, 2> ply_;
const Piece& piece_;
move::type type_;
square::name from_;
square::name to_;
};
} // namespace chess

namespace chess::types {
using MoveOptional = std::optional<Move>;
using nullmove = std::nullopt_t;
} // namespace chess::types

namespace chess {
constexpr types::MoveOptional nullmove = std::nullopt;
}

#endif
8 changes: 4 additions & 4 deletions src/chess++/piece.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
*
* // Get piece type
* piece.type();
*
*
* // Set piece type
* piece.type(piece::knight);
*
*
* // Get piece color
* piece.color();
*
*
* // Set piece color
* piece.color(piece::white);
*
*
* piece.color();
*/

Expand Down
35 changes: 0 additions & 35 deletions src/chess++/ply.cc

This file was deleted.

81 changes: 0 additions & 81 deletions src/chess++/ply.h

This file was deleted.

Loading

0 comments on commit 16b7335

Please sign in to comment.