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
|
|
|
|
|
|
|
while (gameOver != true)
|
|
|
|
{
|
2015-10-25 16:53:32 -05:00
|
|
|
gameOver = b->isGameOver();
|
2015-10-25 16:43:27 -05:00
|
|
|
|
2015-10-25 16:53:32 -05:00
|
|
|
while(b->getTurn() == 'O' )
|
2015-10-25 16:43:27 -05:00
|
|
|
{
|
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-27 11:31:44 -05:00
|
|
|
b->changeTurns();
|
2015-10-25 16:43:27 -05:00
|
|
|
}
|
2015-10-27 10:40:55 -05:00
|
|
|
|
2015-10-27 13:48:45 -05:00
|
|
|
cout << "TEST\n\n";
|
|
|
|
|
2015-10-26 15:22:59 -05:00
|
|
|
while(b->getTurn() == 'X' )
|
2015-10-25 16:43:27 -05:00
|
|
|
{
|
2015-10-27 12:55:23 -05:00
|
|
|
AI();
|
2015-10-25 16:43:27 -05:00
|
|
|
}
|
|
|
|
|
2015-10-25 16:53:32 -05:00
|
|
|
gameOver = b->isGameOver();
|
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-25 16:53:32 -05:00
|
|
|
void Engine::easyAI()
|
2015-10-25 16:43:27 -05:00
|
|
|
{
|
2015-10-25 16:53:32 -05:00
|
|
|
vector<moves> listOfMoves = b->viewPossibleMoves();
|
2015-10-25 16:43:27 -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
|
|
|
|
|
|
|
int temp = randomChoice;
|
2015-10-27 14:48:03 -05:00
|
|
|
cout << "easy AI move: " << listOfMoves[randomChoice].row << listOfMoves[randomChoice].column << listOfMoves[randomChoice].moveType << "\n";
|
2015-10-27 11:31:44 -05:00
|
|
|
b->move(listOfMoves[randomChoice]);
|
|
|
|
b->changeTurns();
|
2015-10-25 16:43:27 -05:00
|
|
|
}
|
|
|
|
|
2015-10-25 16:53:32 -05:00
|
|
|
void Engine::AI(){
|
2015-10-26 17:08:48 -05:00
|
|
|
cout << "----------------------BEGIN AI FUNCTION----------------------\n";
|
2015-10-26 15:22:59 -05:00
|
|
|
vector<moves> listOfMoves = b->viewPossibleMoves();
|
2015-10-27 12:55:23 -05:00
|
|
|
Board* temp = new Board(*b);
|
2015-10-27 08:38:25 -05:00
|
|
|
|
|
|
|
//probably not needed, check later
|
2015-10-27 12:55:23 -05:00
|
|
|
if (temp->getTurn() != 'X'){
|
2015-10-27 08:38:25 -05:00
|
|
|
cout << "a changing of turns is needed. \n";
|
2015-10-27 12:55:23 -05:00
|
|
|
temp->changeTurns();
|
2015-10-26 17:08:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//only doing 1 branch right now because testing
|
|
|
|
/*for (int i = 0; i < listOfMoves.size(); ++i){
|
|
|
|
minMax(temp, listOfMoves[i]);
|
|
|
|
}*/
|
2015-10-27 10:40:55 -05:00
|
|
|
|
2015-10-27 12:55:23 -05:00
|
|
|
b->move(minMax(temp, listOfMoves[0], 0));
|
|
|
|
b->changeTurns();
|
2015-10-27 10:40:55 -05:00
|
|
|
|
|
|
|
//verification of correct turn
|
|
|
|
if (b->getTurn() != 'O'){
|
|
|
|
cout << "ERROR in Engine::AI: b is on the wrong turn. \n";
|
|
|
|
}
|
|
|
|
|
2015-10-27 08:38:25 -05:00
|
|
|
b->displayBoard();
|
2015-10-26 17:08:48 -05:00
|
|
|
cout << "----------------------END AI FUNCTION----------------------\n";
|
2015-10-25 16:43:27 -05:00
|
|
|
}
|
|
|
|
|
2015-10-27 12:55:23 -05:00
|
|
|
moves Engine::minMax(Board* temp, moves m, int c){
|
2015-10-27 14:48:03 -05:00
|
|
|
//testing purposes only, c = finite depth
|
2015-10-27 12:55:23 -05:00
|
|
|
if (c > 5){
|
2015-10-27 08:38:25 -05:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2015-10-27 12:55:23 -05:00
|
|
|
if (temp->isGameOver() == true){
|
2015-10-26 17:08:48 -05:00
|
|
|
cout << "END OF PATH REACHED\n";
|
2015-10-27 08:38:25 -05:00
|
|
|
return m;
|
2015-10-26 17:08:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
2015-10-27 12:55:23 -05:00
|
|
|
if(temp->isThisMovePossible(8 - m.row, temp->charToIntColumn(m.column), m.moveType)){
|
2015-10-27 08:38:25 -05:00
|
|
|
cout << "piece has been moved in minMax\n";
|
2015-10-27 12:55:23 -05:00
|
|
|
temp->move(m);
|
|
|
|
temp->changeTurns();
|
2015-10-27 08:38:25 -05:00
|
|
|
}
|
|
|
|
cout << "c: " << c << "\n\n";
|
2015-10-27 12:55:23 -05:00
|
|
|
cout << "current turn: " << temp->getTurn() << "\n";
|
|
|
|
vector<moves> listOfMoves = temp->viewPossibleMoves();
|
2015-10-27 08:38:25 -05:00
|
|
|
|
|
|
|
for (int i = 0; i < listOfMoves.size(); ++i){
|
2015-10-27 10:40:55 -05:00
|
|
|
//return minMax(temp, listOfMoves[i]);
|
2015-10-27 08:38:25 -05:00
|
|
|
}
|
2015-10-27 10:40:55 -05:00
|
|
|
|
2015-10-27 12:55:23 -05:00
|
|
|
temp->displayBoard();
|
2015-10-27 10:40:55 -05:00
|
|
|
//limited recursion
|
2015-10-27 08:38:25 -05:00
|
|
|
return minMax(temp, listOfMoves[0], ++c);
|
2015-10-27 10:40:55 -05:00
|
|
|
|
|
|
|
//testing
|
|
|
|
return m;
|
2015-10-26 17:08:48 -05:00
|
|
|
}
|
2015-10-25 16:43:27 -05:00
|
|
|
}
|