From 8320f7ec281891501a9b3092e20d990e7fd14700 Mon Sep 17 00:00:00 2001 From: zedarider Date: Fri, 13 Jan 2017 23:19:00 +0000 Subject: made it more object oriented but removed the board implementation --- src/bishop.cpp | 25 ++++++ src/chess_board.cpp | 244 ---------------------------------------------------- src/chess_piece.cpp | 61 ++----------- src/king.cpp | 25 ++++++ src/knight.cpp | 25 ++++++ src/main.cpp.bck | 23 ----- src/pawn.cpp | 25 ++++++ src/queen.cpp | 25 ++++++ src/rook.cpp | 25 ++++++ src/test_bench.cpp | 45 +++------- 10 files changed, 165 insertions(+), 358 deletions(-) create mode 100644 src/bishop.cpp delete mode 100644 src/chess_board.cpp create mode 100644 src/king.cpp create mode 100644 src/knight.cpp delete mode 100644 src/main.cpp.bck create mode 100644 src/pawn.cpp create mode 100644 src/queen.cpp create mode 100644 src/rook.cpp (limited to 'src') diff --git a/src/bishop.cpp b/src/bishop.cpp new file mode 100644 index 0000000..fc87db2 --- /dev/null +++ b/src/bishop.cpp @@ -0,0 +1,25 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * + * ----------------------------------------------------------------------------- + * + * Bishop class implementation + * + */ + +#include "chess_piece.hpp" + +using namespace ymhChessAI; + + +Bishop::Bishop() : ChessPiece() { +} + +Bishop::Bishop(const int& x, const int& y) : ChessPiece(x, y) { +} + +void Bishop::move(const int& x, const int& y) { +} diff --git a/src/chess_board.cpp b/src/chess_board.cpp deleted file mode 100644 index de21015..0000000 --- a/src/chess_board.cpp +++ /dev/null @@ -1,244 +0,0 @@ -#include "chess_ai.hpp" - -chess_ai::chess_board::chess_board() : SIZE(CHESS_BOARD_SIZE) { - init_board_vector(); -} - -chess_ai::chess_board::chess_board(board_state state) : SIZE(CHESS_BOARD_SIZE) { - unsigned vec_index, sqr_index; - - init_board_vector(); - - if(state == initial) { - - for(vector_iterator it_vec = grid.begin(); it_vec != grid.end(); - ++it_vec) { - - for(square_iterator it_sqr = (*it_vec).begin(); - it_sqr != (*it_vec).end(); ++it_sqr) { - chess_piece piece; - - piece_colour colour; - - if(vec_index < 3) { - colour = black; - } else { - colour = white; - } - - vec_index = it_vec - grid.begin(); - sqr_index = it_sqr - (*it_vec).begin(); - - if(vec_index == 1 || vec_index == 6) { - piece.set_type(pawn); - } else if(vec_index == 0 || vec_index == 7) { - - if(sqr_index == 0 || sqr_index == 7) - piece.set_type(rook); - else if(sqr_index == 1 || sqr_index == 6) - piece.set_type(knight); - else if(sqr_index == 2 || sqr_index == 5) - piece.set_type(bishop); - else if(sqr_index == 3) - piece.set_type(queen); - else if(sqr_index == 4) - piece.set_type(king); - } - - piece.set_colour(colour); - piece.set_x(sqr_index); - piece.set_y(vec_index); - - *it_sqr = piece; - } - } - } else { - for(auto& row : grid) { - for(auto& square : row) { - chess_piece piece; - square = piece; - } - } - } -} - -chess_ai::chess_board::~chess_board() {} - -void chess_ai::chess_board::init_board_vector() { - for(unsigned i = 0; i < SIZE; ++i) { - std::vector tmp_vec; - for(unsigned j = 0; j < SIZE; ++j) { - chess_piece piece; - tmp_vec.push_back(piece); - } - grid.push_back(tmp_vec); - } -} - -void chess_ai::chess_board::print_board() { - std::cout << " |"; - for(unsigned i = 0; i < SIZE; ++i) { - std::cout << "---|"; - } - std::cout << std::endl << "8 |"; - - for(unsigned y = 0; y < SIZE; ++y) { - for(unsigned x = 0; x < SIZE; ++x) { - std::cout << " " << grid[y][x].str() << " |"; - } - - std::cout << std::endl << " |"; - - - if(y == SIZE-1) { - std::cout << "---+"; - for(unsigned i = 0; i < SIZE-2; ++i) { - std::cout << "---+"; - } - std::cout << "---|" << std::endl; - } else { - for(unsigned i = 0; i < SIZE-1; ++i) { - std::cout << "---+"; - } - std::cout << "---|" << std::endl; - std::cout << 7-y << " |"; - } - } - std::cout << " a b c d e f g h" << std::endl; -} - -chess_ai::chess_piece chess_ai::chess_board::at(unsigned x, - unsigned y) { - - square_iterator it_sqr; - chess_piece tmp_piece; - - tmp_piece = *iterate_board(it_sqr, x, y); - return tmp_piece; -} - -void chess_ai::chess_board::set_piece(chess_piece piece) { - square_iterator it_sqr; - *iterate_board(it_sqr, piece.x, piece.y) = piece; -} - -void chess_ai::chess_board::set_piece(piece_type type, piece_colour colour, - unsigned x, unsigned y) { - - chess_piece piece(type, colour, x, y); - set_piece(piece); -} - -void chess_ai::chess_board::remove_piece(chess_piece piece) { - remove_piece(piece.x, piece.y); -} - -void chess_ai::chess_board::remove_piece(unsigned x, unsigned y) { - square_iterator it_sqr; - *iterate_board(it_sqr, x, y) = chess_piece(); -} - -chess_ai::move_error chess_ai::chess_board::move_piece(chess_piece piece) { - return move_piece(piece.x, piece.y); -} - -chess_ai::move_error chess_ai::chess_board::move_piece(unsigned x, unsigned y) { - square_iterator it; - iterate_board(it, x, y); - - if(it->y < 7 && it->y > 0) { - if(it->colour == white) - return move_piece(x, y, x, y-1); - return move_piece(x, y, x, y+1); - } - return move_error_IllegalMove; -} - -chess_ai::move_error chess_ai::chess_board::move_piece -(chess_ai::chess_piece piece, unsigned x, unsigned y) { - - return move_piece(piece.x, piece.y, x, y); -} - -chess_ai::move_error chess_ai::chess_board::move_piece -(unsigned orig_x, unsigned orig_y, unsigned dest_x, unsigned dest_y) { - - chess_piece taken; - return move_piece(orig_x, orig_y, dest_x, dest_y, taken); -} - -chess_ai::move_error chess_ai::chess_board::move_piece -(unsigned orig_x, unsigned orig_y, unsigned dest_x, unsigned dest_y, - chess_piece& taken_piece) { - - square_iterator it; - square_iterator new_it; - - iterate_board(it, orig_x, orig_y); - iterate_board(new_it, dest_x, dest_y); - - switch(it->type) { - case pawn: - return move_pawn(it, new_it, taken_piece); - case rook: - case knight: - case bishop: - case queen: - case king: - default: - return move_error_IllegalMove; - } -} - -chess_ai::square_iterator& chess_ai::chess_board::iterate_board -(square_iterator& it, unsigned x, unsigned y) { - - unsigned vec_index, sqr_index; - for(vector_iterator it_vec = grid.begin(); it_vec != grid.end(); ++it_vec) { - for(square_iterator it_sqr = (*it_vec).begin(); - it_sqr != (*it_vec).end(); ++it_sqr) { - vec_index = it_vec - grid.begin(); - sqr_index = it_sqr - (*it_vec).begin(); - - if(vec_index == y && sqr_index == x) { - it = it_sqr; - return it; - } - } - } - return it; -} - -chess_ai::move_error chess_ai::chess_board::move_pawn -(square_iterator it, square_iterator new_it, chess_piece& taken_piece) { - - chess_piece piece(it->type, it->colour, new_it->x, new_it->y); - - if((new_it->y - it->y == 2 && it->y == 1) || - ((int)new_it->y - (int)it->y == -2 && it->y == 6) || - (new_it->y - it->y == 1 && piece.colour == black && it->y < 7) || - ((int)new_it->y - (int)it->y == -1 && piece.colour == white && - it->y > 0)) { - if(new_it->x == it->x) { - if(new_it->type == empty) { - remove_piece(it->x, it->y); - set_piece(piece); - return move_Success; - } else if(new_it->colour == it->colour) { - return move_error_FriendlyPieceOnDestination; - } - return move_error_EnemyPieceOnDestination; - } else if(new_it->x - it->x == 1 || (int)new_it->x - (int)it->x == -1) { - if(new_it->colour != it->colour && new_it->colour != none) { - taken_piece.set(new_it->type, new_it->colour, new_it->x, - new_it->y); - remove_piece(it->x, it->y); - set_piece(piece); - return move_Success; - } else if(new_it->colour == it->colour) { - return move_error_FriendlyPieceOnDestination; - } - } - } - return move_error_IllegalMove; -} diff --git a/src/chess_piece.cpp b/src/chess_piece.cpp index f73d4c1..8d4cb94 100644 --- a/src/chess_piece.cpp +++ b/src/chess_piece.cpp @@ -1,63 +1,12 @@ -#include "chess_ai.hpp" +#include "chess_piece.hpp" -chess_ai::chess_piece::chess_piece(piece_type type, piece_colour colour) : - type(type), colour(colour) { +using namespace ymhChessAI; - if(colour == black) { - y = 0; - } else { - y = 7; - } - if(type == king) { - x = 4; - } else { - x = 3; - } -} - -chess_ai::chess_piece::chess_piece(piece_type type, piece_colour colour, - unsigned x, unsigned y) : - type(type), colour(colour), x(x), y(y) {} - -chess_ai::chess_piece::~chess_piece() {} - -void chess_ai::chess_piece::set_type(piece_type type) { - this->type = type; -} - -void chess_ai::chess_piece::set_colour(piece_colour colour) { - this->colour = colour; -} - -void chess_ai::chess_piece::set_x(unsigned x) { - this->x = x; -} - -void chess_ai::chess_piece::set_y(unsigned y) { - this->y = y; -} +// Chess Piece Base class that all the pieces inherit from -void chess_ai::chess_piece::set(piece_type type, piece_colour colour, - unsigned x, unsigned y) { - set_type(type); - set_colour(colour); - set_x(x); - set_y(y); +ChessPiece::ChessPiece() : m_x(0), m_y(0) { } -std::string chess_ai::chess_piece::str() { - if(type == empty) - return " "; - else if(type == pawn) - return "p"; - else if(type == rook) - return "r"; - else if(type == knight) - return "n"; - else if(type == bishop) - return "b"; - else if(type == queen) - return "q"; - return "k"; +ChessPiece::ChessPiece(const int& x, const int& y) : m_x(x), m_y(y) { } diff --git a/src/king.cpp b/src/king.cpp new file mode 100644 index 0000000..0f332b7 --- /dev/null +++ b/src/king.cpp @@ -0,0 +1,25 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * + * ----------------------------------------------------------------------------- + * + * King class implementation + * + */ + +#include "chess_piece.hpp" + +using namespace ymhChessAI; + + +King::King() : ChessPiece() { +} + +King::King(const int& x, const int& y) : ChessPiece(x, y) { +} + +void King::move(const int& x, const int& y) { +} diff --git a/src/knight.cpp b/src/knight.cpp new file mode 100644 index 0000000..c40921f --- /dev/null +++ b/src/knight.cpp @@ -0,0 +1,25 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * + * ----------------------------------------------------------------------------- + * + * Knight class implementation + * + */ + +#include "chess_piece.hpp" + +using namespace ymhChessAI; + + +Knight::Knight() : ChessPiece() { +} + +Knight::Knight(const int& x, const int& y) : ChessPiece(x, y) { +} + +void Knight::move(const int& x, const int& y) { +} diff --git a/src/main.cpp.bck b/src/main.cpp.bck deleted file mode 100644 index 2fd9be2..0000000 --- a/src/main.cpp.bck +++ /dev/null @@ -1,23 +0,0 @@ -/* - - description: This is the main file for the chess_ai - - author: Yann Herklotz - date created: DD-MM-YYYY - - */ - -#include "chess_ai.hpp" -#include "chess_tester.hpp" - -#include - -using namespace std; -using namespace chess_ai; - -int main(int argc, char** argv) { - (void)argc; - (void)argv; - - return 0; -} diff --git a/src/pawn.cpp b/src/pawn.cpp new file mode 100644 index 0000000..c1f172f --- /dev/null +++ b/src/pawn.cpp @@ -0,0 +1,25 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * + * ----------------------------------------------------------------------------- + * + * Pawn class implementation + * + */ + +#include "chess_piece.hpp" + +using namespace ymhChessAI; + + +Pawn::Pawn() : ChessPiece() { +} + +Pawn::Pawn(const int& x, const int& y) : ChessPiece(x, y) { +} + +void Pawn::move(const int& x, const int& y) { +} diff --git a/src/queen.cpp b/src/queen.cpp new file mode 100644 index 0000000..d4e7ce0 --- /dev/null +++ b/src/queen.cpp @@ -0,0 +1,25 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * + * ----------------------------------------------------------------------------- + * + * Queen class implementation + * + */ + +#include "chess_piece.hpp" + +using namespace ymhChessAI; + + +Queen::Queen() : ChessPiece() { +} + +Queen::Queen(const int& x, const int& y) : ChessPiece(x, y) { +} + +void Queen::move(const int& x, const int& y) { +} diff --git a/src/rook.cpp b/src/rook.cpp new file mode 100644 index 0000000..166e114 --- /dev/null +++ b/src/rook.cpp @@ -0,0 +1,25 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * + * ----------------------------------------------------------------------------- + * + * Rook class implementation + * + */ + +#include "chess_piece.hpp" + +using namespace ymhChessAI; + + +Rook::Rook() : ChessPiece() { +} + +Rook::Rook(const int& x, const int& y) : ChessPiece(x, y) { +} + +void Rook::move(const int& x, const int& y) { +} diff --git a/src/test_bench.cpp b/src/test_bench.cpp index 68c93fb..861f51b 100644 --- a/src/test_bench.cpp +++ b/src/test_bench.cpp @@ -1,10 +1,10 @@ /* - - description: This is the test_bench file for the chess_ai - - author: Yann Herklotz - date created: DD-MM-YYYY - + * + * description: This is the test_bench file for the chess_ai + * + * author: Yann Herklotz + * date created: DD-MM-YYYY + * */ #include "chess_ai.hpp" @@ -15,34 +15,9 @@ using namespace std; using namespace chess_ai; -int main(int argc, char** argv) { - (void)argc; - (void)argv; - - int test_id; - bool passed; - - chess_tester test_bench; - - chess_board empty_board(clear); - chess_board initial_board(initial); - - test_bench.chess_begin_test_suite(); - - test_id = test_bench.chess_begin_test("PAWN"); - chess_piece piece(pawn, white, 1, 6); - empty_board.set_piece(piece); - empty_board.print_board(); - cout << empty_board.move_piece(1, 6, 1, 4) << endl; - piece.set(pawn, white, 1, 4); - if(empty_board.at(1, 4) == piece) - passed = true; - else - passed = false; - empty_board.print_board(); - test_bench.chess_end_test(test_id, passed); - - test_bench.chess_end_test_suite(); +int main(int argc, char **argv) { + (void)argc; + (void)argv; - return 0; + return 0; } -- cgit From 6fc3dfc10ce0e690b0de9c811e617dc76104db40 Mon Sep 17 00:00:00 2001 From: zedarider Date: Sat, 14 Jan 2017 00:09:29 +0000 Subject: adding a few small comments --- src/test_bench.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/test_bench.cpp b/src/test_bench.cpp index 861f51b..474c53b 100644 --- a/src/test_bench.cpp +++ b/src/test_bench.cpp @@ -1,23 +1,28 @@ /* * - * description: This is the test_bench file for the chess_ai + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com * - * author: Yann Herklotz - * date created: DD-MM-YYYY + * ----------------------------------------------------------------------------- + * + * Main file that tests the chess ai * */ -#include "chess_ai.hpp" #include "chess_tester.hpp" +#include "chess_piece.hpp" #include using namespace std; -using namespace chess_ai; +using namespace ymhChessAI; int main(int argc, char **argv) { - (void)argc; - (void)argv; + cout << "Program name: " << argv[0] << endl; + cout << "Arguments: " << argc - 1 << endl; + + cout << endl << "====== Executed Successfully ======" << endl; return 0; } -- cgit From a5bc94a36fe37fb20eba61516da3b6b58208537b Mon Sep 17 00:00:00 2001 From: ymherklotz Date: Sat, 14 Jan 2017 14:17:26 +0000 Subject: added colour to pieces and comments --- src/bishop.cpp | 2 +- src/chess_piece.cpp | 18 ++++++++++++++---- src/king.cpp | 2 +- src/knight.cpp | 2 +- src/pawn.cpp | 2 +- src/queen.cpp | 2 +- src/rook.cpp | 2 +- 7 files changed, 20 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/bishop.cpp b/src/bishop.cpp index fc87db2..4894be5 100644 --- a/src/bishop.cpp +++ b/src/bishop.cpp @@ -18,7 +18,7 @@ using namespace ymhChessAI; Bishop::Bishop() : ChessPiece() { } -Bishop::Bishop(const int& x, const int& y) : ChessPiece(x, y) { +Bishop::Bishop(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) { } void Bishop::move(const int& x, const int& y) { diff --git a/src/chess_piece.cpp b/src/chess_piece.cpp index 8d4cb94..ee307e2 100644 --- a/src/chess_piece.cpp +++ b/src/chess_piece.cpp @@ -1,12 +1,22 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * + * ----------------------------------------------------------------------------- + * + * Chess Piece class implementation + * + */ + #include "chess_piece.hpp" using namespace ymhChessAI; -// Chess Piece Base class that all the pieces inherit from - -ChessPiece::ChessPiece() : m_x(0), m_y(0) { +ChessPiece::ChessPiece() : m_x(0), m_y(0), m_colour(Colour::White) { } -ChessPiece::ChessPiece(const int& x, const int& y) : m_x(x), m_y(y) { +ChessPiece::ChessPiece(const int& x, const int& y, const Colour& colour) : m_x(x), m_y(y), m_colour(colour) { } diff --git a/src/king.cpp b/src/king.cpp index 0f332b7..8e8a6b9 100644 --- a/src/king.cpp +++ b/src/king.cpp @@ -18,7 +18,7 @@ using namespace ymhChessAI; King::King() : ChessPiece() { } -King::King(const int& x, const int& y) : ChessPiece(x, y) { +King::King(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) { } void King::move(const int& x, const int& y) { diff --git a/src/knight.cpp b/src/knight.cpp index c40921f..5c2a946 100644 --- a/src/knight.cpp +++ b/src/knight.cpp @@ -18,7 +18,7 @@ using namespace ymhChessAI; Knight::Knight() : ChessPiece() { } -Knight::Knight(const int& x, const int& y) : ChessPiece(x, y) { +Knight::Knight(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) { } void Knight::move(const int& x, const int& y) { diff --git a/src/pawn.cpp b/src/pawn.cpp index c1f172f..e81959f 100644 --- a/src/pawn.cpp +++ b/src/pawn.cpp @@ -18,7 +18,7 @@ using namespace ymhChessAI; Pawn::Pawn() : ChessPiece() { } -Pawn::Pawn(const int& x, const int& y) : ChessPiece(x, y) { +Pawn::Pawn(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) { } void Pawn::move(const int& x, const int& y) { diff --git a/src/queen.cpp b/src/queen.cpp index d4e7ce0..d685548 100644 --- a/src/queen.cpp +++ b/src/queen.cpp @@ -18,7 +18,7 @@ using namespace ymhChessAI; Queen::Queen() : ChessPiece() { } -Queen::Queen(const int& x, const int& y) : ChessPiece(x, y) { +Queen::Queen(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) { } void Queen::move(const int& x, const int& y) { diff --git a/src/rook.cpp b/src/rook.cpp index 166e114..cba38a9 100644 --- a/src/rook.cpp +++ b/src/rook.cpp @@ -18,7 +18,7 @@ using namespace ymhChessAI; Rook::Rook() : ChessPiece() { } -Rook::Rook(const int& x, const int& y) : ChessPiece(x, y) { +Rook::Rook(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) { } void Rook::move(const int& x, const int& y) { -- cgit From 1f138d96ab7f517e7842710ef3428f67a2965877 Mon Sep 17 00:00:00 2001 From: ymherklotz Date: Sun, 15 Jan 2017 19:44:15 +0000 Subject: creating the board --- src/bishop.cpp | 1 + src/chess_board.cpp | 34 ++++++++++++++++++++++++++++++++++ src/chess_piece.cpp | 1 + src/king.cpp | 3 +++ src/knight.cpp | 1 + src/pawn.cpp | 1 + src/queen.cpp | 1 + src/rook.cpp | 1 + src/test_bench.cpp | 6 ++++++ 9 files changed, 49 insertions(+) create mode 100644 src/chess_board.cpp (limited to 'src') diff --git a/src/bishop.cpp b/src/bishop.cpp index 4894be5..849db19 100644 --- a/src/bishop.cpp +++ b/src/bishop.cpp @@ -3,6 +3,7 @@ * author: Yann Herklotz * username: ymherklotz * email: ymherklotz@gmail.com + * date created: 13/01/17 * * ----------------------------------------------------------------------------- * diff --git a/src/chess_board.cpp b/src/chess_board.cpp new file mode 100644 index 0000000..30c8aff --- /dev/null +++ b/src/chess_board.cpp @@ -0,0 +1,34 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * date created: 14/01/17 + * + * ----------------------------------------------------------------------------- + * + * Chess Board class implementation + * + */ + +#include "chess_board.hpp" +#include +using namespace ymhChessAI; + + +ChessBoard::ChessBoard() { + board.reserve(CHESS_BOARD_SIZE); + + for(auto&& piece : board) + piece = std::unique_ptr(new King); + + // for(unsigned i = 0; i < CHESS_BOARD_SIZE; ++i) + // board.emplace_back(new King); +} + +void ChessBoard::printBoard() { + for(auto&& piece : board) { + piece->move(1, 2); + std::cout << "Hello" << std::endl; + } +} diff --git a/src/chess_piece.cpp b/src/chess_piece.cpp index ee307e2..626377e 100644 --- a/src/chess_piece.cpp +++ b/src/chess_piece.cpp @@ -3,6 +3,7 @@ * author: Yann Herklotz * username: ymherklotz * email: ymherklotz@gmail.com + * date created: 13/01/17 * * ----------------------------------------------------------------------------- * diff --git a/src/king.cpp b/src/king.cpp index 8e8a6b9..1d297d3 100644 --- a/src/king.cpp +++ b/src/king.cpp @@ -3,6 +3,7 @@ * author: Yann Herklotz * username: ymherklotz * email: ymherklotz@gmail.com + * date created: 13/01/17 * * ----------------------------------------------------------------------------- * @@ -11,6 +12,7 @@ */ #include "chess_piece.hpp" +#include using namespace ymhChessAI; @@ -22,4 +24,5 @@ King::King(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, } void King::move(const int& x, const int& y) { + std::cout << "This is the king" << std::endl; } diff --git a/src/knight.cpp b/src/knight.cpp index 5c2a946..6b7f5ac 100644 --- a/src/knight.cpp +++ b/src/knight.cpp @@ -3,6 +3,7 @@ * author: Yann Herklotz * username: ymherklotz * email: ymherklotz@gmail.com + * date created: 13/01/17 * * ----------------------------------------------------------------------------- * diff --git a/src/pawn.cpp b/src/pawn.cpp index e81959f..1374886 100644 --- a/src/pawn.cpp +++ b/src/pawn.cpp @@ -3,6 +3,7 @@ * author: Yann Herklotz * username: ymherklotz * email: ymherklotz@gmail.com + * date created: 13/01/17 * * ----------------------------------------------------------------------------- * diff --git a/src/queen.cpp b/src/queen.cpp index d685548..897237a 100644 --- a/src/queen.cpp +++ b/src/queen.cpp @@ -3,6 +3,7 @@ * author: Yann Herklotz * username: ymherklotz * email: ymherklotz@gmail.com + * date created: 13/01/17 * * ----------------------------------------------------------------------------- * diff --git a/src/rook.cpp b/src/rook.cpp index cba38a9..cd72029 100644 --- a/src/rook.cpp +++ b/src/rook.cpp @@ -3,6 +3,7 @@ * author: Yann Herklotz * username: ymherklotz * email: ymherklotz@gmail.com + * date created: 13/01/17 * * ----------------------------------------------------------------------------- * diff --git a/src/test_bench.cpp b/src/test_bench.cpp index 474c53b..ab23d6c 100644 --- a/src/test_bench.cpp +++ b/src/test_bench.cpp @@ -3,6 +3,7 @@ * author: Yann Herklotz * username: ymherklotz * email: ymherklotz@gmail.com + * date created: 13/01/17 * * ----------------------------------------------------------------------------- * @@ -12,6 +13,7 @@ #include "chess_tester.hpp" #include "chess_piece.hpp" +#include "chess_board.hpp" #include @@ -22,6 +24,10 @@ int main(int argc, char **argv) { cout << "Program name: " << argv[0] << endl; cout << "Arguments: " << argc - 1 << endl; + ChessBoard cb; + + cb.printBoard(); + cout << endl << "====== Executed Successfully ======" << endl; return 0; -- cgit From 088b948122cbf24dd9dc1dfedf4be2724bab2157 Mon Sep 17 00:00:00 2001 From: ymherklotz Date: Mon, 16 Jan 2017 01:12:25 +0000 Subject: backing up files, with parser changes --- src/board_state_parser.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++ src/chess_board.cpp | 13 ++++------ src/knight.cpp | 2 ++ src/test_bench.cpp | 5 +--- 4 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 src/board_state_parser.cpp (limited to 'src') diff --git a/src/board_state_parser.cpp b/src/board_state_parser.cpp new file mode 100644 index 0000000..fac502f --- /dev/null +++ b/src/board_state_parser.cpp @@ -0,0 +1,63 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * date created: 15/01/17 + * + * ----------------------------------------------------------------------------- + * + * Implementation of the Board state class + * + */ + +#include "board_state_parser.hpp" + +using namespace ymhChessAI; + + +BoardStateParser::BoardStateParser() : boardFile("res/default.board", std::ifstream::in) { +} + +BoardStateParser::BoardStateParser(const std::string& boardFileName) : boardFile(boardFileName.c_str()) { +} + +BoardStateParser::~BoardStateParser() { + boardFile.close(); +} + +void BoardStateParser::populateBoardState() { + std::string currentLine; + unsigned index; + + boardStateVector.reserve(BOARD_SIZE); + + if(!boardFile.is_open()) + throw "Error: Can't open file"; + + while(getline(boardFile, currentLine)) + boardStateVector[stringToInt(currentLine)] = getStateFromLine(currentLine); +} + +unsigned BoardStateParser::stringToInt(const std::string& str) { + unsigned finalInt = 0; + bool foundWhiteSpace = false; + + for(char character : str) + if(character == ' ') + foundWhiteSpace = true; + else if(!foundWhiteSpace && character >= '0' && character <= '9') + finalInt = finalInt * 10 + character - '0'; + + return finalInt; +} + +std::string BoardStateParser::getStateFromLine(const std::string& str) { + std::string state; + + for(unsigned i = 0; i < str.size(); ++i) + if(i > 1) + state += str[i]; + + return state; +} diff --git a/src/chess_board.cpp b/src/chess_board.cpp index 30c8aff..1eaa7d4 100644 --- a/src/chess_board.cpp +++ b/src/chess_board.cpp @@ -13,22 +13,19 @@ #include "chess_board.hpp" #include + using namespace ymhChessAI; ChessBoard::ChessBoard() { - board.reserve(CHESS_BOARD_SIZE); - - for(auto&& piece : board) - piece = std::unique_ptr(new King); - - // for(unsigned i = 0; i < CHESS_BOARD_SIZE; ++i) - // board.emplace_back(new King); } void ChessBoard::printBoard() { + int i = 0; + for(auto&& piece : board) { + std::cout << i << " "; piece->move(1, 2); - std::cout << "Hello" << std::endl; + ++i; } } diff --git a/src/knight.cpp b/src/knight.cpp index 6b7f5ac..25e5ab9 100644 --- a/src/knight.cpp +++ b/src/knight.cpp @@ -12,6 +12,7 @@ */ #include "chess_piece.hpp" +#include using namespace ymhChessAI; @@ -23,4 +24,5 @@ Knight::Knight(const int& x, const int& y, const Colour& colour) : ChessPiece(x, } void Knight::move(const int& x, const int& y) { + std::cout << "This is a Knight" << std::endl; } diff --git a/src/test_bench.cpp b/src/test_bench.cpp index ab23d6c..a68b60c 100644 --- a/src/test_bench.cpp +++ b/src/test_bench.cpp @@ -14,6 +14,7 @@ #include "chess_tester.hpp" #include "chess_piece.hpp" #include "chess_board.hpp" +#include "board_state_parser.hpp" #include @@ -24,10 +25,6 @@ int main(int argc, char **argv) { cout << "Program name: " << argv[0] << endl; cout << "Arguments: " << argc - 1 << endl; - ChessBoard cb; - - cb.printBoard(); - cout << endl << "====== Executed Successfully ======" << endl; return 0; -- cgit From f0799aef3ff8c4a8e8ffea79c963b8f128c462b9 Mon Sep 17 00:00:00 2001 From: zedarider Date: Tue, 17 Jan 2017 15:42:18 +0000 Subject: fixed the chess board and added empty pieces to fill holes, also added forward declarations --- src/chess_board.cpp | 7 +++++++ src/chess_piece.cpp | 9 +++++++++ src/test_bench.cpp | 4 ++++ 3 files changed, 20 insertions(+) (limited to 'src') diff --git a/src/chess_board.cpp b/src/chess_board.cpp index 1eaa7d4..3432b52 100644 --- a/src/chess_board.cpp +++ b/src/chess_board.cpp @@ -18,6 +18,13 @@ using namespace ymhChessAI; ChessBoard::ChessBoard() { + for(unsigned i = 0; i < BOARD_SIZE; ++i) + board.emplace_back(new EmptyPiece); +} + +void ChessBoard::populateBoard() { + for(auto&& piece : board) + piece = chessPiecePtr(new Knight); } void ChessBoard::printBoard() { diff --git a/src/chess_piece.cpp b/src/chess_piece.cpp index 626377e..d4d14e6 100644 --- a/src/chess_piece.cpp +++ b/src/chess_piece.cpp @@ -21,3 +21,12 @@ ChessPiece::ChessPiece() : m_x(0), m_y(0), m_colour(Colour::White) { ChessPiece::ChessPiece(const int& x, const int& y, const Colour& colour) : m_x(x), m_y(y), m_colour(colour) { } + +EmptyPiece::EmptyPiece() : ChessPiece() { +} + +EmptyPiece::EmptyPiece(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) { +} + +void EmptyPiece::move(const int& x, const int& y) { +} diff --git a/src/test_bench.cpp b/src/test_bench.cpp index a68b60c..b1fe4ba 100644 --- a/src/test_bench.cpp +++ b/src/test_bench.cpp @@ -25,6 +25,10 @@ int main(int argc, char **argv) { cout << "Program name: " << argv[0] << endl; cout << "Arguments: " << argc - 1 << endl; + ChessBoard cb; + cb.populateBoard(); + cb.printBoard(); + cout << endl << "====== Executed Successfully ======" << endl; return 0; -- cgit From 9a457557caed9ef614456a9c8139c89084381ed3 Mon Sep 17 00:00:00 2001 From: zedarider Date: Tue, 17 Jan 2017 15:48:32 +0000 Subject: made it more readable --- src/chess_board.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/chess_board.cpp b/src/chess_board.cpp index 3432b52..051decd 100644 --- a/src/chess_board.cpp +++ b/src/chess_board.cpp @@ -22,12 +22,13 @@ ChessBoard::ChessBoard() { board.emplace_back(new EmptyPiece); } -void ChessBoard::populateBoard() { +bool ChessBoard::populateBoard() { for(auto&& piece : board) piece = chessPiecePtr(new Knight); + return true; } -void ChessBoard::printBoard() { +bool ChessBoard::printBoard() { int i = 0; for(auto&& piece : board) { @@ -35,4 +36,6 @@ void ChessBoard::printBoard() { piece->move(1, 2); ++i; } + + return true; } -- cgit From 560cca06326b3d93ba095fde2fbc14bd4c122f00 Mon Sep 17 00:00:00 2001 From: zedarider Date: Tue, 17 Jan 2017 22:49:49 +0000 Subject: fixed the chess tester --- src/chess_tester.cpp | 187 +++++++++++++++++++++++++-------------------------- 1 file changed, 91 insertions(+), 96 deletions(-) (limited to 'src') diff --git a/src/chess_tester.cpp b/src/chess_tester.cpp index 6f3745c..9e20fd3 100644 --- a/src/chess_tester.cpp +++ b/src/chess_tester.cpp @@ -1,125 +1,120 @@ #include "chess_tester.hpp" -chess_tester::chess_tester() : ts_begin(false) { - srand(time(NULL)); +using namespace ymhChessAI; - test_id = rand() % 0xffffffff; - test_id_test = test_id; +chess_tester::chess_tester() : ts_begin(false) { + srand(time(NULL)); + test_id = rand() % 0xffffffff; + test_id_test = test_id; } void chess_tester::chess_begin_test_suite() { - if(ts_begin) { - fprintf(stderr, "Error: already started test suite\n"); - exit(1); - } + if(ts_begin) { + fprintf(stderr, "Error: already started test suite\n"); + exit(1); + } - ts_begin = true; + ts_begin = true; } int chess_tester::chess_begin_test(std::string test_name) { - if(!ts_begin) { - fprintf(stderr, "Error: the test suite hasn't been started yet\n"); - exit(1); - } + if(!ts_begin) { + fprintf(stderr, "Error: the test suite hasn't been started yet\n"); + exit(1); + } - if(!is_in_arr(test_name)) { - fprintf(stderr, "Error: the test name does not exist\n"); - exit(1); - } + if(!is_in_arr(test_name)) { + fprintf(stderr, "Error: the test name does not exist\n"); + exit(1); + } - current_test = test_name; + current_test = test_name; - test_id = rand() % 0xffffffff; - test_id_test = test_id; + test_id = rand() % 0xffffffff; + test_id_test = test_id; - return test_id; + return test_id; } void chess_tester::chess_end_test(int test_id, bool passed) { - if(test_id != test_id_test) { - fprintf(stderr , "Error: the test from before has not been ended\n"); - exit(1); - } - - tested_pieces tmp; - tmp.piece_name = current_test; - - if(passed) { - tmp.num_passed = 1; - tmp.num_failed = 0; - } else { - tmp.num_passed = 0; - tmp.num_failed = 1; - } - - if(test_piece.empty() || !is_in_vec(tmp)) { - test_piece.push_back(tmp); - } else { - for(unsigned i = 0; i < test_piece.size(); ++i) { - if(test_piece[i] == tmp) { - if(passed) { - test_piece[i].num_passed++; - } else { - test_piece[i].num_failed++; - } - } - } - } + if(test_id != test_id_test) { + fprintf(stderr, "Error: the test from before has not been ended\n"); + exit(1); + } + + tested_pieces tmp; + tmp.piece_name = current_test; + + if(passed) { + tmp.num_passed = 1; + tmp.num_failed = 0; + } else { + tmp.num_passed = 0; + tmp.num_failed = 1; + } + + if(test_piece.empty() || !is_in_vec(tmp)) { + test_piece.push_back(tmp); + } else { + for(unsigned i = 0; i < test_piece.size(); ++i) { + if(test_piece[i] == tmp) { + if(passed) + test_piece[i].num_passed++; + else + test_piece[i].num_failed++; + } + } + } } void chess_tester::chess_end_test_suite() { - std::sort(test_piece.begin(), test_piece.end()); - - int pieces_passed = 0; - int pieces_partial = 0; - int pieces_fail = 0; - - fprintf(stderr, "+-%10s-+-%5s-+-%6s-+-%8s-+\n", "----------", "-----", - "------", "--------"); - fprintf(stderr, "| %10s | %5s | %6s | %8s |\n", "Piece ", "Total", "Passed", - "Passed %"); - fprintf(stderr, "+-%10s-+-%5s-+-%6s-+-%8s-+\n", "----------", "-----", - "------", "--------"); - for(unsigned i = 0; i < test_piece.size(); ++i) { - fprintf(stderr, "| %10s | %5d | %6d | %7.3f%% |\n", - test_piece[i].piece_name.c_str(), test_piece[i].num_passed + - test_piece[i].num_failed, test_piece[i].num_passed, - 100.0*(double)test_piece[i].num_passed / - (double)(test_piece[i].num_passed + test_piece[i].num_failed)); - - if(test_piece[i].num_passed == 0) { - pieces_fail++; - } else if(test_piece[i].num_failed == 0) { - pieces_passed++; - } else { - pieces_partial++; - } - } - fprintf(stderr, "+-%10s-+-%5s-+-%6s-+-%8s-+\n", "----------", "-----", - "------", "--------"); - - fprintf(stderr, "\nTotal pieces tested: %lu\n", test_piece.size()); - fprintf(stderr, "Passed: %d\n", pieces_passed); - fprintf(stderr, "Partially working: %d\n", pieces_partial); - fprintf(stderr, "Failed: %d\n", pieces_fail); + std::sort(test_piece.begin(), test_piece.end()); + + int pieces_passed = 0; + int pieces_partial = 0; + int pieces_fail = 0; + + fprintf(stderr, "+-%10s-+-%5s-+-%6s-+-%8s-+\n", "----------", "-----", + "------", "--------"); + fprintf(stderr, "| %10s | %5s | %6s | %8s |\n", "Piece ", "Total", "Passed", + "Passed %"); + fprintf(stderr, "+-%10s-+-%5s-+-%6s-+-%8s-+\n", "----------", "-----", + "------", "--------"); + for(unsigned i = 0; i < test_piece.size(); ++i) { + fprintf(stderr, "| %10s | %5d | %6d | %7.3f%% |\n", + test_piece[i].piece_name.c_str(), test_piece[i].num_passed + + test_piece[i].num_failed, test_piece[i].num_passed, + 100.0 * (double)test_piece[i].num_passed / + (double)(test_piece[i].num_passed + test_piece[i].num_failed)); + + if(test_piece[i].num_passed == 0) + pieces_fail++; + else if(test_piece[i].num_failed == 0) + pieces_passed++; + else + pieces_partial++; + } + fprintf(stderr, "+-%10s-+-%5s-+-%6s-+-%8s-+\n", "----------", "-----", + "------", "--------"); + + fprintf(stderr, "\nTotal pieces tested: %lu\n", test_piece.size()); + fprintf(stderr, "Passed: %d\n", pieces_passed); + fprintf(stderr, "Partially working: %d\n", pieces_partial); + fprintf(stderr, "Failed: %d\n", pieces_fail); } bool chess_tester::is_in_arr(const std::string& test_piece) const { - for(unsigned i = 0; i < CHESS_TEST_SIZE; ++i) { - if(chess_test_pieces[i] == test_piece) { - return true; - } - } + for(unsigned i = 0; i < CHESS_TEST_SIZE; ++i) + if(chess_test_pieces[i] == test_piece) + return true; - return false; + return false; } bool chess_tester::is_in_vec(const tested_pieces& piece) const { - for(unsigned i = 0; i < test_piece.size(); ++i) { - if(test_piece[i] == piece) { - return true; - } - } - return false; + for(unsigned i = 0; i < test_piece.size(); ++i) + if(test_piece[i] == piece) + return true; + return false; } -- cgit From 6d61c014039818d13a6908ae47caf377c8f948f3 Mon Sep 17 00:00:00 2001 From: ymherklotz Date: Thu, 19 Jan 2017 13:42:28 +0000 Subject: Backing up files changed on laptop --- src/chess_tester.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/chess_tester.cpp b/src/chess_tester.cpp index 9e20fd3..a8ec0b5 100644 --- a/src/chess_tester.cpp +++ b/src/chess_tester.cpp @@ -10,10 +10,9 @@ chess_tester::chess_tester() : ts_begin(false) { } void chess_tester::chess_begin_test_suite() { - if(ts_begin) { + if(ts_begin) fprintf(stderr, "Error: already started test suite\n"); - exit(1); - } + exit(1); ts_begin = true; } -- cgit From 43910de3a8899b332e697ba210436be70e6a6f7c Mon Sep 17 00:00:00 2001 From: ymherklotz Date: Sat, 21 Jan 2017 16:04:46 +0000 Subject: Change to lexer --- src/board_state_parser.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'src') diff --git a/src/board_state_parser.cpp b/src/board_state_parser.cpp index fac502f..5ff6bf3 100644 --- a/src/board_state_parser.cpp +++ b/src/board_state_parser.cpp @@ -28,7 +28,6 @@ BoardStateParser::~BoardStateParser() { void BoardStateParser::populateBoardState() { std::string currentLine; - unsigned index; boardStateVector.reserve(BOARD_SIZE); @@ -55,9 +54,7 @@ unsigned BoardStateParser::stringToInt(const std::string& str) { std::string BoardStateParser::getStateFromLine(const std::string& str) { std::string state; - for(unsigned i = 0; i < str.size(); ++i) - if(i > 1) - state += str[i]; + state = str.substr(2, str.length() - 2); return state; } -- cgit From 7ec074f67ae29a792e718fc48f7817b3a77b81ad Mon Sep 17 00:00:00 2001 From: ymherklotz Date: Mon, 23 Jan 2017 15:18:05 +0000 Subject: Created a seperate file --- src/board_state_parser.cpp | 3 ++- src/chess_board.cpp | 8 +++++--- src/chess_piece.cpp | 2 +- src/pawn.cpp | 8 ++++++++ 4 files changed, 16 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/board_state_parser.cpp b/src/board_state_parser.cpp index 5ff6bf3..288a491 100644 --- a/src/board_state_parser.cpp +++ b/src/board_state_parser.cpp @@ -12,6 +12,7 @@ */ #include "board_state_parser.hpp" +#include "chess_constats.hpp" using namespace ymhChessAI; @@ -29,7 +30,7 @@ BoardStateParser::~BoardStateParser() { void BoardStateParser::populateBoardState() { std::string currentLine; - boardStateVector.reserve(BOARD_SIZE); + boardStateVector.reserve(ChessConstants::BOARD_SIZE); if(!boardFile.is_open()) throw "Error: Can't open file"; diff --git a/src/chess_board.cpp b/src/chess_board.cpp index 051decd..e0967ad 100644 --- a/src/chess_board.cpp +++ b/src/chess_board.cpp @@ -12,18 +12,20 @@ */ #include "chess_board.hpp" +#include "chess_constants.hpp" + #include using namespace ymhChessAI; ChessBoard::ChessBoard() { - for(unsigned i = 0; i < BOARD_SIZE; ++i) + for(unsigned i = 0; i < ChessConstants::BOARD_SIZE; ++i) board.emplace_back(new EmptyPiece); } bool ChessBoard::populateBoard() { - for(auto&& piece : board) + for(auto && piece : board) piece = chessPiecePtr(new Knight); return true; } @@ -31,7 +33,7 @@ bool ChessBoard::populateBoard() { bool ChessBoard::printBoard() { int i = 0; - for(auto&& piece : board) { + for(auto && piece : board) { std::cout << i << " "; piece->move(1, 2); ++i; diff --git a/src/chess_piece.cpp b/src/chess_piece.cpp index d4d14e6..33ce34f 100644 --- a/src/chess_piece.cpp +++ b/src/chess_piece.cpp @@ -25,7 +25,7 @@ ChessPiece::ChessPiece(const int& x, const int& y, const Colour& colour) : m_x(x EmptyPiece::EmptyPiece() : ChessPiece() { } -EmptyPiece::EmptyPiece(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, colour) { +EmptyPiece::EmptyPiece(const int& x, const int& y) : ChessPiece(x, y, Colour::None) { } void EmptyPiece::move(const int& x, const int& y) { diff --git a/src/pawn.cpp b/src/pawn.cpp index 1374886..458b980 100644 --- a/src/pawn.cpp +++ b/src/pawn.cpp @@ -12,6 +12,7 @@ */ #include "chess_piece.hpp" +#include "chess_constants.hpp" using namespace ymhChessAI; @@ -23,4 +24,11 @@ Pawn::Pawn(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, } void Pawn::move(const int& x, const int& y) { + if(m_colour == Colour::White) { + if(m_y == ChessConstants::WHITE_PAWN_ROW) { + } + } else { + if(m_y == ChessConstants::BLACK_PAWN_ROW) { + } + } } -- cgit From 4e79ea68d54305e6705a3a8e83a730508b9f1c1b Mon Sep 17 00:00:00 2001 From: ymherklotz Date: Wed, 25 Jan 2017 13:22:29 +0000 Subject: Starting movement of the pawn --- src/pawn.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/pawn.cpp b/src/pawn.cpp index 458b980..5c5f1f6 100644 --- a/src/pawn.cpp +++ b/src/pawn.cpp @@ -7,13 +7,19 @@ * * ----------------------------------------------------------------------------- * - * Pawn class implementation + * Pawn class implementation, checking if there is a piece in the way of the + * one that is currently moving will be the chess boards concern + * + * Update: Checking if a piece is in the way will be the concern of this class + * because the move function has to perform the whole move. * */ #include "chess_piece.hpp" #include "chess_constants.hpp" +#include + using namespace ymhChessAI; @@ -24,11 +30,15 @@ Pawn::Pawn(const int& x, const int& y, const Colour& colour) : ChessPiece(x, y, } void Pawn::move(const int& x, const int& y) { + int move_length = y - (int)m_y; + + // First check what colour the pawn is if(m_colour == Colour::White) { - if(m_y == ChessConstants::WHITE_PAWN_ROW) { + // if this condition is met the pawn can move + if(move_length == -2 && m_y == ChessConstants::WHITE_PAWN_ROW) { + // we now have to check if there is a piece in between the pawn and + // it's desination } } else { - if(m_y == ChessConstants::BLACK_PAWN_ROW) { - } } } -- cgit