diff --git a/include/BitBoard.h b/include/BitBoard.h index 3d1ffbf..8272086 100644 --- a/include/BitBoard.h +++ b/include/BitBoard.h @@ -74,6 +74,8 @@ class BitBoard void addNewMove(uint32_t start, uint32_t end, vmup &moves) const; std::string pieceToString(int piece) const; + const int count(uint32_t i) const; + public: BitBoard(); BitBoard(uint32_t black, uint32_t white, uint32_t kings, bool isBlacksTurn, bool wasKinged); @@ -83,7 +85,9 @@ class BitBoard uint32_t getWhitePieces() { return m_whitePieces; }; uint32_t getKings () { return m_kings; }; bool getIsBlacksTurn() { return m_isBlacksTurn; }; - bool getWasKinged() { return m_wasKinged; } + bool getWasKinged() { return m_wasKinged; }; + + int getTotalPieceCount(); std::string player() const; vmup actions() const; diff --git a/include/Game.hpp b/include/Game.hpp new file mode 100644 index 0000000..5a62a3f --- /dev/null +++ b/include/Game.hpp @@ -0,0 +1,17 @@ +#ifndef GAME_H +#define GAME_H + +#include "Specimen.hpp" + +class Game { + Specimen black; + Specimen white; + +public: + Game(const Specimen& b, const Specimen& w): + black(b), + white(w) {}; + void play(int *winner); +}; + +#endif diff --git a/include/GeneticSimulation.hpp b/include/GeneticSimulation.hpp new file mode 100644 index 0000000..f85a333 --- /dev/null +++ b/include/GeneticSimulation.hpp @@ -0,0 +1,28 @@ +#ifndef GENETIC_SIMULATION_H +#define GENETIC_SIMULATION_H + +#include +#include +#include "Specimen.hpp" + +class GeneticSimulation { + + int numSpecimen; + std::queue population; + + std::random_device rd; + std::mt19937 gen; + std::uniform_int_distribution dist; + +public: + GeneticSimulation(int s): + numSpecimen(s), + dist(-268435456,268435456), + rd(), + gen(rd()) {}; + void simulate(); + +}; + + +#endif diff --git a/include/Heuristic.hpp b/include/Heuristic.hpp index 2178dfe..fbd21cb 100644 --- a/include/Heuristic.hpp +++ b/include/Heuristic.hpp @@ -5,8 +5,12 @@ class Heuristic { + +public: + const static int numFeatures = 11; +private: // Feature coefficients - int coeff[11] = {1,1,1,1,1,1,1,1,1,1,1}; + int coeff[numFeatures] = {1,1,1,1,1,1,1,1,1,1,1}; // Feature Functions int pawnCount() const; @@ -27,7 +31,7 @@ class Heuristic BitBoard *board; public: - // Heuristic(int *co[10]) { coeff = *co;}; + void set(int index, int value) { coeff[index] = value; }; int evaluate(bup &bitboard); // Needed public for variable depth search diff --git a/include/MinimaxSearch.h b/include/MinimaxSearch.h index b92ffec..526694e 100644 --- a/include/MinimaxSearch.h +++ b/include/MinimaxSearch.h @@ -22,11 +22,11 @@ class MinimaxSearch double alphaBeta(std::unique_ptr &board, double alpha, double beta, int depth); bool shouldStop(std::unique_ptr &board, int depth); - -public: + public: mup minimaxDecision(std::unique_ptr &board); MinimaxSearch(Heuristic& h): heuristic(h){}; MinimaxSearch() {}; + void setHeuristics(Heuristic h) { heuristic = h; }; }; #endif diff --git a/include/Specimen.hpp b/include/Specimen.hpp new file mode 100644 index 0000000..d0225bb --- /dev/null +++ b/include/Specimen.hpp @@ -0,0 +1,19 @@ +#ifndef SPECIMEN_H +#define SPECIMEN_H + +#include "Heuristic.hpp" + +class Specimen{ + Heuristic startgame; + Heuristic endgame; +public: + Specimen(Heuristic start, Heuristic end): + startgame(start), + endgame(end) {}; + + Heuristic getStartHeuristics() { return startgame; }; + Heuristic getEndHeuristics() { return endgame; }; +}; + + +#endif diff --git a/src/BitBoard.cpp b/src/BitBoard.cpp index ec78925..d573a2c 100644 --- a/src/BitBoard.cpp +++ b/src/BitBoard.cpp @@ -262,6 +262,14 @@ bup BitBoard::result(mup &move) const return result; } +const int BitBoard::count(uint32_t i) const{ + return std::bitset<32>(i).count(); +} + +int BitBoard::getTotalPieceCount(){ + return count(m_blackPieces) + count(m_whitePieces); +} + void BitBoard::printState() const { std::cout<<"-"< bestMove = blackSearch.minimaxDecision(board); + + + const int noCaptureLimit = 50; + int noCaptureCount = 0; + int piecesOnBoard = 24; + + while(bestMove != nullptr && !bestMove->isEmpty()){ + // update board + board = board->result(bestMove); + + + if(piecesOnBoard > board->getTotalPieceCount()) + noCaptureCount = 0; + else + noCaptureCount++; + + // draw after some limit of no capture + if(noCaptureCount >= noCaptureLimit) + { + *winner = 3; + break; + } + + piecesOnBoard = board->getTotalPieceCount(); + + // check for endgame + if(!isEndGame) + if(piecesOnBoard > 12){ + isEndGame = true; + blackSearch.setHeuristics(black.getEndHeuristics()); + whiteSearch.setHeuristics(white.getEndHeuristics()); + } + + //get next move + if(board->player().compare("black")) + bestMove = blackSearch.minimaxDecision(board); + else + bestMove = whiteSearch.minimaxDecision(board); + } + + std::cout<< "\n"; + board->printState(); + + // 1 if black wins, 2 if white, 3 if draw + if(noCaptureCount == noCaptureLimit) + *winner = 0; + else if(board->player().compare("black")) + *winner = 1; + else if(board->player().compare("white")) + *winner = 2; + +} diff --git a/src/main.cpp b/src/main.cpp index 353670b..b27254c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,18 +8,12 @@ #include "MinimaxSearch.h" #include "Heuristic.hpp" #include "Client.hpp" +#include "GeneticSimulation.hpp" -int main(int argc, const char * argv[]) { - - if(argc < 3) - { - std::cout << "Please specify args: userID oppID\n"; - std::cout << "E.g. ./build/runner 13 0\n"; - return -1; - } +void serverGame(int userID, int oppID){ // Set up telnet connection - Client c(atoi(argv[1]), atoi(argv[2])); + Client c(userID, oppID); std::unique_ptr board (new BitBoard); Heuristic heur_test; @@ -96,5 +90,27 @@ int main(int argc, const char * argv[]) { std::cout << "=======================================" << std::endl; } +} + +void geneticSimulation(int numSpecimen){ + GeneticSimulation gs(numSpecimen); + gs.simulate(); +} + +int main(int argc, const char * argv[]) { + + if(argc == 1) + { + std::cout << "Please specify args: userID oppID\n"; + std::cout << "E.g. ./build/runner 13 0\n"; + std::cout << "\n\nGenetic Alg simulation:\n"; + std::cout << "./build/runner specimen rounds 1 \n"; + return -1; + } + else if(argc == 3) + serverGame(atoi(argv[1]), atoi(argv[2])); + else + geneticSimulation(atoi(argv[1])); + return 0; }