Playing game program for water sort puzzle
import java.util.ArrayList;
public class Water implements Puzzle<ArrayList<Integer>> {
private int current;
private ArrayList<Integer> J;
public ArrayList<Integer> getStart() {
ArrayList<Integer> Start = new ArrayList<Integer>();
for (int i = 0; i < J.size(); i ++){ Start.add(0);}
return Start;
}
public boolean calculateResult(ArrayList<Integer> configures) {
for (Integer i : configures){
if (i.compareTo(current) == 0){
return true;
}
}
return false;
}
public ArrayList<ArrayList<Integer>> pour (ArrayList<Integer> configures){
ArrayList<ArrayList<Integer>> pourConfigs = new ArrayList<ArrayList<Integer>>();
for (int i = 0; i < J.size(); i++){
for (int x = 0; x < J.size(); x++){
ArrayList<Integer> Cc = new ArrayList<Integer>();
Cc.addAll(configures);
if (i != x){
int maxCpa = J.get(x);
int currCpa = maxCpa - Cc.get(x) ;
if (Cc.get(i) <= currCpa){
Cc.set(i, Cc.get(x) + Cc.get(i) );
Cc.set(i, 0);
}
else {
Cc.set(x, maxCpa);
Cc.set(i, Cc.get(i) - currCpa);
}
pourConfigs.add(Cc);
}
}
}
return pourConfigs;
}
public ArrayList<ArrayList<Integer>> fill (ArrayList<Integer> configures){
ArrayList<ArrayList<Integer>> fillConfigs = new ArrayList<ArrayList<Integer>>();
for(int i = 0; i < configures.size(); i++){
ArrayList<Integer> Cc = new ArrayList<Integer>();
Cc.addAll(configures);
Cc.set(i, J.get(i));
fillConfigs.add(Cc);
}
return fillConfigs;
}
public ArrayList<ArrayList<Integer>> dump (ArrayList<Integer> configures){
ArrayList<ArrayList<Integer>> dumpConfigs = new ArrayList<ArrayList<Integer>>();
for(int i = 0; i < configures.size(); i++){
ArrayList<Integer> Cc = new ArrayList<Integer>();
Cc.addAll(configures);
Cc.set(i, 0);
dumpConfigs.add(Cc);
}
return dumpConfigs;
}
public ArrayList<ArrayList<Integer>> getNearest(ArrayList<Integer> configures) {
ArrayList<ArrayList<Integer>> Nearest = new ArrayList<ArrayList<Integer>>();
Nearest.addAll(pour(configures));
Nearest.addAll(fill(configures));
Nearest.addAll(dump(configures));
return Nearest;
}
public Water(int current, ArrayList<Integer> J){
this.current = current;
this.J = J;
}
public static void main(String[] args) {
if (args.length < 2){
System.out.println("Usage: java Water amount J1 J2 ... ");
System.exit(0);
}
Integer c = Integer.parseInt(args[0]);
ArrayList<Integer> x = new ArrayList<Integer>();
for(int i = 1; i < args.length; i ++ ){
x.add(Integer.parseInt(args[i]));
}
Puzzle<ArrayList<Integer>> water1 = new Water(c , x);
Solver <ArrayList<Integer>> i = new Solver <ArrayList<Integer>>();
ArrayList<ArrayList<Integer>> soln = i.solvePuzzle(water1);
if (soln == null){
System.out.println("No solution.");
}
else {
int i = 0;
while (!(soln.isEmpty())){
System.out.print("Step "+i+":");
for (int m = 0; m < soln.get(0).size(); m++){
System.out.print(" " +soln.get(0).get(m));
}
System.out.println("");
soln.remove(0);
i++;
}
}
}
}
Comments
Leave a comment