#include #include #include #include #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 record; b->snapshot(record, *b); while (gameOver != true) { gameOver = b->isGameOver(); while(b->getTurn() == 'O' ) { b->displayBoard(); cout<<"\nEnter command: "; cin>>move; cout << "\n"; b->interpret(move, *b); } b->changeTurns(); while(b->getTurn() == 'X' ) { cout << "\n\n\n\nit gets here\n\n\n\n"; easyAI(); } gameOver = b->isGameOver(); b->snapshot(record, *b); } } void Engine::easyAI() { //1) see all possible movements vector 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 listOfMoves = b->viewPossibleMoves(); Board temp = *b; //probably not needed, check later if (temp.getTurn() != 'X'){ cout << "a changing of turns is needed. \n"; temp.changeTurns(); } //only doing 1 branch right now because testing /*for (int i = 0; i < listOfMoves.size(); ++i){ minMax(temp, listOfMoves[i]); }*/ b->moveWOPrint(minMax(temp, listOfMoves[0], 0)); //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){ //testing purposes only if (c > 5){ return m; } if (temp.isGameOver() == true){ cout << "END OF PATH REACHED\n"; return m; } else { 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 listOfMoves = temp.viewPossibleMoves(); for (int i = 0; i < listOfMoves.size(); ++i){ //return minMax(temp, listOfMoves[i]); } //limited recursion return minMax(temp, listOfMoves[0], ++c); //testing return m; } }