Az alapskálák
Testvér-alapskálák VII.
Java-programkód
Main
import java.util.ArrayList;
import java.util.List;
public class Main {
private List<ArrayList<State>> resultList = new ArrayList<ArrayList<State>>();
private Transitions trans = new Transitions();
/**
* @param args
* @throws IllegalAccessException
*/
public static void main(String[] args) throws IllegalAccessException {
Main m = new Main();
m.stateMachine(State.ONE, new ArrayList<State>());
m.stateMachine(State.TWO, new ArrayList<State>());
m.stateMachine(State.THREE, new ArrayList<State>());
for (ArrayList<State> actualList : m.resultList) {
System.out.println(actualList + " - " + m.trans.getListValue(actualList));
}
}
public void stateMachine(State s, ArrayList<State> actualList) throws
IllegalAccessException{
actualList.add(s);
List<State> transitions = getStatesFromState(s);
for (State state : transitions) {
if (actualList.size() == 14){
if (trans.getListValue(actualList) == 12 && listValidation(actualList)){
resultList.add(actualList);
}
return;
}
stateMachine(state, (ArrayList<State>) actualList.clone());
}
}
private List<State> getStatesFromState(State s){
List<State> transitions = new ArrayList<State>();
switch (s) {
case ONE:
transitions.add(State.TWO);
transitions.add(State.THREE);
break;
case TWO:
transitions.add(State.TWO);
transitions.add(State.THREE);
break;
case THREE:
transitions.add(State.ONE);
break;
}
return transitions;
}
private boolean listValidation(ArrayList<State> list){
List<State> subList1 = list.subList(0, 7);
List<State> subList2 = list.subList(7, 14);
return subList1.equals(subList2);
}
}
State
public enum State {
ONE(3), TWO(4), THREE(3);
private int startValue;
private State(int startValue) {
this.startValue = startValue;
}
public int getStartValue(){
return startValue;
}
}
Transition
import java.util.ArrayList;
public class Transitions {
private int getTransitionsValueBetweenStates(State state1, State state2)
throws IllegalAccessException{
if (State.ONE.equals(state1)){
if (State.ONE.equals(state2)) throw new IllegalAccessException();
if (State.TWO.equals(state2)) return 2;
if (State.THREE.equals(state2)) return 1;
} else if (State.TWO.equals(state1)){
if (State.ONE.equals(state2)) throw new IllegalAccessException();
if (State.TWO.equals(state2)) return 2;
if (State.THREE.equals(state2)) return 1;
} else if (State.THREE.equals(state1)){
if (State.ONE.equals(state2)) return 2;
if (State.TWO.equals(state2)) throw new IllegalAccessException();
if (State.THREE.equals(state2)) throw new IllegalAccessException();
}
throw new IllegalAccessException();
}
public int getListValue(ArrayList<State> list) throws IllegalAccessException{
int result = list.get(0).getStartValue();
for (int i = 1; i < 6; i++) {
result += getTransitionsValueBetweenStates(list.get(i-1), list.get(i));
}
return result;
}
}
Eredmény
[ONE, TWO, TWO, TWO,
THREE, ONE, THREE, ONE, TWO, TWO, TWO, THREE, ONE, THREE]
[ONE, TWO, TWO, THREE, ONE, TWO, THREE, ONE, TWO, TWO, THREE, ONE, TWO, THREE]
[ONE, TWO, THREE, ONE, TWO, TWO, THREE, ONE, TWO, THREE, ONE, TWO, TWO, THREE]
[ONE, THREE, ONE, TWO, TWO, TWO, THREE, ONE, THREE, ONE, TWO, TWO, TWO, THREE]
[TWO, TWO, TWO, THREE, ONE, THREE, ONE, TWO, TWO, TWO, THREE, ONE, THREE, ONE]
[TWO, TWO, THREE, ONE, TWO, THREE, ONE, TWO, TWO, THREE, ONE, TWO, THREE, ONE]
[TWO, TWO, THREE, ONE, THREE, ONE, TWO, TWO, TWO, THREE, ONE, THREE, ONE, TWO]
[TWO, THREE, ONE, TWO, TWO, THREE, ONE, TWO, THREE, ONE, TWO, TWO, THREE, ONE]
[TWO, THREE, ONE, TWO, THREE, ONE, TWO, TWO, THREE, ONE, TWO, THREE, ONE, TWO]
[TWO, THREE, ONE, THREE, ONE, TWO, TWO, TWO, THREE, ONE, THREE, ONE, TWO, TWO]
[THREE, ONE, TWO, TWO, TWO, THREE, ONE, THREE, ONE, TWO, TWO, TWO, THREE, ONE]
[THREE, ONE, TWO, TWO, THREE, ONE, TWO, THREE, ONE, TWO, TWO, THREE, ONE, TWO]
[THREE, ONE, TWO, THREE, ONE, TWO, TWO, THREE, ONE, TWO, THREE, ONE, TWO, TWO]
[THREE, ONE, THREE, ONE, TWO, TWO, TWO, THREE, ONE, THREE, ONE, TWO, TWO, TWO]
A 3 fenti kód letölthető innen: