This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
breakthroughpine64backup/Engine.cpp

187 lines
3.6 KiB
C++
Raw Normal View History

2015-10-29 22:52:18 -05:00
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
2015-10-29 13:54:16 -05:00
#include <limits.h>
#include <time.h>
#include <unistd.h>
2015-10-29 22:52:18 -05:00
#include "Engine.h"
Engine::Engine(){
Board* brd = new Board();
b = brd;
}
void Engine::startGame(){
cout<<"WELCOME\n";
cout<<"1. Play against AI?\n";
cout<<"2. Watch AI vs AI?\n";
2015-10-29 22:52:18 -05:00
cout<<"Enter choice: \n";
int choice = -1;
cin >> choice;
cout << "OK" << endl;
if (choice == 1)
userGame();
else if (choice == 2)
AIGame();
else {
cout << "Please enter a valid choice.\n";
startGame();
}
}
void Engine::userGame(){
2015-10-29 22:52:18 -05:00
string move;
bool gameOver = false;
vector<Board> record;
b->snapshot(record, *b);
while (gameOver != true){
gameOver = b->isGameOver();
while(b->getTurn() == 'O' && !b->isValid()){
b->displayBoard();
cout<<"\nEnter command: ";
cin>>move;
cout << "\n";
b->interpret(move, *b);
if(b->isValid()){
b->changeTurns();
b->resetTaken();
2015-10-29 22:52:18 -05:00
b->setValidFalse();
}
}
while(b->getTurn() == 'X' ){
2015-10-29 13:06:15 -05:00
//easyAI();
AI(3);
2015-10-29 22:52:18 -05:00
}
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(){
bool gameOver = false;
while (gameOver != true){
gameOver = b->isGameOver();
while(b->getTurn() == 'O'){
AI(3);
}
b->displayBoard();
sleep(1);
while(b->getTurn() == 'X' ){
AI(3);
}
gameOver = b->isGameOver();
}
2015-10-29 22:52:18 -05:00
}
void Engine::AI(int depth){
2015-10-29 13:54:16 -05:00
Board* state = new Board(*b);
moves m;
MNode* root = new MNode(*state, m, 0);
createMMTree(root, depth, 1);
2015-10-29 13:54:16 -05:00
m = evaluateMMTree(root);
2015-10-29 22:52:18 -05:00
2015-11-02 16:42:47 -06:00
//printTree(0, root);
2015-10-29 13:06:15 -05:00
2015-10-30 11:56:44 -05:00
b->move(m);
2015-10-29 13:06:15 -05:00
b->changeTurns();
b->resetTaken();
2015-10-29 22:52:18 -05:00
}
void Engine::createMMTree(MNode* node, int depth, int alt){
2015-10-29 13:54:16 -05:00
MNode* temp;
char max, min;
2015-10-29 22:52:18 -05:00
Board current = node->getState();
vector<moves> listOfMoves = current.viewPossibleMoves();
2015-10-29 13:54:16 -05:00
2015-10-29 13:06:15 -05:00
if (depth >= 0){
2015-10-29 13:54:16 -05:00
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();
2015-10-29 13:54:16 -05:00
current = current.move(listOfMoves[i]);
max = current.getTurn();
2015-10-29 13:54:16 -05:00
current.changeTurns();
min = current.getTurn();
2015-11-02 16:42:47 -06:00
/*
if (current.checkTaken('X'))
cout << "xtaken true\n";
if (current.checkTaken('O'))
cout << "otaken true\n";
2015-11-02 16:42:47 -06:00
*/
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();
2015-10-29 13:54:16 -05:00
node->addChild(temp);
createMMTree(temp, --depth, alt * -1);
2015-10-29 13:54:16 -05:00
current.changeTurns();
}
2015-10-29 13:06:15 -05:00
}
2015-10-29 22:52:18 -05:00
}
2015-10-29 13:54:16 -05:00
return;
2015-10-29 08:58:32 -05:00
}
2015-10-29 13:06:15 -05:00
moves Engine::evaluateMMTree(MNode* node){
//returns the move from children that maximizes evaluate()
2015-10-29 13:54:16 -05:00
MNode* temp;
2015-10-29 13:06:15 -05:00
moves m;
2015-10-29 13:54:16 -05:00
int max = INT_MIN;
int val;
2015-10-29 13:06:15 -05:00
vector<MNode*> children = node->getChildren();
for (auto &c : children){
val = evaluateMMBranch(c, INT_MIN);
2015-10-29 13:54:16 -05:00
if(val > max){
temp = c;
max = val;
2015-10-29 22:52:18 -05:00
}
}
2015-10-29 13:54:16 -05:00
return temp->getMove();
2015-10-29 22:52:18 -05:00
}
2015-10-29 13:54:16 -05:00
int Engine::evaluateMMBranch(MNode* node, int max){
if (node->getMMVal() > max)
max = node->getMMVal();
2015-10-29 13:06:15 -05:00
vector<MNode*> children = node->getChildren();
for (auto &c : children){
evaluateMMBranch(c, max);
2015-10-29 13:06:15 -05:00
}
//cout << "max: " << max << "\n";
2015-10-29 13:06:15 -05:00
return max;
2015-11-02 16:21:38 -06:00
}