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 --- include/board_state_parser.hpp | 44 +++++++++++++++++++++++++++++ include/chess_board.hpp | 4 +-- include/chess_piece.hpp | 16 ----------- res/default.board | 4 +++ src/board_state_parser.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++ src/chess_board.cpp | 13 ++++----- src/knight.cpp | 2 ++ src/test_bench.cpp | 5 +--- 8 files changed, 120 insertions(+), 31 deletions(-) create mode 100644 include/board_state_parser.hpp create mode 100644 res/default.board create mode 100644 src/board_state_parser.cpp diff --git a/include/board_state_parser.hpp b/include/board_state_parser.hpp new file mode 100644 index 0000000..39286d4 --- /dev/null +++ b/include/board_state_parser.hpp @@ -0,0 +1,44 @@ +/* + * + * author: Yann Herklotz + * username: ymherklotz + * email: ymherklotz@gmail.com + * date created: 15/01/17 + * + * ----------------------------------------------------------------------------- + * + * Parser that takes in the board state and outputs it into a format that the + * Chess Board class can read + * + */ + +#ifndef YMH_BOARD_STATE_PARSER_HPP +#define YMH_BOARD_STATE_PARSER_HPP + +#include +#include +#include + +namespace ymhChessAI { +typedef std::vector boardStateData; + +class BoardStateParser { +public: + BoardStateParser(); + BoardStateParser(const std::string& boardFileName); + ~BoardStateParser(); + + void populateBoardState(); + + static unsigned stringToInt(const std::string& str); + static std::string getStateFromLine(const std::string& str); +protected: +private: + static const unsigned BOARD_SIZE = 64; + + boardStateData boardStateVector; + std::ifstream boardFile; +}; +} + +#endif diff --git a/include/chess_board.hpp b/include/chess_board.hpp index fef1f69..d3516fc 100644 --- a/include/chess_board.hpp +++ b/include/chess_board.hpp @@ -27,13 +27,11 @@ typedef std::unique_ptr chessPiecePtr; class ChessBoard { public: ChessBoard(); - ChessBoard(const ChessBoard& other) = default; - virtual ~ChessBoard() = default; void printBoard(); protected: private: - static const unsigned CHESS_BOARD_SIZE = 64; + static const unsigned BOARD_SIZE = 64; boardVector board; }; diff --git a/include/chess_piece.hpp b/include/chess_piece.hpp index ce6ca5c..5febc99 100644 --- a/include/chess_piece.hpp +++ b/include/chess_piece.hpp @@ -35,12 +35,8 @@ class ChessPiece { public: // constructor sets the initial position of the chess piece to 0, 0 ChessPiece(); - // using the default constructor for the copy constructor - ChessPiece(const ChessPiece& other) = default; // initialise piece at a specific location on the board to save code ChessPiece(const int& x, const int& y, const Colour& colour); - // using default virtual d, const Colour &colourfault; - virtual ~ChessPiece() = default; // pure virtual function to move a piece as it depends completely on the // implementation of the piece @@ -60,9 +56,7 @@ protected: class King : public ChessPiece { public: King(); - King(const King& other) = default; King(const int& x, const int& y, const Colour& colour); - ~King() = default; virtual void move(const int& x, const int& y); protected: @@ -75,9 +69,7 @@ private: class Queen : public ChessPiece { public: Queen(); - Queen(const Queen& other) = default; Queen(const int& x, const int& y, const Colour& colour); - ~Queen() = default; virtual void move(const int& x, const int& y); protected: @@ -90,9 +82,7 @@ private: class Rook : public ChessPiece { public: Rook(); - Rook(const Rook& other) = default; Rook(const int& x, const int& y, const Colour& colour); - ~Rook() = default; virtual void move(const int& x, const int& y); protected: @@ -105,9 +95,7 @@ private: class Bishop : public ChessPiece { public: Bishop(); - Bishop(const Bishop &other) = default; Bishop(const int& x, const int& y, const Colour& colour); - ~Bishop() = default; virtual void move(const int& x, const int& y); protected: @@ -120,9 +108,7 @@ private: class Knight : public ChessPiece { public: Knight(); - Knight(const Knight& other) = default; Knight(const int& x, const int& y, const Colour& colour); - ~Knight() = default; virtual void move(const int& x, const int& y); protected: @@ -135,9 +121,7 @@ private: class Pawn : public ChessPiece { public: Pawn(); - Pawn(const Pawn& other) = default; Pawn(const int& x, const int& y, const Colour& colour); - ~Pawn() = default; virtual void move(const int& x, const int& y); protected: diff --git a/res/default.board b/res/default.board new file mode 100644 index 0000000..d4ea5b0 --- /dev/null +++ b/res/default.board @@ -0,0 +1,4 @@ +0 0br 1bn 2bb 3bq 4bk 5bb 6bn 7br +1 0bp 1bp 2bp 3bp 4bp 5bp 6bp 7bp +6 0wp 1wp 2wp 3wp 4wp 5wp 6wp 7wp +7 0wr 1wn 2wb 3wq 4wk 5wb 6wn 7wr 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