From 9128d97cc37ddd54d10c6d11e6607f0fe8a3a109 Mon Sep 17 00:00:00 2001 From: trzysiek Date: Sun, 11 Sep 2016 14:50:07 +0200 Subject: [PATCH] Printing board works --- MAKE | 5 +++ board.cpp | 33 +++++++++++++++++++ board.h | 39 +++++++++++++++++++++++ chess-player.cpp | 5 +++ db.cpp | 40 +++++++++++++++++++++++ db.h | 13 ++++++++ pieces.cpp | 20 ++++++++++++ pieces.h | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ tests.cpp | 23 ++++++++++++++ util.cpp | 1 + util.h | 1 + 11 files changed, 262 insertions(+) create mode 100755 MAKE create mode 100644 board.cpp create mode 100644 board.h create mode 100644 chess-player.cpp create mode 100644 db.cpp create mode 100644 db.h create mode 100644 pieces.cpp create mode 100644 pieces.h create mode 100644 tests.cpp create mode 100644 util.cpp create mode 100644 util.h diff --git a/MAKE b/MAKE new file mode 100755 index 0000000..1fe0b8d --- /dev/null +++ b/MAKE @@ -0,0 +1,5 @@ +g++ -std=c++11 pieces.cpp -c +g++ -std=c++11 board.cpp -c +g++ -std=c++11 db.cpp -c +g++ -std=c++11 chess-player.cpp pieces.o board.o db.o -o chess +g++ -std=c++11 tests.cpp pieces.o board.o db.o -o tests diff --git a/board.cpp b/board.cpp new file mode 100644 index 0000000..8bbcb21 --- /dev/null +++ b/board.cpp @@ -0,0 +1,33 @@ +#include "board.h" +#include "db.h" + +Board::Pieces Board::get_default_white_pieces() { + return DB::starting_pieces["white"]; +} + +Board::Pieces Board::get_default_black_pieces() { + return DB::starting_pieces["black"]; +} + +void Board::fill_board_from_pieces() { + std::cerr << white.size() << " " << nigga.size() << std::endl; + board.resize(M); + for (unsigned int i = 0; i < M; ++i) + board[i] = Piece(); + for (const auto& p : white) { + std::cerr << p << std::endl; + board[p.i] = p; + } + for (const auto& p : nigga) + board[p.i] = p; +} + +std::ostream& operator<<(std::ostream &os, const Board& b) { + for (int i = 0; i < b.board.size(); ++i) { + if (i > 0 && i % Board::N == 0) + std::cerr << std::endl; + std::cerr << b.board[i]; + } + std::cerr << std::endl; + return os; +} diff --git a/board.h b/board.h new file mode 100644 index 0000000..a47e27b --- /dev/null +++ b/board.h @@ -0,0 +1,39 @@ +#ifndef board_h +#define board_h + +class Board; + +#include "pieces.h" + +#include +#include +#include + +class Board { + public: + static constexpr unsigned int N = 8; // board is NxN + static constexpr unsigned int M = N * N; + + typedef std::vector Pieces; + + std::vector board; + std::vector white; + std::vector nigga; + + Board() { + white = get_default_white_pieces(); + nigga = get_default_black_pieces(); + fill_board_from_pieces(); + } + + void print_console() const; + friend std::ostream& operator<<(std::ostream &os, const Board& b); + + private: + static Pieces get_default_white_pieces(); + static Pieces get_default_black_pieces(); + void fill_board_from_pieces(); +}; + + +#endif // board_h diff --git a/chess-player.cpp b/chess-player.cpp new file mode 100644 index 0000000..1309602 --- /dev/null +++ b/chess-player.cpp @@ -0,0 +1,5 @@ +#include "board.h" + +int main() { + Board b; +} diff --git a/db.cpp b/db.cpp new file mode 100644 index 0000000..86462eb --- /dev/null +++ b/db.cpp @@ -0,0 +1,40 @@ +#include "db.h" +#include "pieces.h" + +// TODO: dodac dobra mapke +std::map DB::starting_pieces = { + {"white", {Pawn(8), + Pawn(9), + Pawn(10), + Pawn(11), + Pawn(12), + Pawn(13), + Pawn(14), + Pawn(15), + Rook(0), + Knight(1), + Bishop(2), + Queen(3), + King(4), + Bishop(5), + Knight(6), + Rook(7) + }}, + {"black", {Pawn(48), + Pawn(49), + Pawn(50), + Pawn(51), + Pawn(52), + Pawn(53), + Pawn(54), + Pawn(55), + Rook(56), + Knight(57), + Bishop(58), + Queen(59), + King(60), + Bishop(61), + Knight(62), + Rook(63) + }}, +}; diff --git a/db.h b/db.h new file mode 100644 index 0000000..a09d1c0 --- /dev/null +++ b/db.h @@ -0,0 +1,13 @@ +#ifndef db_h +#define db_h + +#include "board.h" + +#include +#include + +struct DB { + static std::map starting_pieces; +}; + +#endif // db_h diff --git a/pieces.cpp b/pieces.cpp new file mode 100644 index 0000000..a54b6fe --- /dev/null +++ b/pieces.cpp @@ -0,0 +1,20 @@ +#include "pieces.h" + +const std::string Pawn::TO_STRING = "P"; +const std::string Knight::TO_STRING = "N"; +const std::string Bishop::TO_STRING = "B"; +const std::string Rook::TO_STRING = "R"; +const std::string Queen::TO_STRING = "Q"; +const std::string King::TO_STRING = "K"; + +const int Pawn::DEFAULT_VALUE = 1; +const int Knight::DEFAULT_VALUE = 3; +const int Bishop::DEFAULT_VALUE = 1; +const int Rook::DEFAULT_VALUE = 5; +const int Queen::DEFAULT_VALUE = 9; +const int King::DEFAULT_VALUE = 4; + +std::ostream& operator<<(std::ostream& os, const Piece& p) { + std::cerr << p.to_string; + return os; +} diff --git a/pieces.h b/pieces.h new file mode 100644 index 0000000..9cfa785 --- /dev/null +++ b/pieces.h @@ -0,0 +1,82 @@ +#ifndef pieces_h +#define pieces_h + +class Piece; + +#include "board.h" + +#include +#include + +class Piece { + public: + int i; // index + std::string to_string = "*"; + int value = -1; + + std::vector> list_of_possible_moves(const Board&); + friend std::ostream& operator<<(std::ostream&, const Piece&); + + Piece& operator=(Piece&& p) = default; + Piece& operator=(const Piece& p) = default; + Piece (const Piece& p) : i(p.i), to_string(p.to_string), value(p.value) {} + Piece (int i) : i(i) {} + Piece () : Piece(0) {} + Piece (int i, int value, std::string to_str) : i(i), value(value), to_string(to_str) {} + Piece (int value, std::string to_str) : Piece(0, value, to_str) {} +}; + +class Pawn : public Piece { + private: + static const int DEFAULT_VALUE; + static const std::string TO_STRING; + public: + Pawn (int i) : Piece(i, DEFAULT_VALUE, TO_STRING) {} + Pawn () : Pawn(0) {} +}; + +class Knight : public Piece { + private: + static const int DEFAULT_VALUE; + static const std::string TO_STRING; + public: + Knight (int i) : Piece(i, DEFAULT_VALUE, TO_STRING) {} + Knight () : Knight(0) {} +}; + +class Bishop : public Piece { + private: + static const int DEFAULT_VALUE; + static const std::string TO_STRING; + public: + Bishop (int i) : Piece(i, DEFAULT_VALUE, TO_STRING) {} + Bishop () : Bishop(0) {} +}; + +class Rook : public Piece { + private: + static const int DEFAULT_VALUE; + static const std::string TO_STRING; + public: + Rook (int i) : Piece(i, DEFAULT_VALUE, TO_STRING) {} + Rook () : Rook(0) {} +}; + +class Queen : public Piece { + private: + static const int DEFAULT_VALUE; + static const std::string TO_STRING; + public: + Queen (int i) : Piece(i, DEFAULT_VALUE, TO_STRING) {} + Queen () : Queen(0) {} +}; + +class King : public Piece { + private: + static const int DEFAULT_VALUE; + static const std::string TO_STRING; + public: + King (int i) : Piece(i, DEFAULT_VALUE, TO_STRING) {} + King () : King(0) {} +}; +#endif // pieces_h diff --git a/tests.cpp b/tests.cpp new file mode 100644 index 0000000..98f006c --- /dev/null +++ b/tests.cpp @@ -0,0 +1,23 @@ +#include +#include + +#include "board.h" + +bool test_printing_board() { + try { + Board b; + std::cerr << b << std::endl; + return true; + } + catch (...) { + return false; + } +} + +void msg(int nr, bool success, std::string m) { + std::cerr << "Test nr " << nr << ((!success) ? " FAILED! " : " Passed. ") << "(" << m << ")" << std::endl; +} + +int main() { + msg(1, test_printing_board(), "Printing board"); +} diff --git a/util.cpp b/util.cpp new file mode 100644 index 0000000..408a76a --- /dev/null +++ b/util.cpp @@ -0,0 +1 @@ +#include "util.h" diff --git a/util.h b/util.h new file mode 100644 index 0000000..604782e --- /dev/null +++ b/util.h @@ -0,0 +1 @@ +#include