AI: fixed not working choice with key-value dialogs, random refactor

This commit is contained in:
Oleg Agafonov 2018-01-04 00:23:20 +04:00
parent ec50a123ec
commit 3dda5712db
8 changed files with 77 additions and 64 deletions

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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<String> 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);

View file

@ -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;
}

View file

@ -367,13 +367,7 @@ public class RandomPlayer extends ComputerPlayer {
@Override
public boolean choose(Outcome outcome, Choice choice, Game game) {
Iterator<String> 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;
}

View file

@ -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);
}

View file

@ -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<String, Integer> sortData);
Map<String, Integer> getSortData();
// random choice
void setRandomChoice();
boolean setChoiceByAnswers(List<String> answers, boolean removeSelectAnswerFromList);
}

View file

@ -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<String> answers, boolean removeSelectAnswerFromList){
// select by answers
if(this.isKeyChoice()){
// keys mode
for (String needChoice : answers) {
for (Map.Entry<String, String> 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
}
}