mirror of
https://github.com/magefree/mage.git
synced 2026-01-26 13:19:18 -08:00
refactor : removed all instances of new Random() and replaced with RandomUtil for a ThreadLocal randomizer
This commit is contained in:
parent
f2cc8d4571
commit
e2a479255a
51 changed files with 173 additions and 133 deletions
|
|
@ -56,6 +56,7 @@ import mage.game.permanent.token.EmptyToken;
|
|||
import mage.game.turn.TurnMod;
|
||||
import mage.players.Player;
|
||||
import mage.util.CardUtil;
|
||||
import mage.util.RandomUtil;
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
@ -135,7 +136,6 @@ class MomirEmblem extends Emblem {
|
|||
|
||||
class MomirEffect extends OneShotEffect {
|
||||
|
||||
private static final Random rnd = new Random();
|
||||
|
||||
public MomirEffect() {
|
||||
super(Outcome.PutCreatureInPlay);
|
||||
|
|
@ -158,7 +158,7 @@ class MomirEffect extends OneShotEffect {
|
|||
CardCriteria criteria = new CardCriteria().types(CardType.CREATURE).convertedManaCost(value);
|
||||
List<CardInfo> options = CardRepository.instance.findCards(criteria);
|
||||
if (options != null && !options.isEmpty()) {
|
||||
Card card = options.get(rnd.nextInt(options.size())).getCard();
|
||||
Card card = options.get(RandomUtil.nextInt(options.size())).getCard();
|
||||
EmptyToken token = new EmptyToken();
|
||||
CardUtil.copyTo(token).from(card);
|
||||
token.putOntoBattlefield(1, game, source.getSourceId(), source.getControllerId(), false, false);
|
||||
|
|
|
|||
|
|
@ -104,6 +104,7 @@ import mage.players.Player;
|
|||
import mage.target.Target;
|
||||
import mage.target.TargetCard;
|
||||
import mage.target.Targets;
|
||||
import mage.util.RandomUtil;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
|
|
@ -128,7 +129,6 @@ public class ComputerPlayer6 extends ComputerPlayer /*implements Player*/ {
|
|||
protected Set<String> actionCache;
|
||||
private static final List<TreeOptimizer> optimizers = new ArrayList<>();
|
||||
protected int lastLoggedTurn = 0;
|
||||
Random random = new Random();
|
||||
protected static final String BLANKS = "...............................................";
|
||||
|
||||
static {
|
||||
|
|
@ -631,7 +631,7 @@ public class ComputerPlayer6 extends ComputerPlayer /*implements Player*/ {
|
|||
if (depth == maxDepth && action instanceof PassAbility) {
|
||||
val = val - PASSIVITY_PENALTY; // passivity penalty
|
||||
}
|
||||
if (val > alpha || (depth == maxDepth && val == alpha && random.nextBoolean())) { // Adding random for equal value to get change sometimes
|
||||
if (val > alpha || (depth == maxDepth && val == alpha && RandomUtil.nextBoolean())) { // Adding random for equal value to get change sometimes
|
||||
alpha = val;
|
||||
bestNode = newNode;
|
||||
bestNode.setScore(val);
|
||||
|
|
|
|||
|
|
@ -138,10 +138,7 @@ import mage.target.common.TargetDiscard;
|
|||
import mage.target.common.TargetOpponent;
|
||||
import mage.target.common.TargetPermanentOrPlayer;
|
||||
import mage.target.common.TargetSpellOrPermanent;
|
||||
import mage.util.Copier;
|
||||
import mage.util.MessageToClient;
|
||||
import mage.util.TournamentUtil;
|
||||
import mage.util.TreeNode;
|
||||
import mage.util.*;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
|
|
@ -165,7 +162,6 @@ public class ComputerPlayer extends PlayerImpl implements Player {
|
|||
private transient List<ColoredManaSymbol> chosenColors;
|
||||
|
||||
private transient ManaCost currentUnpaidMana;
|
||||
private final Random random = new Random();
|
||||
|
||||
public ComputerPlayer(String name, RangeOfInfluence range) {
|
||||
super(name, range);
|
||||
|
|
@ -1258,7 +1254,7 @@ public class ComputerPlayer extends PlayerImpl implements Player {
|
|||
public int announceXCost(int min, int max, String message, Game game, Ability ability, VariableCost variablCost) {
|
||||
log.debug("announceXMana");
|
||||
//TODO: improve this
|
||||
int value = new Random().nextInt(max + 1);
|
||||
int value = RandomUtil.nextInt(max + 1);
|
||||
if (value < max) {
|
||||
value++;
|
||||
}
|
||||
|
|
@ -1558,7 +1554,7 @@ public class ComputerPlayer extends PlayerImpl implements Player {
|
|||
}
|
||||
//TODO: improve this
|
||||
if (min < max && min == 0) {
|
||||
return new Random().nextInt(max + 1);
|
||||
return RandomUtil.nextInt(max + 1);
|
||||
}
|
||||
return min;
|
||||
}
|
||||
|
|
@ -1587,7 +1583,6 @@ public class ComputerPlayer extends PlayerImpl implements Player {
|
|||
}
|
||||
|
||||
private static void addBasicLands(Deck deck, String landName, int number) {
|
||||
Random random = new Random();
|
||||
Set<String> landSets = TournamentUtil.getLandSetCodeForDeckSets(deck.getExpansionSetCodes());
|
||||
|
||||
CardCriteria criteria = new CardCriteria();
|
||||
|
|
@ -1605,7 +1600,7 @@ public class ComputerPlayer extends PlayerImpl implements Player {
|
|||
}
|
||||
|
||||
for (int i = 0; i < number; i++) {
|
||||
Card land = cards.get(random.nextInt(cards.size())).getCard();
|
||||
Card land = cards.get(RandomUtil.nextInt(cards.size())).getCard();
|
||||
deck.getCards().add(land);
|
||||
}
|
||||
}
|
||||
|
|
@ -2239,7 +2234,7 @@ public class ComputerPlayer extends PlayerImpl implements Player {
|
|||
UUID randomOpponentId = game.getOpponents(abilityControllerId).iterator().next();
|
||||
Set<UUID> opponents = game.getOpponents(abilityControllerId);
|
||||
if (opponents.size() > 1) {
|
||||
int rand = random.nextInt(opponents.size());
|
||||
int rand = RandomUtil.nextInt(opponents.size());
|
||||
int count = 0;
|
||||
for (UUID currentId : opponents) {
|
||||
if (count == rand) {
|
||||
|
|
|
|||
|
|
@ -54,6 +54,7 @@ import mage.players.Player;
|
|||
import mage.target.Target;
|
||||
import mage.target.TargetAmount;
|
||||
import mage.target.TargetCard;
|
||||
import mage.util.RandomUtil;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
|
|
@ -65,7 +66,6 @@ import org.apache.log4j.Logger;
|
|||
public class SimulatedPlayerMCTS extends MCTSPlayer {
|
||||
|
||||
private boolean isSimulatedPlayer;
|
||||
private static Random rnd = new Random();
|
||||
private int actionCount = 0;
|
||||
private static final Logger logger = Logger.getLogger(SimulatedPlayerMCTS.class);
|
||||
|
||||
|
|
@ -115,21 +115,21 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
|
|||
if (playables.size() == 1) {
|
||||
ability = playables.get(0);
|
||||
} else {
|
||||
ability = playables.get(rnd.nextInt(playables.size()));
|
||||
ability = playables.get(RandomUtil.nextInt(playables.size()));
|
||||
}
|
||||
List<Ability> options = getPlayableOptions(ability, game);
|
||||
if (!options.isEmpty()) {
|
||||
if (options.size() == 1) {
|
||||
ability = options.get(0);
|
||||
} else {
|
||||
ability = options.get(rnd.nextInt(options.size()));
|
||||
ability = options.get(RandomUtil.nextInt(options.size()));
|
||||
}
|
||||
}
|
||||
if (ability.getManaCosts().getVariableCosts().size() > 0) {
|
||||
int amount = getAvailableManaProducers(game).size() - ability.getManaCosts().convertedManaCost();
|
||||
if (amount > 0) {
|
||||
ability = ability.copy();
|
||||
ability.getManaCostsToPay().add(new GenericManaCost(rnd.nextInt(amount)));
|
||||
ability.getManaCostsToPay().add(new GenericManaCost(RandomUtil.nextInt(amount)));
|
||||
}
|
||||
}
|
||||
// check if ability kills player, if not then it's ok to play
|
||||
|
|
@ -163,7 +163,7 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
|
|||
if (options.size() == 1) {
|
||||
ability = options.get(0);
|
||||
} else {
|
||||
ability = options.get(rnd.nextInt(options.size()));
|
||||
ability = options.get(RandomUtil.nextInt(options.size()));
|
||||
}
|
||||
}
|
||||
if (ability.isUsesStack()) {
|
||||
|
|
@ -192,7 +192,7 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
|
|||
List<Permanent> attackersList = super.getAvailableAttackers(defenderId, game);
|
||||
//use binary digits to calculate powerset of attackers
|
||||
int powerElements = (int) Math.pow(2, attackersList.size());
|
||||
int value = rnd.nextInt(powerElements);
|
||||
int value = RandomUtil.nextInt(powerElements);
|
||||
StringBuilder binary = new StringBuilder();
|
||||
binary.append(Integer.toBinaryString(value));
|
||||
while (binary.length() < attackersList.size()) {
|
||||
|
|
@ -219,7 +219,7 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
|
|||
|
||||
List<Permanent> blockers = getAvailableBlockers(game);
|
||||
for (Permanent blocker : blockers) {
|
||||
int check = rnd.nextInt(numGroups + 1);
|
||||
int check = RandomUtil.nextInt(numGroups + 1);
|
||||
if (check < numGroups) {
|
||||
CombatGroup group = game.getCombat().getGroups().get(check);
|
||||
if (group.getAttackers().size() > 0) {
|
||||
|
|
@ -245,7 +245,7 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
|
|||
return true;
|
||||
}
|
||||
Iterator<UUID> it = possibleTargets.iterator();
|
||||
int targetNum = rnd.nextInt(possibleTargets.size());
|
||||
int targetNum = RandomUtil.nextInt(possibleTargets.size());
|
||||
UUID targetId = it.next();
|
||||
for (int i = 0; i < targetNum; i++) {
|
||||
targetId = it.next();
|
||||
|
|
@ -260,7 +260,7 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
|
|||
return false;
|
||||
}
|
||||
if (!target.isRequired(source)) {
|
||||
if (rnd.nextInt(possibleTargets.size() + 1) == 0) {
|
||||
if (RandomUtil.nextInt(possibleTargets.size() + 1) == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -269,7 +269,7 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
|
|||
return true;
|
||||
}
|
||||
Iterator<UUID> it = possibleTargets.iterator();
|
||||
int targetNum = rnd.nextInt(possibleTargets.size());
|
||||
int targetNum = RandomUtil.nextInt(possibleTargets.size());
|
||||
UUID targetId = it.next();
|
||||
for (int i = 0; i < targetNum; i++) {
|
||||
targetId = it.next();
|
||||
|
|
@ -305,7 +305,7 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
|
|||
return false;
|
||||
}
|
||||
Iterator<UUID> it = possibleTargets.iterator();
|
||||
int targetNum = rnd.nextInt(possibleTargets.size());
|
||||
int targetNum = RandomUtil.nextInt(possibleTargets.size());
|
||||
UUID targetId = it.next();
|
||||
for (int i = 0; i < targetNum; i++) {
|
||||
targetId = it.next();
|
||||
|
|
@ -338,7 +338,7 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
|
|||
return !target.isRequired(source);
|
||||
}
|
||||
if (!target.isRequired(source)) {
|
||||
if (rnd.nextInt(possibleTargets.size() + 1) == 0) {
|
||||
if (RandomUtil.nextInt(possibleTargets.size() + 1) == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -347,24 +347,24 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
|
|||
return true;
|
||||
}
|
||||
Iterator<UUID> it = possibleTargets.iterator();
|
||||
int targetNum = rnd.nextInt(possibleTargets.size());
|
||||
int targetNum = RandomUtil.nextInt(possibleTargets.size());
|
||||
UUID targetId = it.next();
|
||||
for (int i = 0; i < targetNum; i++) {
|
||||
targetId = it.next();
|
||||
}
|
||||
target.addTarget(targetId, rnd.nextInt(target.getAmountRemaining()) + 1, source, game);
|
||||
target.addTarget(targetId, RandomUtil.nextInt(target.getAmountRemaining()) + 1, source, game);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean chooseMulligan(Game game) {
|
||||
return rnd.nextBoolean();
|
||||
return RandomUtil.nextBoolean();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean chooseUse(Outcome outcome, String message, Ability source, Game game) {
|
||||
if (this.isHuman()) {
|
||||
return rnd.nextBoolean();
|
||||
return RandomUtil.nextBoolean();
|
||||
}
|
||||
return super.chooseUse(outcome, message, source, game);
|
||||
}
|
||||
|
|
@ -372,7 +372,7 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
|
|||
@Override
|
||||
public boolean choosePile(Outcome outcome, String message, List<? extends Card> pile1, List<? extends Card> pile2, Game game) {
|
||||
if (this.isHuman()) {
|
||||
return rnd.nextBoolean();
|
||||
return RandomUtil.nextBoolean();
|
||||
}
|
||||
return super.choosePile(outcome, message, pile1, pile2, game);
|
||||
}
|
||||
|
|
@ -382,7 +382,7 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
|
|||
if (this.isHuman()) {
|
||||
Iterator<String> it = choice.getChoices().iterator();
|
||||
String sChoice = it.next();
|
||||
int choiceNum = rnd.nextInt(choice.getChoices().size());
|
||||
int choiceNum = RandomUtil.nextInt(choice.getChoices().size());
|
||||
for (int i = 0; i < choiceNum; i++) {
|
||||
sChoice = it.next();
|
||||
}
|
||||
|
|
@ -395,7 +395,7 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
|
|||
@Override
|
||||
public int chooseReplacementEffect(Map<String, String> rEffects, Game game) {
|
||||
if (this.isHuman()) {
|
||||
return rnd.nextInt(rEffects.size());
|
||||
return RandomUtil.nextInt(rEffects.size());
|
||||
}
|
||||
return super.chooseReplacementEffect(rEffects, game);
|
||||
}
|
||||
|
|
@ -403,7 +403,7 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
|
|||
@Override
|
||||
public TriggeredAbility chooseTriggeredAbility(List<TriggeredAbility> abilities, Game game) {
|
||||
if (this.isHuman()) {
|
||||
return abilities.get(rnd.nextInt(abilities.size()));
|
||||
return abilities.get(RandomUtil.nextInt(abilities.size()));
|
||||
}
|
||||
return super.chooseTriggeredAbility(abilities, game);
|
||||
}
|
||||
|
|
@ -416,7 +416,7 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
|
|||
if (modes.size() == 1) {
|
||||
return mode;
|
||||
}
|
||||
int modeNum = rnd.nextInt(modes.getAvailableModes(source, game).size());
|
||||
int modeNum = RandomUtil.nextInt(modes.getAvailableModes(source, game).size());
|
||||
for (int i = 0; i < modeNum; i++) {
|
||||
mode = it.next();
|
||||
}
|
||||
|
|
@ -428,7 +428,7 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
|
|||
@Override
|
||||
public UUID chooseAttackerOrder(List<Permanent> attackers, Game game) {
|
||||
if (this.isHuman()) {
|
||||
return attackers.get(rnd.nextInt(attackers.size())).getId();
|
||||
return attackers.get(RandomUtil.nextInt(attackers.size())).getId();
|
||||
}
|
||||
return super.chooseAttackerOrder(attackers, game);
|
||||
}
|
||||
|
|
@ -436,7 +436,7 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
|
|||
@Override
|
||||
public UUID chooseBlockerOrder(List<Permanent> blockers, CombatGroup combatGroup, List<UUID> blockerOrder, Game game) {
|
||||
if (this.isHuman()) {
|
||||
return blockers.get(rnd.nextInt(blockers.size())).getId();
|
||||
return blockers.get(RandomUtil.nextInt(blockers.size())).getId();
|
||||
}
|
||||
return super.chooseBlockerOrder(blockers, combatGroup, blockerOrder, game);
|
||||
}
|
||||
|
|
@ -452,8 +452,8 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
|
|||
targetId = targets.get(0);
|
||||
amount = remainingDamage;
|
||||
} else {
|
||||
targetId = targets.get(rnd.nextInt(targets.size()));
|
||||
amount = rnd.nextInt(damage + 1);
|
||||
targetId = targets.get(RandomUtil.nextInt(targets.size()));
|
||||
amount = RandomUtil.nextInt(damage + 1);
|
||||
}
|
||||
Permanent permanent = game.getPermanent(targetId);
|
||||
if (permanent != null) {
|
||||
|
|
@ -476,7 +476,7 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
|
|||
@Override
|
||||
public int getAmount(int min, int max, String message, Game game) {
|
||||
if (this.isHuman()) {
|
||||
return rnd.nextInt(max - min) + min;
|
||||
return RandomUtil.nextInt(max - min) + min;
|
||||
}
|
||||
return super.getAmount(min, max, message, game);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue