aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzedarider <ymherklotz@gmail.com>2016-11-05 12:49:32 +0000
committerzedarider <ymherklotz@gmail.com>2016-11-05 12:49:32 +0000
commit4716585d5acb45b00960eebb4f8ae4580e580da0 (patch)
treece5caa0fed7216ba83f8a8ac979807b332b6f3ce
parent146e32f222be037993fd4d1ab6148eda6c895d08 (diff)
downloadChessAI-4716585d5acb45b00960eebb4f8ae4580e580da0.tar.gz
ChessAI-4716585d5acb45b00960eebb4f8ae4580e580da0.zip
adding initial files
-rw-r--r--Makefile34
-rwxr-xr-xbin/chess_aibin0 -> 234216 bytes
-rw-r--r--include/chess_ai.hpp176
-rw-r--r--src/chess_board.cpp157
-rw-r--r--src/chess_piece.cpp66
-rw-r--r--src/main.cpp30
6 files changed, 463 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..3c0f9e7
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,34 @@
+CC := g++ # this is the main compiler
+# CC := clange --analyze # and comment out the linker last line
+SRCDIR := src
+BUILDDIR := build
+TARGET := bin/chess_ai
+
+SRCEXT := cpp
+SOURCES := $(shell find $(SRCDIR) -type f -name "*.$(SRCEXT)")
+OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
+CFLAGS := -g -Wall -Wextra
+LIB :=
+INC := -I include
+
+$(TARGET): $(OBJECTS)
+ @echo " Linking..."
+ @echo " $(CC) $^ -o $(TARGET) $(LIB)"; $(CC) $^ -o $(TARGET) $(LIB)
+
+$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
+ @mkdir -p $(BUILDDIR)
+ @echo " $(CC) $(CFLAGS) $(INC) -c -o $@ $<"; $(CC) $(CFLAGS) $(INC) -c -o $@ $<
+
+clean:
+ @echo " Cleaning..."
+ @echo " $(RM) -r $(BUILDDIR) $(TARGET)"; $(RM) -r $(BUILDDIR) $(TARGET)
+
+# Tests
+tester:
+ @echo " $(CC) $(CFLAGS) test/tester.cpp $(INC) $(LIB) -o bin/tester"; $(CC) $(CFLAGS) test/tester.cpp $(INC) $(LIB) -o bin/tester
+
+# Spikes
+ticket:
+ @echo " $(CC) $(CFLAGS) spikes/ticket.cpp $(INC) $(LIB) -o bin/ticket"; $(CC) $(CFLAGS) spikes/ticket.cpp $(INC) $(LIB) -o bin/ticket
+
+.PHONY: clean
diff --git a/bin/chess_ai b/bin/chess_ai
new file mode 100755
index 0000000..a09b495
--- /dev/null
+++ b/bin/chess_ai
Binary files differ
diff --git a/include/chess_ai.hpp b/include/chess_ai.hpp
new file mode 100644
index 0000000..c682bd2
--- /dev/null
+++ b/include/chess_ai.hpp
@@ -0,0 +1,176 @@
+#ifndef CHESS_AI_HPP
+#define CHESS_AI_HPP
+
+#define CHESS_BOARD_SIZE 8
+
+#include <vector>
+#include <iostream>
+#include <string>
+
+namespace chess_ai {
+
+ enum piece_type : unsigned;
+ enum piece_colour : unsigned;
+ enum board_state : unsigned;
+ class chess_board;
+ class chess_piece;
+
+ // Describes the different types of chess pieces there are on the board
+ enum piece_type : unsigned {
+ // A pawn can only move forward twice on the first move, otherwise only
+ // once. It only take pieces that are on the diagonals in front of it,
+ // or through en passant.
+ // They are chess_initially placed as a line in front of the other pieces.
+ pawn,
+
+ // A rook can only move vertically or horizontally and they are placed
+ // in the corners of the board.
+ rook,
+
+ // A knight can only move to 8 locations around it which are specified
+ // by moving twice horizontally or vertically in a row and once
+ // diagonally. These are placed next to the rook.
+ knight,
+
+ // A Bishop can only move diagonally and are placed next to the knight
+ bishop,
+
+ // The queen combines the bishop and the rook and is placed on either
+ // d1 or d8
+ queen,
+
+ // The king moves like the queen but only for one square and is placed
+ // on e1 or e8
+ king,
+
+ // empty square
+ empty
+ };
+
+ // Describes the colour of the pieces
+ enum piece_colour : unsigned {
+ // Looking at the board white will be at the bottom
+ white,
+ // Black will be at the top
+ black,
+ // no colour
+ none
+ };
+
+ // just describes if the board is full or empty
+ enum board_state : unsigned {
+ // The starting position of the board with all pieces in the right
+ // position
+ initial,
+ // While the game is going this will be the state
+ playing,
+ // When the game is over
+ finished,
+ // When the board is completely empty
+ clear
+ };
+
+ // The chess board that will be played on
+ class chess_board {
+ public:
+
+ // Create an empty chess board
+ chess_board();
+
+ // Create a chess board depending on the state
+ chess_board(board_state state);
+
+ // prints the current board state
+ void print_board();
+
+ // Set a piece somewhere on the board replacing anything that was there
+ // previously
+ void set_piece(chess_piece piece);
+
+ // removes a piece from the board at location
+ void remove_piece(chess_piece piece);
+
+ // remove piece at a specific location only
+ void remove_piece(unsigned x, unsigned y);
+
+ protected:
+
+ void init_board_vector();
+
+ private:
+
+ // The size of the chess board is a constant and hence defined by a
+ // preprocessed define statement.
+ unsigned const SIZE = CHESS_BOARD_SIZE;
+
+ // The actual board where the values of the pieces will be changed.
+ std::vector<std::vector<chess_piece>> grid;
+ };
+
+ // Any chess piece in the game
+ class chess_piece {
+ friend class chess_board;
+ public:
+
+ // Initialises the chess piece to an empty square on the board
+ chess_piece();
+
+ // Otherwise initialise the chess piece to a king or queen where the
+ // location is alreay known
+ chess_piece(piece_type type, piece_colour colour);
+
+ // Finally initialise the chess piece to a specified piece
+ chess_piece(piece_type type, piece_colour colour, unsigned x,
+ unsigned y);
+
+ // Set the type of the chess_piece
+ void set_type(piece_type type);
+
+ // set the colour of the piece
+ void set_colour(piece_colour colour);
+
+ // set the x coordinate of the piece
+ void set_x(unsigned x);
+
+ // set the y coordinate of the piece
+ void set_y(unsigned y);
+
+ // set the different values
+ void set(piece_type type, piece_colour colour, unsigned x, unsigned y);
+
+ // overloading operators
+
+ // so that we can make two copies of a point
+ chess_piece& operator=(const chess_piece& piece) {
+ if(this != &piece) {
+ this->set(piece.type, piece.colour, piece.x, piece.y);
+ }
+ return *this;
+ }
+
+ // overload ++ operator for pawns
+ chess_piece& operator++() {
+ return *this;
+ }
+
+ // return a printable version of the square
+ std::string str();
+
+ protected:
+ private:
+
+ // Type of the chess piece, eg. bishop or queen
+ piece_type type;
+
+ // Colour of the chess piece
+ piece_colour colour;
+
+ // x location of the chess piece
+ unsigned x;
+
+ // y location of the chess piece
+ unsigned y;
+ };
+}
+
+#endif
diff --git a/src/chess_board.cpp b/src/chess_board.cpp
new file mode 100644
index 0000000..9a6848b
--- /dev/null
+++ b/src/chess_board.cpp
@@ -0,0 +1,157 @@
+#include "../include/chess_ai.hpp"
+
+typedef std::vector<std::vector<chess_ai::chess_piece>>::
+iterator vector_iterator;
+typedef std::vector<chess_ai::chess_piece>::iterator square_iterator;
+
+chess_ai::chess_board::chess_board() {
+ init_board_vector();
+}
+
+chess_ai::chess_board::chess_board(board_state state) {
+ 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;
+ }
+ }
+ }
+}
+
+void chess_ai::chess_board::init_board_vector() {
+ for(unsigned i = 0; i < SIZE; ++i) {
+ std::vector<chess_ai::chess_piece> 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;
+}
+
+void chess_ai::chess_board::set_piece(chess_piece piece) {
+ 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 == piece.y && sqr_index == piece.x) {
+ *it_sqr = piece;
+ }
+ }
+ }
+}
+
+void chess_ai::chess_board::remove_piece(chess_piece piece) {
+ 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 == piece.y && sqr_index == piece.x) {
+ chess_piece empty_piece;
+ *it_sqr = empty_piece;
+ }
+ }
+ }
+}
+
+void chess_ai::chess_board::remove_piece(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) {
+ chess_piece empty_piece;
+ *it_sqr = empty_piece;
+ }
+ }
+ }
+}
diff --git a/src/chess_piece.cpp b/src/chess_piece.cpp
new file mode 100644
index 0000000..cd28ef8
--- /dev/null
+++ b/src/chess_piece.cpp
@@ -0,0 +1,66 @@
+#include "../include/chess_ai.hpp"
+
+chess_ai::chess_piece::chess_piece() {
+ type = empty;
+ colour = none;
+ x = -1;
+ y = -1;
+}
+
+chess_ai::chess_piece::chess_piece(piece_type type, piece_colour colour) {
+ this->type = type;
+ this->colour = colour;
+
+ 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) {
+ this->type = type;
+ this->colour = colour;
+ this->x = x;
+ this->y = y;
+
+}
+
+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;
+}
+
+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";
+}
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 0000000..b41c919
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,30 @@
+/*
+
+ description:
+
+ author: Yann Herklotz <ymherklotz@gmail.com>
+ date created: DD-MM-YYYY
+
+ */
+
+#include "../include/chess_ai.hpp"
+
+#include <iostream>
+
+using namespace std;
+using namespace chess_ai;
+
+int main(int argc, char** argv) {
+ (void)argc;
+ (void)argv;
+
+ chess_board board(initial);
+ board.print_board();
+
+ chess_piece piece(rook, white, 5, 3);
+ board.set_piece(piece);
+
+ board.print_board();
+
+ return 0;
+}