Skip to content

Commit

Permalink
Improve the function for calculating SEV
Browse files Browse the repository at this point in the history
Count how close the player and the opponent are to victory
If the opponent is too close, then start to place walls
  • Loading branch information
danliukuri committed Nov 6, 2021
1 parent faf59da commit 7f97fdb
Showing 1 changed file with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Quoridor.Controllers.PlayerControllers.AI.Strong.MinimaxAdapters
{
public static class MinimaxScoreFunction
{
public static double CalculateForTwoPlayers(MinimaxStateAdapter minimaxAdapter, Player player)
public static double CalculateForTwoPlayers(MinimaxStateAdapter minimaxAdapter, IPlayer player)
{
if (minimaxAdapter is null)
{
Expand All @@ -25,22 +25,30 @@ public static double CalculateForTwoPlayers(MinimaxStateAdapter minimaxAdapter,
" Parameter name: " + nameof(quoridorModel.Players));
}

Player firstPlayer = (Player)quoridorModel.Players[0];
Player secondPlayer = (Player)quoridorModel.Players[1];
double score; // A number that shows how close the player is to victory
IPlayer firstPlayer = quoridorModel.Players[0];
IPlayer secondPlayer = quoridorModel.Players[1];
IPlayer opponent = player == firstPlayer ? secondPlayer : firstPlayer;

if (player == firstPlayer)
{
score = CalculateForPlayer(firstPlayer, quoridorModel);
}
else
{
score = CalculateForPlayer(secondPlayer, quoridorModel);
}
return score;
return CalculateForPlayer(player, opponent, quoridorModel);
}
static double CalculateForPlayer(IPlayer player, IPlayer opponent, QuoridorModel quoridorModel)
{
// Maximum number of steps to win in a straight line
int maxStepsToVictory = quoridorModel.Field.Height - 1;

static double CalculateForPlayer(Player player, QuoridorModel quoridorModel)
// A number that shows how close the player is to victory
double score = maxStepsToVictory / CalculateStepsToVictoryForPlayer(player, quoridorModel);

double opponentStepsToVictory = CalculateStepsToVictoryForPlayer(opponent, quoridorModel);

if (opponentStepsToVictory < 2)
score -= maxStepsToVictory / opponentStepsToVictory;
else if (opponentStepsToVictory < 4)
score -= maxStepsToVictory / opponentStepsToVictory / 2;

return score;
}
static double CalculateStepsToVictoryForPlayer(IPlayer player, QuoridorModel quoridorModel)
{
IFieldNode finded = AStar.FindTheShortestPath(quoridorModel.Field, player.Position,
player.Goal.Item2, player.Goal.Item1);
Expand All @@ -54,8 +62,7 @@ static double CalculateForPlayer(Player player, QuoridorModel quoridorModel)
fieldNode = fieldNode.PreviousPathNode;
}
}
// Value inversely proportional to the number of steps to victory
return (double)(quoridorModel.Field.Height - 1) / stepsToVictory;
return stepsToVictory;
}
}
}

0 comments on commit 7f97fdb

Please sign in to comment.