This commit is contained in:
Alexander Huddleston 2015-11-02 17:53:00 -06:00
commit 68f3b9514a
3 changed files with 83 additions and 42 deletions

View file

@ -10,6 +10,8 @@
Engine::Engine(){
Board* brd = new Board();
b = brd;
alpha = INT_MIN;
beta = INT_MAX;
}
void Engine::startGame(){
@ -21,19 +23,23 @@ void Engine::startGame(){
int choice = -1;
cin >> choice;
cout<<"Enter AI difficulty: \n";
int difficulty;
cin >> difficulty;
cout << "OK" << endl;
if (choice == 1)
userGame();
userGame(difficulty);
else if (choice == 2)
AIGame();
AIGame(difficulty);
else {
cout << "Please enter a valid choice.\n";
startGame();
}
}
void Engine::userGame(){
void Engine::userGame(int difficulty){
string move;
bool gameOver = false;
@ -58,40 +64,43 @@ void Engine::userGame(){
}
while(b->getTurn() == 'X' ){
//easyAI();
AI(3);
AI(difficulty);
}
gameOver = b->isGameOver();
b->setValidFalse();
b->snapshot(record, *b);
}
b->displayBoard();
string s = "";
s += b->getTurn();
cout << "Game over. " + s + " wins!\n\n";
}
void Engine::AIGame(){
void Engine::AIGame(int difficulty){
bool gameOver = false;
while (gameOver != true){
gameOver = b->isGameOver();
while(b->getTurn() == 'O'){
AI(3);
AI(difficulty);
}
b->displayBoard();
sleep(1);
while(b->getTurn() == 'X' ){
AI(3);
AI(difficulty);
}
gameOver = b->isGameOver();
}
string s = "";
s += b->getTurn();
cout << "Game over. " + s + " wins!\n\n";
}
void Engine::AI(int depth){
@ -111,42 +120,76 @@ void Engine::AI(int depth){
void Engine::createMMTree(MNode* node, int depth, int alt){
MNode* temp;
char max, min;
bool cond = true;
Board current = node->getState();
vector<moves> listOfMoves = current.viewPossibleMoves();
if (node->getType() == "max"){
if (node->getMMVal() <= alpha){
cond = false;
}
}
else if (node->getType() == "min"){
if (node->getMMVal() >= beta){
cond = false;
}
}
if (depth >= 0){
for (int i = 0; i < listOfMoves.size(); ++i){
if(current.getPiece(8 - listOfMoves[i].row, listOfMoves[i].column)->getType() ==
current.getTurn() && current.isThisMovePossible(8 - listOfMoves[i].row,
listOfMoves[i].column,
listOfMoves[i].moveType)){
current.resetTaken();
current = current.move(listOfMoves[i]);
max = current.getTurn();
current.changeTurns();
min = current.getTurn();
/*
if (current.checkTaken('X'))
cout << "xtaken true\n";
if (current.checkTaken('O'))
cout << "otaken true\n";
*/
temp = new MNode(current, listOfMoves[i], current.evaluate(max, min));
if (alt == 1)
temp->setType("max");
else if (alt == 1)
temp->setType("min");
current.resetTaken();
node->addChild(temp);
createMMTree(temp, --depth, alt * -1);
current.changeTurns();
if (cond){
current.resetTaken();
current = current.move(listOfMoves[i]);
max = current.getTurn();
current.changeTurns();
min = current.getTurn();
/*
if (current.checkTaken('X'))
cout << "xtaken true\n";
if (current.checkTaken('O'))
cout << "otaken true\n";
*/
temp = new MNode(current, listOfMoves[i], current.evaluate(max, min));
if (alt == 1)
temp->setType("max");
else if (alt == 1)
temp->setType("min");
current.resetTaken();
node->addChild(temp);
createMMTree(temp, --depth, alt * -1);
current.changeTurns();
}
}
}
}
else {
if (node->getType() == "max"){
if (node->getMMVal() > alpha){
alpha = node->getMMVal();
}
}
else if (node->getType() == "min"){
if (node->getMMVal() < beta){
beta = node->getMMVal();
}
}
}
//cout << "alpha: " << alpha << "\n";
//cout << "beta: " << beta << "\n";
return;
}
@ -171,16 +214,14 @@ moves Engine::evaluateMMTree(MNode* node){
}
int Engine::evaluateMMBranch(MNode* node, int max){
if (node->getMMVal() > max)
max = node->getMMVal();
vector<MNode*> children = node->getChildren();
for (auto &c : children){
evaluateMMBranch(c, max);
}
//cout << "max: " << max << "\n";
if (node->getMMVal() > max)
max = node->getMMVal();
return max;
}

View file

@ -7,14 +7,14 @@ using namespace std;
class Engine {
Board* b;
int alpha, beta;
public:
Engine();
Board* getBoard() { return b; }
void startGame();
void userGame();
void AIGame();
void userGame(int difficulty);
void AIGame(int difficulty);
void AI(int depth);
void createMMTree(MNode* node, int depth, int alt);
moves evaluateMMTree(MNode* node);

View file

@ -1,5 +1,5 @@
# Breakthrough
Reposity for the second CSCE 315 project
Repository for the second CSCE 315 project
# To compile everything:
make