aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzedarider <ymherklotz@gmail.com>2016-11-10 18:19:22 +0000
committerzedarider <ymherklotz@gmail.com>2016-11-10 18:19:22 +0000
commit95f75aa7aacf5e0e1ef11e8d200f84de44bd891f (patch)
tree5ffafeb62866e121453b82e11e402a27a6a30256
parent375e4b989af8b4ac2c3479ea32f9324f29ce9071 (diff)
downloadChessAI-95f75aa7aacf5e0e1ef11e8d200f84de44bd891f.tar.gz
ChessAI-95f75aa7aacf5e0e1ef11e8d200f84de44bd891f.zip
finished the tester
-rwxr-xr-xbin/chess_aibin242832 -> 326304 bytes
-rw-r--r--include/chess_ai.hpp13
-rw-r--r--include/chess_tester.hpp89
-rw-r--r--src/chess_board.cpp3
-rw-r--r--src/chess_piece.cpp2
-rw-r--r--src/chess_tester.cpp125
-rw-r--r--src/main.cpp19
7 files changed, 229 insertions, 22 deletions
diff --git a/bin/chess_ai b/bin/chess_ai
index bb6ae57..6114edc 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 380d406..fc05b2a 100644
--- a/include/chess_ai.hpp
+++ b/include/chess_ai.hpp
@@ -112,6 +112,9 @@ namespace chess_ai {
// Create a chess board depending on the state
chess_board(board_state state);
+
+ // destructor to clean up the variables
+ ~chess_board();
// prints the current board state
void print_board();
@@ -132,6 +135,9 @@ namespace chess_ai {
// move piece using only x and y (for pawns)
move_error move_piece(unsigned x, unsigned y);
+ // moves a piece to an x, y coordinate
+ move_error move_piece(chess_piece piece, unsigned x, unsigned y);
+
// 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);
@@ -140,8 +146,6 @@ namespace chess_ai {
move_error move_piece(unsigned orig_x, unsigned orig_y,
unsigned dest_x, unsigned dest_y,
chess_piece& taken_piece);
-
- move_error move_piece(chess_piece piece, unsigned x, unsigned y);
// iterate through the list and return the pointer to change
square_iterator& iterate_board(square_iterator& it, unsigned x,
@@ -153,7 +157,7 @@ namespace chess_ai {
// initialises vector
void init_board_vector();
- // moves the pawn
+ // 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);
@@ -180,6 +184,9 @@ namespace chess_ai {
chess_piece(piece_type type, piece_colour colour, unsigned x,
unsigned y);
+ // destructor to clean up the variables
+ ~chess_piece();
+
// Set the type of the chess_piece
void set_type(piece_type type);
diff --git a/include/chess_tester.hpp b/include/chess_tester.hpp
new file mode 100644
index 0000000..c0c2f1c
--- /dev/null
+++ b/include/chess_tester.hpp
@@ -0,0 +1,89 @@
+#ifndef CHESS_TESTER_HPP
+#define CHESS_TESTER_HPP
+
+#define CHESS_TEST_SIZE 7
+
+#include <string>
+#include <vector>
+#include <cstdio>
+#include <cstdlib>
+#include <ctime>
+#include <algorithm>
+#include <iostream>
+
+struct tested_pieces {
+ std::string piece_name;
+ int num_passed;
+ int num_failed;
+
+ friend bool operator==(const tested_pieces& tp1, const tested_pieces& tp2) {
+ if(tp1.piece_name == tp2.piece_name) {
+ return true;
+ }
+ return false;
+ }
+
+ friend bool operator<(const tested_pieces& tp1, const tested_pieces& tp2) {
+ if(tp1.piece_name < tp2.piece_name) {
+ return true;
+ }
+ return false;
+ }
+};
+
+class chess_tester {
+
+public:
+
+ // sets all the initial values for the tester
+ chess_tester();
+
+ // begins the test suite so that this class can record the results and
+ // analyse them
+ void chess_begin_test_suite();
+
+ // begins the test
+ int chess_begin_test(std::string test_name);
+
+ // records the result of the test
+ void chess_end_test(int test_id, bool passed);
+
+ // analyses the results and prints them out
+ void chess_end_test_suite();
+
+ // see if the test is acceptable
+ bool is_in_arr(const std::string& test_piece) const;
+
+ // see if test is in vector
+ bool is_in_vec(const tested_pieces& piece) const;
+
+private:
+
+ // checks if the test suite is running
+ bool ts_begin;
+
+ // list of pieces to test
+ const std::string chess_test_pieces[CHESS_TEST_SIZE] = {
+ "<INTERNAL>",
+ "PAWN",
+ "ROOK",
+ "KNIGHT",
+ "BISHOP",
+ "QUEEN",
+ "KING"
+ };
+
+ // vector that contains the tested pieces
+ std::vector<tested_pieces> test_piece;
+
+ // the test_id of the current instruction
+ int test_id;
+
+ // the test id of the test that has been started
+ int test_id_test;
+
+ // the current test that is being tested
+ std::string current_test;
+};
+
+#endif
diff --git a/src/chess_board.cpp b/src/chess_board.cpp
index 4891981..8b449ca 100644
--- a/src/chess_board.cpp
+++ b/src/chess_board.cpp
@@ -62,6 +62,8 @@ chess_ai::chess_board::chess_board(board_state state) : SIZE(CHESS_BOARD_SIZE) {
}
}
+chess_ai::chess_board::~chess_board() {}
+
void chess_ai::chess_board::init_board_vector() {
for(unsigned i = 0; i < SIZE; ++i) {
std::vector<chess_ai::chess_piece> tmp_vec;
@@ -158,7 +160,6 @@ chess_ai::move_error chess_ai::chess_board::move_piece
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);
diff --git a/src/chess_piece.cpp b/src/chess_piece.cpp
index e81b9b7..83b664f 100644
--- a/src/chess_piece.cpp
+++ b/src/chess_piece.cpp
@@ -20,6 +20,8 @@ chess_ai::chess_piece::chess_piece(piece_type type, piece_colour colour,
unsigned x, unsigned y) :
type(type), colour(colour), x(x), y(y) {}
+chess_ai::chess_piece::~chess_piece() {}
+
void chess_ai::chess_piece::set_type(piece_type type) {
this->type = type;
}
diff --git a/src/chess_tester.cpp b/src/chess_tester.cpp
new file mode 100644
index 0000000..60f6ee0
--- /dev/null
+++ b/src/chess_tester.cpp
@@ -0,0 +1,125 @@
+#include "../include/chess_tester.hpp"
+
+chess_tester::chess_tester() : ts_begin(false) {
+ srand(time(NULL));
+
+ test_id = rand() % 0xffffffff;
+ test_id_test = test_id;
+
+}
+
+void chess_tester::chess_begin_test_suite() {
+ if(ts_begin) {
+ fprintf(stderr, "Error: already started test suite\n");
+ exit(1);
+ }
+
+ ts_begin = true;
+}
+
+int chess_tester::chess_begin_test(std::string test_name) {
+ if(!ts_begin) {
+ fprintf(stderr, "Error: the test suite hasn't been started yet\n");
+ exit(1);
+ }
+
+ if(!is_in_arr(test_name)) {
+ fprintf(stderr, "Error: the test name does not exist\n");
+ exit(1);
+ }
+
+ current_test = test_name;
+
+ test_id = rand() % 0xffffffff;
+ test_id_test = test_id;
+
+ return test_id;
+}
+
+void chess_tester::chess_end_test(int test_id, bool passed) {
+ if(test_id != test_id_test) {
+ 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;
+ } else {
+ tmp.num_passed = 0;
+ tmp.num_failed = 1;
+ }
+
+ if(test_piece.empty() || !is_in_vec(tmp)) {
+ test_piece.push_back(tmp);
+ } else {
+ for(unsigned i = 0; i < test_piece.size(); ++i) {
+ if(test_piece[i] == tmp) {
+ if(passed) {
+ test_piece[i].num_passed++;
+ } else {
+ test_piece[i].num_failed++;
+ }
+ }
+ }
+ }
+}
+
+void chess_tester::chess_end_test_suite() {
+ std::sort(test_piece.begin(), test_piece.end());
+
+ 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",
+ "Passed %");
+ fprintf(stderr, "+-%10s-+-%5s-+-%6s-+-%8s-+\n", "----------", "-----",
+ "------", "--------");
+ for(unsigned i = 0; i < test_piece.size(); ++i) {
+ fprintf(stderr, "| %10s | %5d | %6d | %7.3f%% |\n",
+ test_piece[i].piece_name.c_str(), test_piece[i].num_passed +
+ 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) {
+ pieces_passed++;
+ } else {
+ pieces_partial++;
+ }
+ }
+ 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);
+ fprintf(stderr, "Failed: %d\n", pieces_fail);
+}
+
+bool chess_tester::is_in_arr(const std::string& test_piece) const {
+ for(unsigned i = 0; i < CHESS_TEST_SIZE; ++i) {
+ if(chess_test_pieces[i] == test_piece) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+bool chess_tester::is_in_vec(const tested_pieces& piece) const {
+ for(unsigned i = 0; i < test_piece.size(); ++i) {
+ if(test_piece[i] == piece) {
+ return true;
+ }
+ }
+ return false;
+}
diff --git a/src/main.cpp b/src/main.cpp
index 81485f3..def5cf0 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -8,6 +8,7 @@
*/
#include "../include/chess_ai.hpp"
+#include "../include/chess_tester.hpp"
#include <iostream>
@@ -17,24 +18,6 @@ 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();
-
- board.remove_piece(5, 3);
-
- board.print_board();
-
- board.move_piece(4, 6, 4, 4);
- board.move_piece(4, 1, 4, 3);
- board.move_piece(3, 6, 3, 5);
-
- board.print_board();
return 0;
}