Code:
package AXJava;
public class Strategy {
private UniqueItemSizeLimitedPriorityQueue activeStrategy; // active strategic options
private DrawRnd allStrategies; // bank of all strategies. out of this pool active strategies are pulled
public Strategy(DrawRnd allStrategies) {
// create the strategy Object with a bank of options
this.allStrategies = allStrategies;
this.activeStrategy = new UniqueItemSizeLimitedPriorityQueue();
}
public void evolveStrategies(int strategiesLimit){
// add N strategic options to the active strategies bank, from the total strategy bank
activeStrategy.setLimit(strategiesLimit);
String temp = allStrategies.draw();
for (int i = 0; i < strategiesLimit; i++) {
if(temp.isEmpty()){
break;
}
activeStrategy.add(temp);
temp = allStrategies.draw();
}
allStrategies.reset();
}
public String getStrategy(){
return this.activeStrategy.getRNDElement();
}
}
Code:
package AXJava;
import java.util.Enumeration;
import java.util.Hashtable;
public class AXStrategy {
/* this auxiliary module is used to output strategies based on context
can be used for battles, and games
upon pain/lose use the evolve methode to update to different new active strategies
check for battle state end externaly (opponent state/hits on rival counter)
a dictionary of strategies*/
private int lim;
private Hashtable<String,Strategy> strategies = new Hashtable<>();
public AXStrategy(int lim) {
// limit of active strategies (pulled from all available strategies)
this.lim = lim;
}
public void addStrategy(String context, DrawRnd techniques){
// add strategies per context
Strategy temp = new Strategy(techniques);
temp.evolveStrategies(lim);
this.strategies.put(context,temp);
}
public void evolve(){
// replace active strategies
Enumeration<String> e = this.strategies.keys();
String key = "";
while (e.hasMoreElements()){
key = e.nextElement();
this.strategies.get(key).evolveStrategies(lim);
}
}
public String process(String context){
// process input, return action based on game context now
if(this.strategies.containsKey(context)){
return this.strategies.get(context).getStrategy();
}
return "";
}
}
example usage in main class:
Code:
public static void main(String[] args) {
DrawRnd offense = new DrawRnd("kombo1","kombo2","kombo3","kombo4","kombo5");
DrawRnd defense = new DrawRnd("zoning","reversal","backdash");
DrawRnd blocking = new DrawRnd("grapple1","grapple2","wait","hit n grab kombo");
AXStrategy combatAI = new AXStrategy(2);
combatAI.addStrategy("offense",offense);
combatAI.addStrategy("defense",defense);
combatAI.addStrategy("blocking",blocking);
System.out.println("");
for (int i = 0; i < 10; i++) {
System.out.println(combatAI.process("offense"));
System.out.println(combatAI.process("defense"));
System.out.println(combatAI.process("blocking"));
System.out.println(combatAI.process(""));
}
combatAI.evolve();
System.out.println("following game lose the combat AI has evolved!");
for (int i = 0; i < 10; i++) {
System.out.println(combatAI.process("offense"));
System.out.println(combatAI.process("defense"));
System.out.println(combatAI.process("blocking"));
System.out.println(combatAI.process(""));
}
}
output:
kombo5
zoning
grapple1
kombo1
zoning
hit n grab kombo
kombo5
zoning
grapple1
kombo5
zoning
hit n grab kombo
kombo5
zoning
grapple1
kombo1
zoning
hit n grab kombo
kombo1
zoning
grapple1
kombo5
zoning
grapple1
kombo1
zoning
grapple1
kombo1
zoning
hit n grab kombo
following game lose the combat AI has evolved!
kombo1
zoning
grapple2
kombo1
reversal
hit n grab kombo
kombo1
zoning
grapple2
kombo1
zoning
hit n grab kombo
kombo1
reversal
grapple2
kombo3
zoning
hit n grab kombo
kombo1
zoning
hit n grab kombo
kombo3
zoning
hit n grab kombo
kombo3
zoning
grapple2
kombo1
reversal
grapple2
Process finished with exit code 0