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

143 lines
3.2 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<<"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
while(b->getTurn() == 'O' && !b->isValid())
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);
if(b->isValid()) {
b->changeTurns();
b->setValidFalse();
}
2015-10-25 16:43:27 -05:00
}
2015-10-27 10:40:55 -05:00
if(b->isValid()) cout << b->getTurn();
2015-10-27 13:48:45 -05:00
2015-10-26 15:22:59 -05:00
while(b->getTurn() == 'X' )
2015-10-25 16:43:27 -05:00
{
easyAI();
2015-10-25 16:43:27 -05:00
}
2015-10-25 16:53:32 -05:00
gameOver = b->isGameOver();
b->setValidFalse();
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();
/*
for(int x = 0; x < listOfMoves.size(); ++x) {
cout << listOfMoves[x].row << " " << listOfMoves[x].column << " " << listOfMoves[x].moveType << "\n\n";
}
*/
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
2015-10-27 21:19:24 -05:00
//int temp = randomChoice;
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 21:19:24 -05:00
Board* temp = new Board(*b);
2015-10-27 08:38:25 -05:00
//probably not needed, check later
/*
if (b->getTurn() != 'X'){
2015-10-27 08:38:25 -05:00
cout << "a changing of turns is needed. \n";
b->changeTurns();
2015-10-26 17:08:48 -05:00
}
*/
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(b, listOfMoves[i]);
2015-10-26 17:08:48 -05:00
}*/
2015-10-27 10:40:55 -05:00
2015-10-27 21:48:04 -05:00
b->move(minMax(temp, listOfMoves[0], 0, 0));
2015-10-27 12:55:23 -05:00
b->changeTurns();
2015-10-27 10:40:55 -05:00
//verification of correct turn
/*
2015-10-27 10:40:55 -05:00
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 21:48:04 -05:00
moves Engine::minMax(Board* temp, moves m, int c, int r){
//testing purposes only, c = finite depth
2015-10-27 21:19:24 -05:00
cout << "c: " << c << "\n\n";
cout << "current turn: " << temp->getTurn() << "\n";
vector<moves> listOfMoves = temp->viewPossibleMoves();
2015-10-27 21:48:04 -05:00
cout << "listOfMoves size: " << listOfMoves.size() << "\n\n";
2015-10-27 12:55:23 -05:00
if (c > 5){
2015-10-27 08:38:25 -05:00
return m;
}
2015-10-27 21:19:24 -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 21:48:04 -05:00
if(temp->getPiece(8 - m.row, m.column)->getType() == temp->getTurn() && temp->isThisMovePossible(8 - m.row, m.column, m.moveType)){
2015-10-27 08:38:25 -05:00
cout << "piece has been moved in minMax\n";
2015-10-27 21:19:24 -05:00
temp->move(m);
temp->changeTurns();
2015-10-27 21:48:04 -05:00
//temp->displayBoard();
2015-10-27 21:19:24 -05:00
}
else {
2015-10-27 21:48:04 -05:00
cout << m.row << " " << m.column << "\n\n";
m = minMax(temp, listOfMoves[++r], c, r);
2015-10-27 08:38:25 -05:00
}
2015-10-27 10:40:55 -05:00
2015-10-27 21:19:24 -05:00
temp->displayBoard();
2015-10-27 21:48:04 -05:00
vector<moves> listOfMoves = temp->viewPossibleMoves();
minMax(temp, listOfMoves[c], ++c, 0);
2015-10-27 10:40:55 -05:00
//testing
return m;
2015-10-27 21:19:24 -05:00
}
2015-10-25 16:43:27 -05:00
}