Skip to content

Commit

Permalink
Printing board works
Browse files Browse the repository at this point in the history
  • Loading branch information
trzysiek committed Sep 11, 2016
1 parent 5a1671a commit 9128d97
Show file tree
Hide file tree
Showing 11 changed files with 262 additions and 0 deletions.
5 changes: 5 additions & 0 deletions MAKE
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions board.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
39 changes: 39 additions & 0 deletions board.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef board_h
#define board_h

class Board;

#include "pieces.h"

#include <array>
#include <iostream>
#include <vector>

class Board {
public:
static constexpr unsigned int N = 8; // board is NxN
static constexpr unsigned int M = N * N;

typedef std::vector<Piece> Pieces;

std::vector<Piece> board;
std::vector<Piece> white;
std::vector<Piece> 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
5 changes: 5 additions & 0 deletions chess-player.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "board.h"

int main() {
Board b;
}
40 changes: 40 additions & 0 deletions db.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "db.h"
#include "pieces.h"

// TODO: dodac dobra mapke
std::map<std::string, Board::Pieces> 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)
}},
};
13 changes: 13 additions & 0 deletions db.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef db_h
#define db_h

#include "board.h"

#include <map>
#include <string>

struct DB {
static std::map<std::string, Board::Pieces> starting_pieces;
};

#endif // db_h
20 changes: 20 additions & 0 deletions pieces.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
82 changes: 82 additions & 0 deletions pieces.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#ifndef pieces_h
#define pieces_h

class Piece;

#include "board.h"

#include <iostream>
#include <vector>

class Piece {
public:
int i; // index
std::string to_string = "*";
int value = -1;

std::vector<std::pair<int, int>> 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
23 changes: 23 additions & 0 deletions tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <iostream>
#include <string>

#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");
}
1 change: 1 addition & 0 deletions util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "util.h"
1 change: 1 addition & 0 deletions util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <iostream>

0 comments on commit 9128d97

Please sign in to comment.