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

132 lines
2.8 KiB
C++
Raw Normal View History

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<<"CHANGE THIS TO PARSE THINGS\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-25 16:53:32 -05:00
b->interpret(move, *b);
2015-10-25 16:43:27 -05:00
}
2015-10-26 15:22:59 -05:00
while(b->getTurn() == 'X' )
2015-10-25 16:43:27 -05:00
{
2015-10-26 15:22:59 -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
}
//for debugging purposes
cout<<"Record:"<<endl;
cout<<record.size();
for (int i = 0; i < record.size(); ++i)
{
record[i].displayBoard();
}
}
2015-10-25 16:53:32 -05:00
void Engine::easyAI()
2015-10-25 16:43:27 -05:00
{
//1) see all possible movements
2015-10-25 16:53:32 -05:00
vector<moves> listOfMoves = b->viewPossibleMoves();
2015-10-25 16:43:27 -05:00
//obvious moves
if (false){
2015-10-26 15:22:59 -05:00
b->changeTurns();
2015-10-25 16:43:27 -05:00
}
//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;
2015-10-26 15:22:59 -05:00
b->move(listOfMoves[randomChoice]);
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 08:38:25 -05:00
Board temp = *b;
//probably not needed, check later
if (temp.getTurn() != 'X'){
cout << "a changing of turns is needed. \n";
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 08:38:25 -05:00
b->moveWOPrint(minMax(temp, listOfMoves[0], 0));
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 08:38:25 -05:00
moves Engine::minMax(Board temp, moves m, int c){
//testing purposes only
if (c > 1000){
return m;
}
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 08:38:25 -05:00
if(temp.isThisMovePossible(8 - m.row, temp.charToIntColumn(m.column), m.moveType)){
cout << "piece has been moved in minMax\n";
temp.moveWOPrint(m);
}
cout << "c: " << c << "\n\n";
cout << "current turn: " << temp.getTurn() << "\n";
vector<moves> listOfMoves = temp.viewPossibleMoves();
for (int i = 0; i < listOfMoves.size(); ++i){
cout << "listOfMoves[i]: (" << listOfMoves[i].row << ", " << listOfMoves[i].column << ")\n";
//minMax(temp, listOfMoves[i]);
}
2015-10-26 17:08:48 -05:00
2015-10-27 08:38:25 -05:00
return minMax(temp, listOfMoves[0], ++c);
2015-10-26 17:08:48 -05:00
}
2015-10-25 16:43:27 -05:00
}