Skip to content

Commit

Permalink
refactor api
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokartikey committed Aug 25, 2023
1 parent ecfd4f5 commit fef0ef1
Show file tree
Hide file tree
Showing 18 changed files with 119 additions and 123 deletions.
2 changes: 1 addition & 1 deletion src/chess++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ add_library(
constants.hxx constants.cxx
core.hxx core.cxx
move.hxx move.cxx
piece_list.hxx piece_list.cxx
piece.hxx piece.cxx
ply.hxx ply.cxx
square.hxx square.cxx
types.hxx types.cxx
)
Expand Down
8 changes: 8 additions & 0 deletions src/chess++/board.cxx
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
#include "board.hxx"

namespace chess {
Board::Board() {
for (auto* sq : _board) {
sq = nullptr;
}
}
} // namespace chess
8 changes: 7 additions & 1 deletion src/chess++/board.hxx
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#ifndef CHESSPP_BOARD_HXX
#define CHESSPP_BOARD_HXX

#include <array>

#include "types.hxx"

namespace chess {
class Board {
types::board _board;
std::array<Square*, constants::num_squares> _board;
std::vector<std::reference_wrapper<Piece>> _piece_list;

public:
Board();
};
} // namespace chess

Expand Down
32 changes: 10 additions & 22 deletions src/chess++/constants.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,16 @@
* piece::rook
* piece::queen
* piece::king
*
*
* color::white
* color::black
*
*/

namespace chess::constants {
constexpr int num_squares = 64;
}

namespace chess::square {
// clang-format off
enum square {
enum square { // clang-format off
A1, B1, C1, D1, E1, F1, G1, H1,
A2, B2, C2, D2, E2, F2, G2, H2,
A3, B3, C3, D3, E3, F3, G3, H3,
Expand All @@ -41,34 +38,25 @@ enum square {
A6, B6, C6, D6, E6, F6, G6, H6,
A7, B7, C7, D7, E7, F7, G7, H7,
A8, B8, C8, D8, E8, F8, G8, H8
};
// clang-format on
} // namespace chess::square
}; // clang-format on

namespace chess::file {
enum file { A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7 };
}

namespace chess::rank {
enum rank { R1 = 0, R2 = 1, R3 = 2, R4 = 3, R5 = 4, R6 = 5, R7 = 6, R8 = 7 };
}

namespace chess::piece {
enum piece { pawn, knight, bishop, rook, queen, king };
}

namespace chess::color {
enum color { white, black };
} // namespace chess::color
enum color { white = 1, black = 0 };
} // namespace chess::constants

namespace move {
namespace chess::move {
enum move { regular, promotion, en_passant, king_castle, queen_castle };

enum legal { illegal, legal };
enum legal { illegal = 0, legal = 1 };

enum capture { no_capture, is_capture };
enum capture { no_capture = 0, is_capture = 1 };

enum check { no_check, is_check };
} // namespace move
enum check { no_check = 0, is_check = 1 };
} // namespace chess::move

#endif
2 changes: 1 addition & 1 deletion src/chess++/core.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "constants.hxx"
#include "move.hxx"
#include "piece.hxx"
#include "piece_list.hxx"
#include "ply.hxx"
#include "square.hxx"
#include "types.hxx"

Expand Down
21 changes: 21 additions & 0 deletions src/chess++/move.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "move.hxx"

#include <utility>

#include "constants.hxx"

namespace chess {
Move::Move() {
_ply[constants::color::black] = nullptr;
_ply[constants::color::white] = nullptr;
}

Move::Move(Ply& ply_black, Ply& ply_white) {
ply(constants::color::black, ply_black);
ply(constants::color::white, ply_white);
}

void Move::ply(constants::color color, Ply& ply) { _ply[color] = &ply; }

Ply& Move::ply(constants::color color) { return *_ply[color]; }
} // namespace chess
15 changes: 12 additions & 3 deletions src/chess++/move.hxx
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
#ifndef CHESSPP_MOVE_HXX
#define CHESSPP_MOVE_HXX

#include <array>

#include "ply.hxx"

namespace chess {
class Move {
piece::piece _type;
square::square _from;
square::square _to;
std::array<Ply*, 2> _ply;

public:
Move();
Move(Ply& ply_black, Ply& ply_white);

void ply(constants::color color, Ply& ply);
Ply& ply(constants::color color);
};
} // namespace chess

Expand Down
6 changes: 3 additions & 3 deletions src/chess++/piece.cxx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "piece.hxx"

namespace chess {
Piece::Piece(piece::piece type, color::color color) {
Piece::Piece(constants::piece type, constants::color color) {
_type = type;
_color = color;
}

piece::piece Piece::get_type() { return _type; }
constants::piece Piece::type() { return _type; }

color::color Piece::get_color() { return _color; }
constants::color Piece::color() { return _color; }
} // namespace chess
10 changes: 5 additions & 5 deletions src/chess++/piece.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

namespace chess {
class Piece {
piece::piece _type;
color::color _color;
constants::piece _type;
constants::color _color;

public:
Piece(piece::piece type, color::color color);
Piece(constants::piece type, constants::color color);

piece::piece get_type();
color::color get_color();
constants::piece type();
constants::color color();
};
} // namespace chess

Expand Down
17 changes: 0 additions & 17 deletions src/chess++/piece_list.cxx

This file was deleted.

25 changes: 0 additions & 25 deletions src/chess++/piece_list.hxx

This file was deleted.

1 change: 1 addition & 0 deletions src/chess++/ply.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "ply.hxx"
15 changes: 15 additions & 0 deletions src/chess++/ply.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef CHESSPP_PLY_HXX
#define CHESSPP_PLY_HXX

#include "constants.hxx"

namespace chess {
class Ply {
constants::color _color;
constants::piece _piece_type;
constants::square _from;
constants::square _to;
};
} // namespace chess

#endif
16 changes: 8 additions & 8 deletions src/chess++/square.cxx
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#include "square.hxx"

namespace chess {
Square::Square(square::square square) {
Square::Square(constants::square square) {
_name = square;
_piece = nullptr;
}

Square::Square(square::square square, Piece &piece) {
Square::Square(constants::square square, Piece& piece) {
_name = square;
set_piece(piece);
this->piece(piece);
}

square::square Square::name() { return _name; }
constants::square Square::name() { return _name; }

file::file Square::file() { return file::file(_name % 8); }
constants::file Square::file() { return constants::file(_name % 8); }

rank::rank Square::rank() { return rank::rank(_name / 8); }
constants::rank Square::rank() { return constants::rank(_name / 8); }

Piece &Square::get_piece() { return *_piece; }
Piece& Square::piece() { return *_piece; }

void Square::set_piece(Piece &piece) { _piece = &piece; }
void Square::piece(Piece& piece) { _piece = &piece; }
} // namespace chess
18 changes: 9 additions & 9 deletions src/chess++/square.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@

namespace chess {
class Square {
square::square _name;
Piece *_piece;
constants::square _name;
Piece* _piece;

public:
Square(square::square square);
Square(square::square square, Piece &piece);
Square(constants::square square);
Square(constants::square square, Piece& piece);

square::square name();
file::file file();
rank::rank rank();
constants::square name();
constants::file file();
constants::rank rank();

Piece &get_piece();
void set_piece(Piece &piece);
Piece& piece();
void piece(Piece& piece);
};
} // namespace chess

Expand Down
2 changes: 1 addition & 1 deletion src/chess++/types.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

#include "constants.hxx"
#include "move.hxx"
#include "piece.hxx"
#include "square.hxx"

namespace chess::types {
using move_list = std::vector<Move>;
using board = std::array<Square, constants::num_squares>;
} // namespace chess::types

#endif
12 changes: 1 addition & 11 deletions src/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,4 @@

#include "chess++/core.hxx"

int main() {
chess::Piece piece(chess::piece::pawn, chess::color::white);

chess::types::PieceList list;
list.push_back(piece);

chess::Square square(chess::square::E4);
square.set_piece(list[0]);

return 0;
}
int main() { return 0; }
32 changes: 16 additions & 16 deletions tests/constants.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
using namespace chess;

TEST(ConstantsTest, FileTest) {
EXPECT_EQ(file::A, 0);
EXPECT_EQ(file::B, 1);
EXPECT_EQ(file::C, 2);
EXPECT_EQ(file::D, 3);
EXPECT_EQ(file::E, 4);
EXPECT_EQ(file::F, 5);
EXPECT_EQ(file::G, 6);
EXPECT_EQ(file::H, 7);
EXPECT_EQ(constants::file::A, 0);
EXPECT_EQ(constants::file::B, 1);
EXPECT_EQ(constants::file::C, 2);
EXPECT_EQ(constants::file::D, 3);
EXPECT_EQ(constants::file::E, 4);
EXPECT_EQ(constants::file::F, 5);
EXPECT_EQ(constants::file::G, 6);
EXPECT_EQ(constants::file::H, 7);
}

TEST(ConstantsTest, RankTest) {
EXPECT_EQ(rank::R1, 0);
EXPECT_EQ(rank::R2, 1);
EXPECT_EQ(rank::R3, 2);
EXPECT_EQ(rank::R4, 3);
EXPECT_EQ(rank::R5, 4);
EXPECT_EQ(rank::R6, 5);
EXPECT_EQ(rank::R7, 6);
EXPECT_EQ(rank::R8, 7);
EXPECT_EQ(constants::rank::R1, 0);
EXPECT_EQ(constants::rank::R2, 1);
EXPECT_EQ(constants::rank::R3, 2);
EXPECT_EQ(constants::rank::R4, 3);
EXPECT_EQ(constants::rank::R5, 4);
EXPECT_EQ(constants::rank::R6, 5);
EXPECT_EQ(constants::rank::R7, 6);
EXPECT_EQ(constants::rank::R8, 7);
}

0 comments on commit fef0ef1

Please sign in to comment.