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

140 lines
2.9 KiB
C++
Raw Normal View History

2015-10-25 16:43:27 -05:00
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
2015-10-29 13:54:16 -05:00
#include <limits.h>
2015-10-25 16:43:27 -05:00
#include "Engine.h"
Engine::Engine(){
2015-10-25 16:53:32 -05:00
Board* brd = new Board();
b = brd;
2015-10-25 16:43:27 -05:00
}
void Engine::startGame(){
cout<<"WELCOME\n";
cout<<"1. Play against AI?\n";
cout<<"2. Play against a human?\n";
cout<<"Enter choice: \n";
2015-10-26 15:22:59 -05:00
int choice = -1;
2015-10-25 16:43:27 -05:00
cin >> choice;
cout << "OK" << endl;
string move;
bool gameOver = false;
vector<Board> record;
2015-10-25 16:53:32 -05:00
b->snapshot(record, *b);
2015-10-25 16:43:27 -05:00
2015-10-28 17:12:44 -05:00
while (gameOver != true){
2015-10-25 16:53:32 -05:00
gameOver = b->isGameOver();
2015-10-25 16:43:27 -05:00
2015-10-28 17:12:44 -05:00
while(b->getTurn() == 'O' && !b->isValid()){
2015-10-25 16:53:32 -05:00
b->displayBoard();
2015-10-25 16:43:27 -05:00
cout<<"\nEnter command: ";
cin>>move;
2015-10-27 10:40:55 -05:00
cout << "\n";
2015-10-25 16:53:32 -05:00
b->interpret(move, *b);
2015-10-28 17:12:44 -05:00
if(b->isValid()){
b->changeTurns();
b->setValidFalse();
}
2015-10-25 16:43:27 -05:00
}
2015-10-27 10:40:55 -05:00
2015-10-28 17:12:44 -05:00
while(b->getTurn() == 'X' ){
2015-10-29 13:06:15 -05:00
//easyAI();
AI(3);
2015-10-25 16:43:27 -05:00
}
2015-10-25 16:53:32 -05:00
gameOver = b->isGameOver();
b->setValidFalse();
2015-10-25 16:43:27 -05:00
2015-10-25 16:53:32 -05:00
b->snapshot(record, *b);
2015-10-25 16:43:27 -05:00
}
}
2015-10-28 17:12:44 -05:00
void Engine::easyAI(){
2015-10-25 16:53:32 -05:00
vector<moves> listOfMoves = b->viewPossibleMoves();
2015-10-28 17:12:44 -05:00
2015-10-27 11:31:44 -05:00
srand(time(NULL));
2015-10-27 11:34:20 -05:00
int randomChoice = rand() % (listOfMoves.size()-1) - 0;
2015-10-27 11:31:44 -05:00
b->move(listOfMoves[randomChoice]);
b->changeTurns();
2015-10-25 16:43:27 -05:00
}
2015-10-28 17:12:44 -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);
m = evaluateMMTree(root);
2015-10-30 11:56:44 -05:00
2015-10-29 13:54:16 -05:00
/*
printTree(0, root);
cout << "\n";
2015-10-30 11:56:44 -05:00
cout << "AI move: (" << m.row << ", " << m.column << "): " << m.moveType << "\n";
2015-10-29 13:54:16 -05:00
*/
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();
2015-10-27 08:38:25 -05:00
b->displayBoard();
2015-10-25 16:43:27 -05:00
}
2015-10-29 13:54:16 -05:00
void Engine::createMMTree(MNode* node, int depth){
MNode* temp;
2015-10-29 08:58:32 -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 = current.move(listOfMoves[i]);
current.changeTurns();
temp = new MNode(current, listOfMoves[i], current.evaluate('X'));
node->addChild(temp);
createMMTree(temp, --depth);
current.changeTurns();
}
2015-10-29 13:06:15 -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){
2015-10-29 13:54:16 -05:00
val = evaluateMMBranch(c, 0);
if(val > max){
temp = c;
max = val;
}
2015-10-29 13:06:15 -05:00
}
2015-10-29 13:54:16 -05:00
return temp->getMove();
2015-10-29 13:06:15 -05:00
}
2015-10-29 13:54:16 -05:00
2015-10-29 13:06:15 -05:00
//return sum of eval values
//pass in each child of root and 0
2015-10-29 13:54:16 -05:00
int Engine::evaluateMMBranch(MNode* node, int sum){
2015-10-29 13:06:15 -05:00
sum += node->getMMVal();
vector<MNode*> children = node->getChildren();
for (auto &c : children){
sum += evaluateMMBranch(c, sum);
}
return sum;
2015-10-29 13:54:16 -05:00
}