#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->isValid()){ b->displayBoard(); cout<<"\nEnter command: "; cin>>move; cout << "\n"; b->interpret(move, *b); if(b->isValid()){ b->changeTurns(); b->setValidFalse(); } } while(b->getTurn() == 'X' ){ //easyAI(); AI(3); } gameOver = b->isGameOver(); b->setValidFalse(); b->snapshot(record, *b); } } void Engine::easyAI(){ vector listOfMoves = b->viewPossibleMoves(); srand(time(NULL)); int randomChoice = rand() % (listOfMoves.size()-1) - 0; b->move(listOfMoves[randomChoice]); b->changeTurns(); } void Engine::AI(int depth){ Board* temp = new Board(*b); moves empty; MNode* root = new MNode(*temp, empty, 0); root = createMMTree(root, depth); //printTree(0, root); CURRENTLY PRODUCES SEG FAULT //only doing 1 branch right now because testing /*for (int i = 0; i < listOfMoves.size(); ++i){ minMax(b, listOfMoves[i]); }*/ //remove this soon //b->move(minMax(temp, listOfMoves[0], 0, 0; vector listOfMoves = b->viewPossibleMoves(); b->move(listOfMoves[0]); b->changeTurns(); b->displayBoard(); } MNode* Engine::createMMTree(MNode* node, int depth){ Board current = node->getState(); vector listOfMoves = current.viewPossibleMoves(); vector children = node->getChildren(); if (depth >= 0){ for (int i = 0; i < children.size(); ++i){ cout << "i: " << i << "\n"; node->addChild(createMMTree(children[i], --depth)); } } else return node; /* if (current.isGameOver() == true){ cout << "END OF PATH REACHED\n\n"; return m; } else { if(current.getPiece(8 - m.row, m.column)->getType() == current.getTurn() && current.isThisMovePossible(8 - m.row, m.column, m.moveType)){ temp->move(m); temp->evaluate('X', 'O'); temp->changeTurns(); } else { m = minMax(temp, listOfMoves[++r], c, r); } vector listOfMoves = temp->viewPossibleMoves(); minMax(temp, listOfMoves[0], 0, 0); return m; } */ } moves Engine::evaluateMMTree(MNode* node){ //returns the move from children that maximizes evaluate() moves m; vector children = node->getChildren(); for (auto &c : children){ cout << "c: " << c->getMMVal(); } return m; } /* //return sum of eval values //pass in each child of root and 0 int Engine::evaluateMMBranch(Node* node, int sum){ sum += node->getMMVal(); vector children = node->getChildren(); for (auto &c : children){ sum += evaluateMMBranch(c, sum); } return sum; } */