150 lines
No EOL
3.1 KiB
C++
150 lines
No EOL
3.1 KiB
C++
#include <iostream>
|
|
#include <vector>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#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. Play against a human?\n";
|
|
cout<<"Enter choice: \n";
|
|
|
|
int choice = -1;
|
|
cin >> choice;
|
|
cout << "OK" << endl;
|
|
|
|
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->setValidFalse();
|
|
}
|
|
}
|
|
|
|
while(b->getTurn() == 'X' ){
|
|
//easyAI();
|
|
AI(3);
|
|
}
|
|
|
|
gameOver = b->isGameOver();
|
|
b->setValidFalse();
|
|
|
|
b->snapshot(record, *b);
|
|
}
|
|
}
|
|
|
|
void Engine::easyAI(){
|
|
vector<moves> listOfMoves = b->viewPossibleMoves();
|
|
|
|
srand(time(NULL));
|
|
int randomChoice = rand() % (listOfMoves.size()-1) - 0;
|
|
|
|
b->move(listOfMoves[randomChoice]);
|
|
b->changeTurns();
|
|
}
|
|
|
|
void Engine::AI(int depth){
|
|
Board* temp = new Board(*b);
|
|
moves empty;
|
|
MNode* root = new MNode(*temp, empty, 0);
|
|
root = createMMTree(root, depth);
|
|
//printTree(0, root); CURRENTLY PRODUCES SEG FAULT
|
|
|
|
//only doing 1 branch right now because testing
|
|
/*for (int i = 0; i < listOfMoves.size(); ++i){
|
|
minMax(b, listOfMoves[i]);
|
|
}*/
|
|
|
|
//remove this soon
|
|
//b->move(minMax(temp, listOfMoves[0], 0, 0;
|
|
|
|
vector<moves> listOfMoves = b->viewPossibleMoves();
|
|
b->move(listOfMoves[0]);
|
|
|
|
b->changeTurns();
|
|
b->displayBoard();
|
|
}
|
|
|
|
MNode* Engine::createMMTree(MNode* node, int depth){
|
|
Board current = node->getState();
|
|
vector<moves> listOfMoves = current.viewPossibleMoves();
|
|
vector<MNode*> children = node->getChildren();
|
|
|
|
if (depth >= 0){
|
|
for (int i = 0; i < children.size(); ++i){
|
|
cout << "i: " << i << "\n";
|
|
node->addChild(createMMTree(children[i], --depth));
|
|
}
|
|
}
|
|
|
|
else return node;
|
|
|
|
/*
|
|
if (current.isGameOver() == true){
|
|
cout << "END OF PATH REACHED\n\n";
|
|
return m;
|
|
}
|
|
|
|
else {
|
|
if(current.getPiece(8 - m.row, m.column)->getType() == current.getTurn() && current.isThisMovePossible(8 - m.row, m.column, m.moveType)){
|
|
temp->move(m);
|
|
temp->evaluate('X', 'O');
|
|
temp->changeTurns();
|
|
}
|
|
else {
|
|
m = minMax(temp, listOfMoves[++r], c, r);
|
|
}
|
|
|
|
vector<moves> listOfMoves = temp->viewPossibleMoves();
|
|
minMax(temp, listOfMoves[0], 0, 0);
|
|
return m;
|
|
}
|
|
*/
|
|
}
|
|
|
|
moves Engine::evaluateMMTree(MNode* node){
|
|
//returns the move from children that maximizes evaluate()
|
|
moves m;
|
|
vector<MNode*> children = node->getChildren();
|
|
|
|
for (auto &c : children){
|
|
cout << "c: " << c->getMMVal();
|
|
}
|
|
|
|
return m;
|
|
}
|
|
/*
|
|
//return sum of eval values
|
|
//pass in each child of root and 0
|
|
int Engine::evaluateMMBranch(Node* node, int sum){
|
|
sum += node->getMMVal();
|
|
vector<MNode*> children = node->getChildren();
|
|
|
|
for (auto &c : children){
|
|
sum += evaluateMMBranch(c, sum);
|
|
}
|
|
|
|
return sum;
|
|
}
|
|
*/ |