mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-17 03:08:02 +00:00
Allows running tests using a seeded RNG.
Running the same game twice now works! There may be issues I haven't found with certain AI behaviors around mechanics I didn't use in my tests. (cherry picked from commit 194b47c1ad61c8f1efb6bce8af2bb10d1fa8f6c3)
This commit is contained in:
@@ -2216,7 +2216,10 @@ public class ComputerUtil {
|
|||||||
}
|
}
|
||||||
return ComputerUtilCard.getBestAI(list);
|
return ComputerUtilCard.getBestAI(list);
|
||||||
} else {
|
} else {
|
||||||
return Iterables.getFirst(votes.keySet(), null);
|
// TODO: This is just picking randomly amongst already picked things. It should probably pick the worst instead.
|
||||||
|
List<Object> a = Arrays.asList(votes.keySet().toArray());
|
||||||
|
Collections.shuffle(a, MyRandom.getRandom());
|
||||||
|
return a.get(0);
|
||||||
}
|
}
|
||||||
case "Protection":
|
case "Protection":
|
||||||
if (votes.isEmpty()) {
|
if (votes.isEmpty()) {
|
||||||
|
|||||||
@@ -134,7 +134,8 @@ public class ComputerUtilMana {
|
|||||||
Collections.sort(orderedCards, new Comparator<Card>() {
|
Collections.sort(orderedCards, new Comparator<Card>() {
|
||||||
@Override
|
@Override
|
||||||
public int compare(final Card card1, final Card card2) {
|
public int compare(final Card card1, final Card card2) {
|
||||||
return Integer.compare(manaCardMap.get(card1), manaCardMap.get(card2));
|
int result = Integer.compare(manaCardMap.get(card1), manaCardMap.get(card2));
|
||||||
|
return result != 0 ? result : Float.compare(card1.getTimestamp(), card2.getTimestamp());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -308,7 +309,7 @@ public class ComputerUtilMana {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// select which abilities may be used for each shard
|
// select which abilities may be used for each shard
|
||||||
Multimap<ManaCostShard, SpellAbility> sourcesForShards = ComputerUtilMana.groupAndOrderToPayShards(ai, manaAbilityMap, cost);
|
ListMultimap<ManaCostShard, SpellAbility> sourcesForShards = ComputerUtilMana.groupAndOrderToPayShards(ai, manaAbilityMap, cost);
|
||||||
|
|
||||||
sortManaAbilities(sourcesForShards);
|
sortManaAbilities(sourcesForShards);
|
||||||
|
|
||||||
@@ -858,7 +859,6 @@ public class ComputerUtilMana {
|
|||||||
}
|
}
|
||||||
|
|
||||||
AiController aic = ((PlayerControllerAi)ai.getController()).getAi();
|
AiController aic = ((PlayerControllerAi)ai.getController()).getAi();
|
||||||
int chanceToReserve = aic.getIntProperty(AiProps.RESERVE_MANA_FOR_MAIN2_CHANCE);
|
|
||||||
|
|
||||||
PhaseType curPhase = ai.getGame().getPhaseHandler().getPhase();
|
PhaseType curPhase = ai.getGame().getPhaseHandler().getPhase();
|
||||||
|
|
||||||
@@ -877,7 +877,8 @@ public class ComputerUtilMana {
|
|||||||
// obey mana reservations for Main 2; otherwise, obey mana reservations depending on the "chance to reserve"
|
// obey mana reservations for Main 2; otherwise, obey mana reservations depending on the "chance to reserve"
|
||||||
// AI profile variable.
|
// AI profile variable.
|
||||||
if (sa.getSVar("LowPriorityAI").equals("")) {
|
if (sa.getSVar("LowPriorityAI").equals("")) {
|
||||||
if (chanceToReserve == 0 || MyRandom.getRandom().nextInt(100) >= chanceToReserve) {
|
float chanceToReserve = aic.getFloatProperty(AiProps.RESERVE_MANA_FOR_MAIN2_CHANCE);
|
||||||
|
if (MyRandom.getRandom().nextDouble() >= chanceToReserve) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1005,7 +1006,7 @@ public class ComputerUtilMana {
|
|||||||
* @return a boolean.
|
* @return a boolean.
|
||||||
*/
|
*/
|
||||||
private static String payMultipleMana(ManaCostBeingPaid testCost, String mana, final Player p) {
|
private static String payMultipleMana(ManaCostBeingPaid testCost, String mana, final Player p) {
|
||||||
List<String> unused = new ArrayList<>(4);
|
List<String> unused = new ArrayList<String>(4);
|
||||||
for (String manaPart : TextUtil.split(mana, ' ')) {
|
for (String manaPart : TextUtil.split(mana, ' ')) {
|
||||||
if (StringUtils.isNumeric(manaPart)) {
|
if (StringUtils.isNumeric(manaPart)) {
|
||||||
for (int i = Integer.parseInt(manaPart); i > 0; i--) {
|
for (int i = Integer.parseInt(manaPart); i > 0; i--) {
|
||||||
|
|||||||
@@ -34,22 +34,27 @@ public class MyRandom {
|
|||||||
private static Random random = new SecureRandom();
|
private static Random random = new SecureRandom();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* Changes into a non-CSPRNG, which can be seeded for use in tests/repeatable experiments.
|
||||||
* percentTrue.<br>
|
|
||||||
* If percent is like 30, then 30% of the time it will be true.
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @param percent
|
|
||||||
* a int.
|
|
||||||
* @return a boolean.
|
|
||||||
*/
|
*/
|
||||||
public static boolean percentTrue(final int percent) {
|
public static void setSeed(int seed) {
|
||||||
return percent > MyRandom.getRandom().nextInt(100);
|
System.out.println("Setting the RNG seed to: " + seed);
|
||||||
|
random = new Random(seed);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns True with Percent probability.
|
||||||
|
*
|
||||||
|
* TODO: My guess is no one is passing in a number scaled to 100. This API should probably be cut.
|
||||||
|
*/
|
||||||
|
public static boolean percentTrue(final long percent) {
|
||||||
|
return percent > MyRandom.getRandom().nextDouble() * 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the random.
|
* Gets the random.
|
||||||
*
|
*
|
||||||
|
* TODO: Make this private, and instead add a robust set of APIs here.
|
||||||
|
*
|
||||||
* @return the random
|
* @return the random
|
||||||
*/
|
*/
|
||||||
public static Random getRandom() {
|
public static Random getRandom() {
|
||||||
@@ -60,7 +65,7 @@ public class MyRandom {
|
|||||||
int[] groups = new int[numGroups];
|
int[] groups = new int[numGroups];
|
||||||
|
|
||||||
for (int i = 0; i < value; i++) {
|
for (int i = 0; i < value; i++) {
|
||||||
groups[random.nextInt(numGroups)]++;
|
groups[MyRandom.getRandom().nextInt(numGroups)]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return groups;
|
return groups;
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import forge.game.player.RegisteredPlayer;
|
|||||||
import forge.model.FModel;
|
import forge.model.FModel;
|
||||||
import forge.player.GamePlayerUtil;
|
import forge.player.GamePlayerUtil;
|
||||||
import forge.util.Lang;
|
import forge.util.Lang;
|
||||||
|
import forge.util.MyRandom;
|
||||||
|
|
||||||
public class SimulateMatch {
|
public class SimulateMatch {
|
||||||
public static void simulate(String[] args) {
|
public static void simulate(String[] args) {
|
||||||
@@ -55,6 +56,7 @@ public class SimulateMatch {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
System.err.println("Illegal parameter usage");
|
System.err.println("Illegal parameter usage");
|
||||||
|
argumentHelp();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -78,6 +80,10 @@ public class SimulateMatch {
|
|||||||
type = GameType.valueOf(WordUtils.capitalize(params.get("f").get(0)));
|
type = GameType.valueOf(WordUtils.capitalize(params.get("f").get(0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (params.containsKey("s")) {
|
||||||
|
MyRandom.setSeed(Integer.parseInt(params.get("s").get(0)));
|
||||||
|
}
|
||||||
|
|
||||||
GameRules rules = new GameRules(type);
|
GameRules rules = new GameRules(type);
|
||||||
rules.setAppliedVariants(EnumSet.of(type));
|
rules.setAppliedVariants(EnumSet.of(type));
|
||||||
|
|
||||||
@@ -145,7 +151,7 @@ public class SimulateMatch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void argumentHelp() {
|
private static void argumentHelp() {
|
||||||
System.out.println("Syntax: forge.exe sim -d <deck1[.dck]> ... <deckX[.dck]> -D [D] -n [N] -m [M] -t [T] -p [P] -f [F] -q");
|
System.out.println("Syntax: forge.exe sim -d <deck1[.dck]> ... <deckX[.dck]> -D [D] -n [N] -m [M] -t [T] -p [P] -f [F] -s [S] -q");
|
||||||
System.out.println("\tsim - stands for simulation mode");
|
System.out.println("\tsim - stands for simulation mode");
|
||||||
System.out.println("\tdeck1 (or deck2,...,X) - constructed deck name or filename (has to be quoted when contains multiple words)");
|
System.out.println("\tdeck1 (or deck2,...,X) - constructed deck name or filename (has to be quoted when contains multiple words)");
|
||||||
System.out.println("\tdeck is treated as file if it ends with a dot followed by three numbers or letters");
|
System.out.println("\tdeck is treated as file if it ends with a dot followed by three numbers or letters");
|
||||||
@@ -155,6 +161,7 @@ public class SimulateMatch {
|
|||||||
System.out.println("\tT - Type of tournament to run with all provided decks (Bracket, RoundRobin, Swiss)");
|
System.out.println("\tT - Type of tournament to run with all provided decks (Bracket, RoundRobin, Swiss)");
|
||||||
System.out.println("\tP - Amount of players per match (used only with Tournaments, defaults to 2)");
|
System.out.println("\tP - Amount of players per match (used only with Tournaments, defaults to 2)");
|
||||||
System.out.println("\tF - format of games, defaults to constructed");
|
System.out.println("\tF - format of games, defaults to constructed");
|
||||||
|
System.out.println("\ts - Set the RNG seed. Use if you want to play the same game twice.");
|
||||||
System.out.println("\tq - Quiet flag. Output just the game result, not the entire game log.");
|
System.out.println("\tq - Quiet flag. Output just the game result, not the entire game log.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,7 +174,6 @@ public class SimulateMatch {
|
|||||||
final Game g1 = mc.createGame();
|
final Game g1 = mc.createGame();
|
||||||
// will run match in the same thread
|
// will run match in the same thread
|
||||||
|
|
||||||
long startTime = System.currentTimeMillis();
|
|
||||||
try {
|
try {
|
||||||
TimeLimitedCodeBlock.runWithTimeout(new Runnable() {
|
TimeLimitedCodeBlock.runWithTimeout(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -29,12 +29,14 @@ import forge.item.IPaperCard;
|
|||||||
import forge.model.FModel;
|
import forge.model.FModel;
|
||||||
import forge.properties.ForgePreferences;
|
import forge.properties.ForgePreferences;
|
||||||
import forge.properties.ForgePreferences.FPref;
|
import forge.properties.ForgePreferences.FPref;
|
||||||
|
import forge.util.MyRandom;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
public class SimulationTestCase extends TestCase {
|
public class SimulationTestCase extends TestCase {
|
||||||
private static boolean initialized = false;
|
private static boolean initialized = false;
|
||||||
|
|
||||||
protected Game initAndCreateGame() {
|
protected Game initAndCreateGame() {
|
||||||
|
MyRandom.setSeed(0); // Initialize the random seed to create predictable tests.
|
||||||
List<RegisteredPlayer> players = Lists.newArrayList();
|
List<RegisteredPlayer> players = Lists.newArrayList();
|
||||||
Deck d1 = new Deck();
|
Deck d1 = new Deck();
|
||||||
players.add(new RegisteredPlayer(d1).setPlayer(new LobbyPlayerAi("p2", null)));
|
players.add(new RegisteredPlayer(d1).setPlayer(new LobbyPlayerAi("p2", null)));
|
||||||
|
|||||||
Reference in New Issue
Block a user