-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
177 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#ifndef GENETIC_SIMULATION_H | ||
#define GENETIC_SIMULATION_H | ||
|
||
#include <random> | ||
#include <queue> | ||
#include "Specimen.hpp" | ||
|
||
class GeneticSimulation { | ||
|
||
int numSpecimen; | ||
std::queue<Specimen> population; | ||
|
||
std::random_device rd; | ||
std::mt19937 gen; | ||
std::uniform_int_distribution<int> dist; | ||
|
||
public: | ||
GeneticSimulation(int s): | ||
numSpecimen(s), | ||
dist(-268435456,268435456), | ||
rd(), | ||
gen(rd()) {}; | ||
void simulate(); | ||
|
||
}; | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
|
||
#include "MinimaxSearch.h" | ||
#include "Game.hpp" | ||
|
||
void Game::play(int *winner){ | ||
bup board(new BitBoard()); | ||
|
||
MinimaxSearch blackSearch; | ||
blackSearch.setHeuristics(black.getStartHeuristics()); | ||
MinimaxSearch whiteSearch; | ||
whiteSearch.setHeuristics(white.getEndHeuristics()); | ||
|
||
bool isEndGame = false; | ||
|
||
std::unique_ptr<Move> 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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters