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

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