public int makemove(char[] gameboard) { // get the humanmarker, or enemy marker humanmarker = GetEnemyPlayer(aimarker); // call the max function to start the minmax process to get the best move returnmove = max(gameboard); return returnmove.Position; }
//The max part of minmax, looks for the best move for the computer private bestmove max(char[] gameboard) { //Create variables used in the function //bestscore is for the best score bestposition for the position //move is used for the responce of min and what is returned //gamestate is used to store the current state of the gameboard //validmoves is an array of the moves open to the computer int bestscore; int bestposition; bestmove move = new bestmove(); int gamestate; int[] validmoves; //setting default values bestscore = -1; move.Score = -2; bestposition = -1; //get an array of the moves the player needs to run though validmoves = GenerateValidMoves(gameboard); foreach (int i in validmoves) { //make the move gameboard[i] = aimarker; //check gamestate gamestate = util.ValidateGamestate(gameboard, aimarker); //if game is still on going call min if (gamestate == 3) { move = min(gameboard); //If the reutrn score from min is better than the current bestscore //set teh return value to the best score and the position to the max function made if (move.Score > bestscore) { bestscore = move.Score; bestposition = i; } } //else the max player won, set the bestscore to the gamestate and the bestposition to //the value currently played else { if (gamestate > bestscore) { bestscore = gamestate; bestposition = i; } } //Undo the move made to go through and make the next move gameboard[i] = '-'; } //set the move struct with the bestscore and position and return it move.Score = bestscore; move.Position = bestposition; return move; }
//The min part of minmax, works about the same as max expect it looks for the best move of the enemy private bestmove min(char[] gameboard) { int bestscore; int bestposition; bestmove move = new bestmove(); int gamestate; int[] validmoves; bestscore = 2; move.Score = -2; bestposition = 2; validmoves = GenerateValidMoves(gameboard); foreach (int i in validmoves) { gameboard[i] = humanmarker; gamestate = util.ValidateGamestate(gameboard, humanmarker); if (gamestate == 1) gamestate = -1; if (gamestate == 3) { move = max(gameboard); if (move.Score < bestscore) { bestscore = move.Score; bestposition = i; } } else { if (gamestate < bestscore) { bestscore = gamestate; bestposition = i; } } gameboard[i] = '-'; } move.Score = bestscore; move.Position = bestposition; return move; }