aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzedarider <ymherklotz@gmail.com>2016-11-25 23:24:19 +0000
committerzedarider <ymherklotz@gmail.com>2016-11-25 23:24:19 +0000
commit4a876b0d57566fc3f26c5f2db9ee7427854d468b (patch)
tree254f95c76b23d783172d44a22b03eb008e3ec726
parentb12a0df92452c8da471e36218effadd6f5958556 (diff)
downloadChessAI-4a876b0d57566fc3f26c5f2db9ee7427854d468b.tar.gz
ChessAI-4a876b0d57566fc3f26c5f2db9ee7427854d468b.zip
starting building the testbench
-rw-r--r--.atom-build.json3
-rw-r--r--.clang_complete1
-rwxr-xr-xbin/chess_aibin95168 -> 96232 bytes
-rw-r--r--include/chess_ai.hpp32
-rw-r--r--src/chess_board.cpp57
-rw-r--r--src/chess_piece.cpp6
-rw-r--r--src/chess_tester.cpp18
-rw-r--r--src/main.cpp.bck (renamed from src/main.cpp)8
-rw-r--r--src/test_bench.cpp48
9 files changed, 129 insertions, 44 deletions
diff --git a/.atom-build.json b/.atom-build.json
new file mode 100644
index 0000000..33d28f5
--- /dev/null
+++ b/.atom-build.json
@@ -0,0 +1,3 @@
+{
+ "cmd": "make"
+}
diff --git a/.clang_complete b/.clang_complete
new file mode 100644
index 0000000..30679be
--- /dev/null
+++ b/.clang_complete
@@ -0,0 +1 @@
+-Iinclude
diff --git a/bin/chess_ai b/bin/chess_ai
index 1494202..410298b 100755
--- a/bin/chess_ai
+++ b/bin/chess_ai
Binary files differ
diff --git a/include/chess_ai.hpp b/include/chess_ai.hpp
index 3a0d95b..06cbd43 100644
--- a/include/chess_ai.hpp
+++ b/include/chess_ai.hpp
@@ -29,7 +29,7 @@ namespace chess_ai {
// typedefs for iterators to access elements easier
- typedef std::vector<std::vector<chess_ai::chess_piece>>::
+ typedef std::vector<std::vector<chess_ai::chess_piece> >::
iterator vector_iterator;
typedef std::vector<chess_ai::chess_piece>::iterator square_iterator;
@@ -119,10 +119,17 @@ namespace chess_ai {
// prints the current board state
void print_board();
+ // returns the piece at the location
+ chess_piece at(unsigned x, unsigned y);
+
// Set a piece somewhere on the board replacing anything that was there
// previously
void set_piece(chess_piece piece);
+ // set piece directly on board
+ void set_piece(piece_type type, piece_colour colour, unsigned x,
+ unsigned y);
+
// removes a piece from the board at location
void remove_piece(chess_piece piece);
@@ -140,16 +147,15 @@ namespace chess_ai {
// move piece with x and y as original and final destination
move_error move_piece(unsigned orig_x, unsigned orig_y,
- unsigned dest_x, unsigned dest_y);
+ unsigned dest_x, unsigned dest_y);
// move piece and return a piece that has been captured
move_error move_piece(unsigned orig_x, unsigned orig_y,
- unsigned dest_x, unsigned dest_y,
- chess_piece& taken_piece);
+ unsigned dest_x, unsigned dest_y, chess_piece& taken_piece);
// iterate through the list and return the pointer to change
square_iterator& iterate_board(square_iterator& it, unsigned x,
- unsigned y);
+ unsigned y);
private:
@@ -159,14 +165,14 @@ namespace chess_ai {
// moves the pawn and tests all the cases that it should.
move_error move_pawn(square_iterator it, square_iterator new_it,
- chess_piece& taken_piece);
+ chess_piece& taken_piece);
// The size of the chess board is a constant and hence defined by a
// preprocessed define statement.
const unsigned SIZE;
// The actual board where the values of the pieces will be changed.
- std::vector<std::vector<chess_piece>> grid;
+ std::vector<std::vector<chess_piece> > grid;
};
class chess_piece {
@@ -182,11 +188,21 @@ namespace chess_ai {
// Finally initialise the chess piece to a specified piece
chess_piece(piece_type type, piece_colour colour, unsigned x,
- unsigned y);
+ unsigned y);
// destructor to clean up the variables
~chess_piece();
+ // overload == operator
+ friend bool operator==(const chess_piece& p1, const chess_piece& p2) {
+ if(p1.type == p2.type && p1.colour == p2.colour && p1.x == p2.x &&
+ p1.y == p2.y) {
+
+ return true;
+ }
+ return false;
+ }
+
// Set the type of the chess_piece
void set_type(piece_type type);
diff --git a/src/chess_board.cpp b/src/chess_board.cpp
index 8b449ca..de21015 100644
--- a/src/chess_board.cpp
+++ b/src/chess_board.cpp
@@ -1,4 +1,4 @@
-#include "../include/chess_ai.hpp"
+#include "chess_ai.hpp"
chess_ai::chess_board::chess_board() : SIZE(CHESS_BOARD_SIZE) {
init_board_vector();
@@ -8,27 +8,27 @@ 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) {
@@ -48,7 +48,7 @@ chess_ai::chess_board::chess_board(board_state state) : SIZE(CHESS_BOARD_SIZE) {
piece.set_colour(colour);
piece.set_x(sqr_index);
piece.set_y(vec_index);
-
+
*it_sqr = piece;
}
}
@@ -81,14 +81,14 @@ void chess_ai::chess_board::print_board() {
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 << "---+";
@@ -107,11 +107,28 @@ void chess_ai::chess_board::print_board() {
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);
}
@@ -128,9 +145,9 @@ chess_ai::move_error chess_ai::chess_board::move_piece(chess_piece piece) {
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)
+ if(it->colour == white)
return move_piece(x, y, x, y-1);
return move_piece(x, y, x, y+1);
}
@@ -139,13 +156,13 @@ chess_ai::move_error chess_ai::chess_board::move_piece(unsigned x, unsigned y) {
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);
}
@@ -153,13 +170,13 @@ chess_ai::move_error chess_ai::chess_board::move_piece
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);
@@ -175,14 +192,14 @@ chess_ai::move_error chess_ai::chess_board::move_piece
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;
@@ -194,7 +211,7 @@ chess_ai::square_iterator& chess_ai::chess_board::iterate_board
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) ||
diff --git a/src/chess_piece.cpp b/src/chess_piece.cpp
index 83b664f..f73d4c1 100644
--- a/src/chess_piece.cpp
+++ b/src/chess_piece.cpp
@@ -1,14 +1,14 @@
-#include "../include/chess_ai.hpp"
+#include "chess_ai.hpp"
chess_ai::chess_piece::chess_piece(piece_type type, piece_colour colour) :
type(type), colour(colour) {
-
+
if(colour == black) {
y = 0;
} else {
y = 7;
}
-
+
if(type == king) {
x = 4;
} else {
diff --git a/src/chess_tester.cpp b/src/chess_tester.cpp
index 60f6ee0..6f3745c 100644
--- a/src/chess_tester.cpp
+++ b/src/chess_tester.cpp
@@ -1,4 +1,4 @@
-#include "../include/chess_tester.hpp"
+#include "chess_tester.hpp"
chess_tester::chess_tester() : ts_begin(false) {
srand(time(NULL));
@@ -29,7 +29,7 @@ int chess_tester::chess_begin_test(std::string test_name) {
}
current_test = test_name;
-
+
test_id = rand() % 0xffffffff;
test_id_test = test_id;
@@ -41,10 +41,10 @@ void chess_tester::chess_end_test(int test_id, bool passed) {
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;
@@ -52,7 +52,7 @@ void chess_tester::chess_end_test(int test_id, bool passed) {
tmp.num_passed = 0;
tmp.num_failed = 1;
}
-
+
if(test_piece.empty() || !is_in_vec(tmp)) {
test_piece.push_back(tmp);
} else {
@@ -61,7 +61,7 @@ void chess_tester::chess_end_test(int test_id, bool passed) {
if(passed) {
test_piece[i].num_passed++;
} else {
- test_piece[i].num_failed++;
+ test_piece[i].num_failed++;
}
}
}
@@ -74,7 +74,7 @@ void chess_tester::chess_end_test_suite() {
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",
@@ -87,7 +87,7 @@ void chess_tester::chess_end_test_suite() {
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) {
@@ -98,7 +98,7 @@ void chess_tester::chess_end_test_suite() {
}
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);
diff --git a/src/main.cpp b/src/main.cpp.bck
index def5cf0..2fd9be2 100644
--- a/src/main.cpp
+++ b/src/main.cpp.bck
@@ -1,14 +1,14 @@
/*
- description:
+ description: This is the main file for the chess_ai
author: Yann Herklotz <ymherklotz@gmail.com>
date created: DD-MM-YYYY
*/
-#include "../include/chess_ai.hpp"
-#include "../include/chess_tester.hpp"
+#include "chess_ai.hpp"
+#include "chess_tester.hpp"
#include <iostream>
@@ -18,6 +18,6 @@ using namespace chess_ai;
int main(int argc, char** argv) {
(void)argc;
(void)argv;
-
+
return 0;
}
diff --git a/src/test_bench.cpp b/src/test_bench.cpp
new file mode 100644
index 0000000..68c93fb
--- /dev/null
+++ b/src/test_bench.cpp
@@ -0,0 +1,48 @@
+/*
+
+ description: This is the test_bench file for the chess_ai
+
+ author: Yann Herklotz <ymherklotz@gmail.com>
+ date created: DD-MM-YYYY
+
+ */
+
+#include "chess_ai.hpp"
+#include "chess_tester.hpp"
+
+#include <iostream>
+
+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();
+
+ return 0;
+}