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
2015-10-26 17:08:48 -05:00

125 lines
No EOL
2.6 KiB
C++
Executable file

#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<<"CHANGE THIS TO PARSE THINGS\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->displayBoard();
cout<<"\nEnter command: ";
cin>>move;
b->interpret(move, *b);
}
while(b->getTurn() == 'X' )
{
AI();
}
gameOver = b->isGameOver();
b->snapshot(record, *b);
}
//for debugging purposes
cout<<"Record:"<<endl;
cout<<record.size();
for (int i = 0; i < record.size(); ++i)
{
record[i].displayBoard();
}
}
void Engine::easyAI()
{
//1) see all possible movements
vector<moves> listOfMoves = b->viewPossibleMoves();
//obvious moves
if (false){
b->changeTurns();
}
//random
else {
srand(time(NULL));
int randomChoice = rand() % (listOfMoves.size()-1) - 0; // choose a move betwen listOfMoves[0] to last element
//3) execute movement
int temp = randomChoice;
b->move(listOfMoves[randomChoice]);
}
}
void Engine::AI(){
cout << "----------------------BEGIN AI FUNCTION----------------------\n";
vector<moves> listOfMoves = b->viewPossibleMoves();
Board* temp = b;
if (temp->getTurn() != 'X'){
temp->changeTurns();
}
//temp->changeTurns();
//cout << "after: " << temp->getTurn() << "\n";
//only doing 1 branch right now because testing
/*for (int i = 0; i < listOfMoves.size(); ++i){
minMax(temp, listOfMoves[i]);
}*/
minMax(temp, listOfMoves[0]);
b->move(listOfMoves[0]);
cout << "----------------------END AI FUNCTION----------------------\n";
}
void Engine::minMax(Board* temp, moves m){
if (temp->isGameOver() == true){
cout << "END OF PATH REACHED\n";
return;
}
else {
cout << "m.row: " << m.row << "\n";
cout << "charToIntColumn(m.column): " << temp->charToIntColumn(m.column) << "\n";
cout << "m.moveType: " << m.moveType << "\n";
cout << "temp->isThisMovePossible(stuff): " << temp->isThisMovePossible(m.row, temp->charToIntColumn(m.column), m.moveType) << "\n\n\n";
vector<moves> listOfMoves = temp->viewPossibleMoves();
/*for (int i = 0; i < listOfMoves.size(); ++i){
minMax(temp, listOfMoves[i]);
}*/
temp->displayBoard();
}
}