Vec2 MiniMax(Board& board, unsigned int depth) { if (board.IsAllSetAble(mypiece)){ return MiniMax(board, depth, true, false).first; } else{ return MiniMax(board, depth, true, true).first; } }
std::pair<Vec2,int> MiniMax(Board& board, unsigned int depth, bool myturn,bool pass) { Piece nowpiece = myturn ? (mypiece == Piece::black ? Piece::black : Piece::white) : (mypiece == Piece::black ? Piece::white : Piece::black); if (depth == 0) if (myturn) return std::make_pair(BestN(board, nowpiece), EvaluationMap1[BestN(board, nowpiece).x][BestN(board, nowpiece).y]); else return std::make_pair(BestN(board, nowpiece, -1), EvaluationMap1[BestN(board, nowpiece, -1).x][BestN(board, nowpiece, -1).y]); if (!board.IsAllSetAble(nowpiece)){ if (pass){ if (myturn) return std::make_pair(BestN(board, nowpiece), EvaluationMap1[BestN(board, nowpiece).x][BestN(board, nowpiece).y]); else return std::make_pair(BestN(board, nowpiece, -1), EvaluationMap1[BestN(board, nowpiece, -1).x][BestN(board, nowpiece, -1).y]); } else{ return MiniMax(board, depth - 1, !myturn, true); } } auto where_=board.WhereSetAble(nowpiece); auto SetAble = std::vector<std::pair<Vec2,int>>(where_.size()); for (int i = 0; i < where_.size();i++){ SetAble[i].first = where_[i]; auto board_ = board; board_.SetPiece(SetAble[i].first, nowpiece); SetAble[i].second=MiniMax(board_, depth - 1, !myturn,false).second; } std::sort(SetAble.begin(), SetAble.end(), [](std::pair<Vec2, int> p1, std::pair<Vec2, int> p2){ return p1.second < p2.second; }); if (myturn) return SetAble[SetAble.size() - 1]; else return SetAble[0]; }
void main() { clrscr(); int pos; char choice; int legal; cout<<"Do You Want Me to Start?\nChoice(y/n):"; //Who plays the first move? cin>>choice; DisplayBoard(a); switch(choice) { case 'y': { while(move<5) //First one to move would make a maximum of 5 moves { pos=MiniMax(a); a[pos]=2; DisplayBoard(a); if(CheckWin(a)) { DisplayBoard(a); cout<<"\n\nYou lost!"; delay(5000); break; } else if(CheckDraw(a)) { DisplayBoard(a); cout<<"\n\nIts Draw!"; delay(5000); break; } enterAgain1: cout<<"\n\nEnter position(0-8):"; cin>>pos; legal=CheckLegal(a,pos); if(legal==0) { cout<<"\nInvalid Move!"; goto enterAgain1; } a[pos]=1; DisplayBoard(a); if(CheckLose(a)) { DisplayBoard(a); cout<<"\n\nYou won!"; delay(5000); break; } else if(CheckDraw(a)) { DisplayBoard(a); cout<<"\n\nIts Draw!"; delay(5000); break; } move++; } break; } case 'n': { while(move<5) { DisplayBoard(a); enterAgain2: cout<<"\n\nEnter position(0-8):"; cin>>pos; legal=CheckLegal(a,pos); if(legal==0) { cout<<"\nInvalid Move!"; goto enterAgain2; } a[pos]=1; DisplayBoard(a); if(CheckLose(a)) { DisplayBoard(a); cout<<"\n\nYou won!"; delay(5000); break; } else if(CheckDraw(a)) { DisplayBoard(a); cout<<"\n\nIts Draw!"; delay(5000); break; } pos=MiniMax(a); a[pos]=2; cout<<"\n\t"<<pos; DisplayBoard(a); if(CheckWin(a)) { DisplayBoard(a); cout<<"\n\nYou lost!"; delay(5000); break; } else if(CheckDraw(a)) { DisplayBoard(a); cout<<"\n\nIts Draw!"; delay(5000); break; } move++; } break; } default: { break; } } }
Vec2 Easy(Board& board){ return MiniMax(board, 3); }