2015-10-25 16:43:27 -05:00
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#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()){
|
2015-10-27 17:30:33 -05:00
|
|
|
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 08:58:32 -05:00
|
|
|
easyAI();
|
|
|
|
//AI(3);
|
2015-10-25 16:43:27 -05:00
|
|
|
}
|
|
|
|
|
2015-10-25 16:53:32 -05:00
|
|
|
gameOver = b->isGameOver();
|
2015-10-27 17:30:33 -05:00
|
|
|
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-29 09:49:01 -05:00
|
|
|
b->displayBoard();
|
|
|
|
string s = "";
|
|
|
|
s += b->getTurn();
|
|
|
|
cout << "Game over. " + s + " wins!\n\n";
|
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-27 21:19:24 -05:00
|
|
|
Board* temp = new Board(*b);
|
2015-10-29 08:58:32 -05:00
|
|
|
moves empty;
|
|
|
|
MNode* root = new MNode(*temp, empty, 0);
|
2015-10-28 17:12:44 -05:00
|
|
|
|
2015-10-26 17:08:48 -05:00
|
|
|
//only doing 1 branch right now because testing
|
|
|
|
/*for (int i = 0; i < listOfMoves.size(); ++i){
|
2015-10-27 17:30:33 -05:00
|
|
|
minMax(b, listOfMoves[i]);
|
2015-10-26 17:08:48 -05:00
|
|
|
}*/
|
2015-10-27 10:40:55 -05:00
|
|
|
|
2015-10-28 17:12:44 -05:00
|
|
|
//remove this soon
|
2015-10-29 08:58:32 -05:00
|
|
|
//b->move(minMax(temp, listOfMoves[0], 0, 0));
|
2015-10-28 17:12:44 -05:00
|
|
|
|
2015-10-29 08:58:32 -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 08:58:32 -05:00
|
|
|
moves Engine::minMax(MNode* node, int depth){
|
|
|
|
Board current = node->getState();
|
|
|
|
vector<moves> listOfMoves = current.viewPossibleMoves();
|
|
|
|
/*
|
|
|
|
if (current.isGameOver() == true){
|
2015-10-28 17:12:44 -05:00
|
|
|
cout << "END OF PATH REACHED\n\n";
|
2015-10-27 08:38:25 -05:00
|
|
|
return m;
|
2015-10-26 17:08:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
2015-10-29 08:58:32 -05:00
|
|
|
if(current.getPiece(8 - m.row, m.column)->getType() == current.getTurn() && current.isThisMovePossible(8 - m.row, m.column, m.moveType)){
|
2015-10-27 21:19:24 -05:00
|
|
|
temp->move(m);
|
2015-10-28 15:03:49 -05:00
|
|
|
temp->evaluate('X', 'O');
|
2015-10-27 21:19:24 -05:00
|
|
|
temp->changeTurns();
|
|
|
|
}
|
|
|
|
else {
|
2015-10-27 21:48:04 -05:00
|
|
|
m = minMax(temp, listOfMoves[++r], c, r);
|
2015-10-27 08:38:25 -05:00
|
|
|
}
|
2015-10-27 10:40:55 -05:00
|
|
|
|
2015-10-27 21:48:04 -05:00
|
|
|
vector<moves> listOfMoves = temp->viewPossibleMoves();
|
2015-10-27 23:22:08 -05:00
|
|
|
minMax(temp, listOfMoves[0], 0, 0);
|
2015-10-27 10:40:55 -05:00
|
|
|
return m;
|
2015-10-27 21:19:24 -05:00
|
|
|
}
|
2015-10-29 08:58:32 -05:00
|
|
|
*/
|
|
|
|
}
|