From 3dda5712db5dffdabd9b581284688e1cc6e2f4b5 Mon Sep 17 00:00:00 2001 From: Oleg Agafonov Date: Thu, 4 Jan 2018 00:23:20 +0400 Subject: [PATCH] AI: fixed not working choice with key-value dialogs, random refactor --- .../src/mage/player/ai/ComputerPlayer6.java | 9 +-- .../java/mage/player/ai/ComputerPlayer.java | 19 +++---- .../mage/player/ai/SimulatedPlayerMCTS.java | 15 +---- .../src/mage/player/ai/ComputerPlayer2.java | 16 ++---- .../org/mage/test/player/RandomPlayer.java | 8 +-- .../java/org/mage/test/player/TestPlayer.java | 10 +--- Mage/src/main/java/mage/choices/Choice.java | 7 ++- .../main/java/mage/choices/ChoiceImpl.java | 57 +++++++++++++++++-- 8 files changed, 77 insertions(+), 64 deletions(-) diff --git a/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer6.java b/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer6.java index b4055ba53f..92952e9ec9 100644 --- a/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer6.java +++ b/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer6.java @@ -767,14 +767,7 @@ public class ComputerPlayer6 extends ComputerPlayer /*implements Player*/ { return super.choose(outcome, choice, game); } if (!choice.isChosen()) { - for (String achoice : choices) { - choice.setChoice(achoice); - if (choice.isChosen()) { - choices.clear(); - return true; - } - } - return false; + return choice.setChoiceByAnswers(choices, true); } return true; } diff --git a/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java b/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java index 79018eb4d6..08bb65641c 100644 --- a/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java +++ b/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java @@ -1301,9 +1301,13 @@ public class ComputerPlayer extends PlayerImpl implements Player { public boolean choose(Outcome outcome, Choice choice, Game game) { log.debug("choose 3"); //TODO: improve this - if (choice.getMessage() != null && choice.getMessage().equals("Choose creature type")) { + + // choose creature type + // TODO: WTF?! Creature types dialog text can changes, need to replace that code + if (choice.getMessage() != null && (choice.getMessage().equals("Choose creature type") || choice.getMessage().equals("Choose a creature type"))) { chooseCreatureType(outcome, choice, game); } + // choose the correct color to pay a spell if (outcome == Outcome.PutManaInPool && choice instanceof ChoiceColor && currentUnpaidMana != null) { if (currentUnpaidMana.containsColor(ColoredManaSymbol.W) && choice.getChoices().contains("White")) { @@ -1331,19 +1335,12 @@ public class ComputerPlayer extends PlayerImpl implements Player { return true; } } + // choose by random if (!choice.isChosen()) { - int choiceIdx = (int) (Math.random() * choice.getChoices().size() + 1); - for (String next : choice.getChoices()) { - if (--choiceIdx > 0) { - continue; - } - if (!next.isEmpty()) { - choice.setChoice(next); - break; - } - } + choice.setRandomChoice(); } + return true; } diff --git a/Mage.Server.Plugins/Mage.Player.AIMCTS/src/mage/player/ai/SimulatedPlayerMCTS.java b/Mage.Server.Plugins/Mage.Player.AIMCTS/src/mage/player/ai/SimulatedPlayerMCTS.java index f2b59669e4..aeceb34f6d 100644 --- a/Mage.Server.Plugins/Mage.Player.AIMCTS/src/mage/player/ai/SimulatedPlayerMCTS.java +++ b/Mage.Server.Plugins/Mage.Player.AIMCTS/src/mage/player/ai/SimulatedPlayerMCTS.java @@ -28,11 +28,8 @@ package mage.player.ai; import java.io.Serializable; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.UUID; +import java.util.*; + import mage.abilities.Ability; import mage.abilities.ActivatedAbility; import mage.abilities.Mode; @@ -379,13 +376,7 @@ public class SimulatedPlayerMCTS extends MCTSPlayer { @Override public boolean choose(Outcome outcome, Choice choice, Game game) { if (this.isHuman()) { - Iterator it = choice.getChoices().iterator(); - String sChoice = it.next(); - int choiceNum = RandomUtil.nextInt(choice.getChoices().size()); - for (int i = 0; i < choiceNum; i++) { - sChoice = it.next(); - } - choice.setChoice(sChoice); + choice.setRandomChoice(); return true; } return super.choose(outcome, choice, game); diff --git a/Mage.Server.Plugins/Mage.Player.AIMinimax/src/mage/player/ai/ComputerPlayer2.java b/Mage.Server.Plugins/Mage.Player.AIMinimax/src/mage/player/ai/ComputerPlayer2.java index fa86b2205a..b2e36b59f5 100644 --- a/Mage.Server.Plugins/Mage.Player.AIMinimax/src/mage/player/ai/ComputerPlayer2.java +++ b/Mage.Server.Plugins/Mage.Player.AIMinimax/src/mage/player/ai/ComputerPlayer2.java @@ -473,18 +473,14 @@ public class ComputerPlayer2 extends ComputerPlayer implements Player { @Override public boolean choose(Outcome outcome, Choice choice, Game game) { - if (choices.isEmpty()) + if (choices.isEmpty()) { return super.choose(outcome, choice, game); - if (!choice.isChosen()) { - for (String achoice: choices) { - choice.setChoice(achoice); - if (choice.isChosen()) { - choices.clear(); - return true; - } - } - return false; } + + if (!choice.isChosen()) { + return choice.setChoiceByAnswers(choices, true); + } + return true; } diff --git a/Mage.Tests/src/test/java/org/mage/test/player/RandomPlayer.java b/Mage.Tests/src/test/java/org/mage/test/player/RandomPlayer.java index ff3efe5d65..b17461f02a 100644 --- a/Mage.Tests/src/test/java/org/mage/test/player/RandomPlayer.java +++ b/Mage.Tests/src/test/java/org/mage/test/player/RandomPlayer.java @@ -367,13 +367,7 @@ public class RandomPlayer extends ComputerPlayer { @Override public boolean choose(Outcome outcome, Choice choice, Game game) { - Iterator it = choice.getChoices().iterator(); - String sChoice = it.next(); - int choiceNum = rnd.nextInt(choice.getChoices().size()); - for (int i = 0; i < choiceNum; i++) { - sChoice = it.next(); - } - choice.setChoice(sChoice); + choice.setRandomChoice(); return true; } diff --git a/Mage.Tests/src/test/java/org/mage/test/player/TestPlayer.java b/Mage.Tests/src/test/java/org/mage/test/player/TestPlayer.java index 1d017f284d..80c2bea0a3 100644 --- a/Mage.Tests/src/test/java/org/mage/test/player/TestPlayer.java +++ b/Mage.Tests/src/test/java/org/mage/test/player/TestPlayer.java @@ -740,15 +740,7 @@ public class TestPlayer implements Player { @Override public boolean choose(Outcome outcome, Choice choice, Game game) { if (!choices.isEmpty()) { - for (String choose2 : choices) { - for (String choose1 : choice.getChoices()) { - if (choose1.equals(choose2)) { - choice.setChoice(choose2); - choices.remove(choose2); - return true; - } - } - } + return choice.setChoiceByAnswers(choices, true); } return computerPlayer.choose(outcome, choice, game); } diff --git a/Mage/src/main/java/mage/choices/Choice.java b/Mage/src/main/java/mage/choices/Choice.java index dbb9051750..c7dfd93159 100644 --- a/Mage/src/main/java/mage/choices/Choice.java +++ b/Mage/src/main/java/mage/choices/Choice.java @@ -28,12 +28,13 @@ package mage.choices; +import java.util.List; import java.util.Map; import java.util.Set; /** * - * @author BetaSteward_at_googlemail.com + * @author BetaSteward_at_googlemail.com, JayDi85 */ public interface Choice { @@ -73,4 +74,8 @@ public interface Choice { boolean isSortEnabled(); void setSortData(Map sortData); Map getSortData(); + + // random choice + void setRandomChoice(); + boolean setChoiceByAnswers(List answers, boolean removeSelectAnswerFromList); } diff --git a/Mage/src/main/java/mage/choices/ChoiceImpl.java b/Mage/src/main/java/mage/choices/ChoiceImpl.java index e87c171b49..6d7214a589 100644 --- a/Mage/src/main/java/mage/choices/ChoiceImpl.java +++ b/Mage/src/main/java/mage/choices/ChoiceImpl.java @@ -29,18 +29,14 @@ package mage.choices; import java.io.Serializable; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; /** * - * @author BetaSteward_at_googlemail.com + * @author BetaSteward_at_googlemail.com, JayDi85 */ public class ChoiceImpl implements Choice, Serializable { - // TODO: add sorting to items protected boolean chosen; protected final boolean required; protected String choice; @@ -52,6 +48,7 @@ public class ChoiceImpl implements Choice, Serializable { protected String subMessage; protected boolean searchEnabled = true; // enable for all windows by default protected String searchText; + private static Random rnd = new Random(); public ChoiceImpl() { this(false); @@ -210,4 +207,52 @@ public class ChoiceImpl implements Choice, Serializable { return this.sortData; }; + @Override + public void setRandomChoice() { + + if(this.isKeyChoice()){ + // key mode + String[] vals = this.getKeyChoices().keySet().toArray(new String[0]); + if(vals.length > 0) { + int choiceNum = rnd.nextInt(vals.length); + this.setChoiceByKey(vals[choiceNum]); + } + } else { + // string mode + String[] vals = this.getChoices().toArray(new String[0]); + if(vals.length > 0) { + int choiceNum = rnd.nextInt(vals.length); + this.setChoice(vals[choiceNum]); + } + } + } + + @Override + public boolean setChoiceByAnswers(List answers, boolean removeSelectAnswerFromList){ + // select by answers + if(this.isKeyChoice()){ + // keys mode + for (String needChoice : answers) { + for (Map.Entry currentChoice: this.getKeyChoices().entrySet()) { + if (currentChoice.getKey().equals(needChoice)) { + this.setChoiceByKey(needChoice); + answers.remove(needChoice); + return true; + } + } + } + } else { + // string mode + for (String needChoice : answers) { + for (String currentChoice : this.getChoices()) { + if (currentChoice.equals(needChoice)) { + this.setChoice(needChoice); + answers.remove(needChoice); + return true; + } + } + } + } + return false; // can't find answer + } } \ No newline at end of file