113 lines
2.3 KiB
C++
113 lines
2.3 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);
|
||
|
}
|
||
|
b->displayBoard();
|
||
|
string s = "";
|
||
|
s += b->getTurn();
|
||
|
cout << "Game over. " + s + " wins!\n\n";
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
|
||
|
//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));
|
||
|
|
||
|
b->changeTurns();
|
||
|
b->displayBoard();
|
||
|
}
|
||
|
|
||
|
moves Engine::minMax(MNode* node, int depth){
|
||
|
Board current = node->getState();
|
||
|
vector<moves> listOfMoves = current.viewPossibleMoves();
|
||
|
/*
|
||
|
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;
|
||
|
}
|
||
|
*/
|
||
|
}
|