Merge branch 'master' of https://github.tamu.edu/brj2013/Breakthrough
This commit is contained in:
commit
68f3b9514a
3 changed files with 83 additions and 42 deletions
107
Engine.cpp
107
Engine.cpp
|
@ -10,6 +10,8 @@
|
||||||
Engine::Engine(){
|
Engine::Engine(){
|
||||||
Board* brd = new Board();
|
Board* brd = new Board();
|
||||||
b = brd;
|
b = brd;
|
||||||
|
alpha = INT_MIN;
|
||||||
|
beta = INT_MAX;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::startGame(){
|
void Engine::startGame(){
|
||||||
|
@ -21,19 +23,23 @@ void Engine::startGame(){
|
||||||
|
|
||||||
int choice = -1;
|
int choice = -1;
|
||||||
cin >> choice;
|
cin >> choice;
|
||||||
|
cout<<"Enter AI difficulty: \n";
|
||||||
|
int difficulty;
|
||||||
|
cin >> difficulty;
|
||||||
|
|
||||||
cout << "OK" << endl;
|
cout << "OK" << endl;
|
||||||
|
|
||||||
if (choice == 1)
|
if (choice == 1)
|
||||||
userGame();
|
userGame(difficulty);
|
||||||
else if (choice == 2)
|
else if (choice == 2)
|
||||||
AIGame();
|
AIGame(difficulty);
|
||||||
else {
|
else {
|
||||||
cout << "Please enter a valid choice.\n";
|
cout << "Please enter a valid choice.\n";
|
||||||
startGame();
|
startGame();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::userGame(){
|
void Engine::userGame(int difficulty){
|
||||||
string move;
|
string move;
|
||||||
|
|
||||||
bool gameOver = false;
|
bool gameOver = false;
|
||||||
|
@ -58,40 +64,43 @@ void Engine::userGame(){
|
||||||
}
|
}
|
||||||
|
|
||||||
while(b->getTurn() == 'X' ){
|
while(b->getTurn() == 'X' ){
|
||||||
//easyAI();
|
AI(difficulty);
|
||||||
AI(3);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gameOver = b->isGameOver();
|
gameOver = b->isGameOver();
|
||||||
b->setValidFalse();
|
b->setValidFalse();
|
||||||
|
|
||||||
b->snapshot(record, *b);
|
b->snapshot(record, *b);
|
||||||
}
|
}
|
||||||
|
|
||||||
b->displayBoard();
|
b->displayBoard();
|
||||||
string s = "";
|
string s = "";
|
||||||
s += b->getTurn();
|
s += b->getTurn();
|
||||||
cout << "Game over. " + s + " wins!\n\n";
|
cout << "Game over. " + s + " wins!\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::AIGame(){
|
void Engine::AIGame(int difficulty){
|
||||||
bool gameOver = false;
|
bool gameOver = false;
|
||||||
|
|
||||||
while (gameOver != true){
|
while (gameOver != true){
|
||||||
gameOver = b->isGameOver();
|
gameOver = b->isGameOver();
|
||||||
|
|
||||||
while(b->getTurn() == 'O'){
|
while(b->getTurn() == 'O'){
|
||||||
AI(3);
|
AI(difficulty);
|
||||||
}
|
}
|
||||||
|
|
||||||
b->displayBoard();
|
b->displayBoard();
|
||||||
sleep(1);
|
sleep(1);
|
||||||
|
|
||||||
while(b->getTurn() == 'X' ){
|
while(b->getTurn() == 'X' ){
|
||||||
AI(3);
|
AI(difficulty);
|
||||||
}
|
}
|
||||||
|
|
||||||
gameOver = b->isGameOver();
|
gameOver = b->isGameOver();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string s = "";
|
||||||
|
s += b->getTurn();
|
||||||
|
cout << "Game over. " + s + " wins!\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::AI(int depth){
|
void Engine::AI(int depth){
|
||||||
|
@ -111,43 +120,77 @@ void Engine::AI(int depth){
|
||||||
void Engine::createMMTree(MNode* node, int depth, int alt){
|
void Engine::createMMTree(MNode* node, int depth, int alt){
|
||||||
MNode* temp;
|
MNode* temp;
|
||||||
char max, min;
|
char max, min;
|
||||||
|
bool cond = true;
|
||||||
Board current = node->getState();
|
Board current = node->getState();
|
||||||
vector<moves> listOfMoves = current.viewPossibleMoves();
|
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){
|
if (depth >= 0){
|
||||||
for (int i = 0; i < listOfMoves.size(); ++i){
|
for (int i = 0; i < listOfMoves.size(); ++i){
|
||||||
if(current.getPiece(8 - listOfMoves[i].row, listOfMoves[i].column)->getType() ==
|
if(current.getPiece(8 - listOfMoves[i].row, listOfMoves[i].column)->getType() ==
|
||||||
current.getTurn() && current.isThisMovePossible(8 - listOfMoves[i].row,
|
current.getTurn() && current.isThisMovePossible(8 - listOfMoves[i].row,
|
||||||
listOfMoves[i].column,
|
listOfMoves[i].column,
|
||||||
listOfMoves[i].moveType)){
|
listOfMoves[i].moveType)){
|
||||||
current.resetTaken();
|
if (cond){
|
||||||
current = current.move(listOfMoves[i]);
|
current.resetTaken();
|
||||||
max = current.getTurn();
|
current = current.move(listOfMoves[i]);
|
||||||
current.changeTurns();
|
max = current.getTurn();
|
||||||
min = 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");
|
if (current.checkTaken('X'))
|
||||||
else if (alt == 1)
|
cout << "xtaken true\n";
|
||||||
temp->setType("min");
|
if (current.checkTaken('O'))
|
||||||
|
cout << "otaken true\n";
|
||||||
|
*/
|
||||||
|
|
||||||
current.resetTaken();
|
temp = new MNode(current, listOfMoves[i], current.evaluate(max, min));
|
||||||
node->addChild(temp);
|
|
||||||
|
|
||||||
createMMTree(temp, --depth, alt * -1);
|
if (alt == 1)
|
||||||
|
temp->setType("max");
|
||||||
|
else if (alt == 1)
|
||||||
|
temp->setType("min");
|
||||||
|
|
||||||
current.changeTurns();
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,16 +214,14 @@ moves Engine::evaluateMMTree(MNode* node){
|
||||||
}
|
}
|
||||||
|
|
||||||
int Engine::evaluateMMBranch(MNode* node, int max){
|
int Engine::evaluateMMBranch(MNode* node, int max){
|
||||||
if (node->getMMVal() > max)
|
|
||||||
max = node->getMMVal();
|
|
||||||
|
|
||||||
vector<MNode*> children = node->getChildren();
|
vector<MNode*> children = node->getChildren();
|
||||||
|
|
||||||
for (auto &c : children){
|
for (auto &c : children){
|
||||||
evaluateMMBranch(c, max);
|
evaluateMMBranch(c, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
//cout << "max: " << max << "\n";
|
if (node->getMMVal() > max)
|
||||||
|
max = node->getMMVal();
|
||||||
|
|
||||||
return max;
|
return max;
|
||||||
}
|
}
|
||||||
|
|
6
Engine.h
6
Engine.h
|
@ -7,14 +7,14 @@ using namespace std;
|
||||||
|
|
||||||
class Engine {
|
class Engine {
|
||||||
Board* b;
|
Board* b;
|
||||||
|
int alpha, beta;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Engine();
|
Engine();
|
||||||
Board* getBoard() { return b; }
|
Board* getBoard() { return b; }
|
||||||
void startGame();
|
void startGame();
|
||||||
void userGame();
|
void userGame(int difficulty);
|
||||||
void AIGame();
|
void AIGame(int difficulty);
|
||||||
void AI(int depth);
|
void AI(int depth);
|
||||||
void createMMTree(MNode* node, int depth, int alt);
|
void createMMTree(MNode* node, int depth, int alt);
|
||||||
moves evaluateMMTree(MNode* node);
|
moves evaluateMMTree(MNode* node);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
# Breakthrough
|
# Breakthrough
|
||||||
Reposity for the second CSCE 315 project
|
Repository for the second CSCE 315 project
|
||||||
|
|
||||||
# To compile everything:
|
# To compile everything:
|
||||||
make
|
make
|
||||||
|
|
Reference in a new issue