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
Alexander Huddleston 37bbd43b84 Updating Branches.
2015-10-28 15:19:53 -05:00

144 lines
3.4 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();
}
}
//if(b->isValid()) cout << b->getTurn();
while(b->getTurn() == 'X' )
{
AI();
}
gameOver = b->isGameOver();
b->setValidFalse();
b->snapshot(record, *b);
}
}
void Engine::easyAI()
{
vector<moves> listOfMoves = b->viewPossibleMoves();
/*
for(int x = 0; x < listOfMoves.size(); ++x) {
cout << listOfMoves[x].row << " " << listOfMoves[x].column << " " << listOfMoves[x].moveType << "\n\n";
}
*/
srand(time(NULL));
int randomChoice = rand() % (listOfMoves.size()-1) - 0;
//int temp = randomChoice;
cout << "easy AI move: " << listOfMoves[randomChoice].row << listOfMoves[randomChoice].column << listOfMoves[randomChoice].moveType << "\n";
b->move(listOfMoves[randomChoice]);
b->changeTurns();
}
void Engine::AI(){
//cout << "----------------------BEGIN AI FUNCTION----------------------\n";
vector<moves> listOfMoves = b->viewPossibleMoves();
Board* temp = new Board(*b);
//probably not needed, check later
/*
if (b->getTurn() != 'X'){
cout << "a changing of turns is needed. \n";
b->changeTurns();
}
*/
//only doing 1 branch right now because testing
/*for (int i = 0; i < listOfMoves.size(); ++i){
minMax(b, listOfMoves[i]);
}*/
b->move(minMax(temp, listOfMoves[0], 0, 0));
b->changeTurns();
//verification of correct turn
/*
if (b->getTurn() != 'O'){
cout << "ERROR in Engine::AI: b is on the wrong turn. \n";
}
*/
b->displayBoard();
//cout << "----------------------END AI FUNCTION----------------------\n";
}
moves Engine::minMax(Board* temp, moves m, int c, int r){
//testing purposes only, c = finite depth
//cout << "c: " << c << "\n\n";
//cout << "current turn: " << temp->getTurn() << "\n";
vector<moves> listOfMoves = temp->viewPossibleMoves();
//cout << "listOfMoves size: " << listOfMoves.size() << "\n\n";
/*
if (c > 5){
return m;
}
*/
if (temp->isGameOver() == true){
//cout << "END OF PATH REACHED\n";
return m;
}
else {
if(temp->getPiece(8 - m.row, m.column)->getType() == temp->getTurn() && temp->isThisMovePossible(8 - m.row, m.column, m.moveType)){
//cout << "piece has been moved in minMax\n";
temp->move(m);
temp->evaluate('X', 'O');
temp->changeTurns();
//temp->displayBoard();
}
else {
//cout << m.row << " " << m.column << "\n\n";
m = minMax(temp, listOfMoves[++r], c, r);
}
//temp->displayBoard();
vector<moves> listOfMoves = temp->viewPossibleMoves();
//cout << "listOfMoves size: " << listOfMoves.size() << "\n\n";
minMax(temp, listOfMoves[0], 0, 0);
//testing
return m;
}
}