diff --git a/nbproject/project.properties b/nbproject/project.properties index 06d10f6..6049ddd 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -5,6 +5,7 @@ annotation.processing.run.all.processors=true annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output application.title=NDBallSim application.vendor=aspwi +auxiliary.org-netbeans-spi-editor-hints-projects.perProjectHintSettingsFile=nbproject/cfg_hints.xml build.classes.dir=${build.dir}/classes build.classes.excludes=**/*.java,**/*.form # This directory is removed when the project is cleaned: diff --git a/src/ndballsim/Instr.java b/src/ndballsim/Instr.java index 9a8fc8c..93e1574 100644 --- a/src/ndballsim/Instr.java +++ b/src/ndballsim/Instr.java @@ -5,6 +5,8 @@ //this class defines the Instruction object, it has no real maningful data, its just to group together data together package ndballsim; +import java.util.Arrays; + public class Instr { //pos the position of the instruction in n-dim space public Pos pos; @@ -20,4 +22,9 @@ public Instr(Pos pos,String name, Object... inputs){ this.info = inputs; } + @Override + public String toString(){ + return "Instr: "+name+", "+pos+", "+Arrays.toString(info); + } + } diff --git a/src/ndballsim/Main.java b/src/ndballsim/Main.java index a0950c6..edd86a5 100644 --- a/src/ndballsim/Main.java +++ b/src/ndballsim/Main.java @@ -2,11 +2,12 @@ * NDBall Simulator by Aspen Wilson is licensed under CC0 1.0. To view a copy of this license, visit https://creativecommons.org/publicdomain/zero/1.0 */ package ndballsim; - +import ndballsim.Pos.Vector; //this is the calss is run with the jar file, it will handle the command line input public class Main { public static void main(String[] args) { + boolean step = false; boolean log = false; boolean info = false; diff --git a/src/ndballsim/Pos.java b/src/ndballsim/Pos.java index 683b9c4..82046ad 100644 --- a/src/ndballsim/Pos.java +++ b/src/ndballsim/Pos.java @@ -8,106 +8,130 @@ public class Pos { - //this array list stores values like this (length_in_dim_0, length_in_dim_1, length_in_dim_2...) - //this is specificly private to allow for more efficent Pos classes to be able to be used with replacement of this class, withought having to reprogram everything - private ArrayList list; - private int sum; + //this is a vector that stores in dim,length + public class Vector { + + public int dim; + public int length; + + public Vector(int dim, int length) { + this.dim = dim; + this.length = length; + } + + @Override + public boolean equals(Object o) { + // If the object is compared with itself then return true + if (o == this) { + return true; + } + /* Check if o is an instance of Complex or not + "null instanceof [type]" also returns false */ + if (!(o instanceof Vector)) { + return false; + } + // typecast o to Vector so that we can compare data members + Vector v = (Vector) o; + // Compare the data members and return accordingly + return v.dim == dim && v.length == length; + } + //we add this so hash codes dont mess up from the equals over ride + @Override + public int hashCode() { + int hash = 7; + hash = 97 * hash + this.dim; + hash = 97 * hash + this.length; + return hash; + } + + @Override + public String toString(){ + return dim+","+length; + } + + } + + //this array list atores a buch of vectors + private ArrayList list; //initalization public Pos(int... ints) { - //new array list - ArrayList arraylist = new ArrayList<>(); - //add all the ints from the input to the list - for (int i : ints) { - arraylist.add(i); + list = new ArrayList<>(); + for (int i = 0; i < ints.length; i++) { + + if (ints[i] != 0) { + list.add(new Vector(i, ints[i])); + } } - //set the object to the array list generated - this.list = arraylist; - arraylist = null;//forces this to be garbge collected (jsut in case) - //remove excess zeros from the end of the line - trim(); } - public ArrayList getPos() { - return list; + public int getLength(int dim) { + for (Vector v : list) { + if (v.dim == dim) { + return v.length; + } + } + return 0; } - public void setPos(ArrayList newList) { - list = newList; + public void setLength(int dim, int length) { + for (int i = 0; i < list.size(); i++) { + if (list.get(i).dim == dim) { + list.get(i).length = length; + return; + } + } + list.add(new Vector(dim, length)); } //this tells if 2 positions are the same in n-dimentional space public boolean equals(Pos checkPos) { - //assumend to be true - boolean isEqual = true; - //if there not the same length they dont have the same highest dimention so not equal + //if they are not the same length they dont have the same number of defined dimentions if (list.size() != checkPos.list.size()) { - isEqual = false; + return false; } else { - //check if the value in each dimnetion is the same - for (int i = 0; i < list.size(); i++) { - //if not - if (list.get(i) != checkPos.list.get(i)) { - //not equal - isEqual = false; - //stop the for loop, we dont need to check anymore - break; + //for each vector in check pos check if + for (Vector v: checkPos.list) { + if(!list.contains(v)){ + return false; } } } - return isEqual; + return true; } - + //this removes exces zeros private void trim() { - //this deals with if the length is zero in all dimntions - //add all the ints in the array together - sum = 0; - for (int i : list) { - sum += i; - } - //if sum = 0 - if (sum == 0) { - //set list to (0) - list.clear(); - list.add(0); - //exit the function - return; - } - //if not we will reach here - //remove extra zeros from the end untill no more are left - while (list.get(list.size() - 1) == 0) { - list.remove(list.size() - 1); - } + list.removeIf(list -> list.length == 0); } //this moves the position along ammount in dimention dim public void shift(int dim, int amount) { - //if the dimention were trying to write to is not defined, add more 0`s untill we reach it - if (list.size() - 1 < dim) { - while (list.size() - 1 != dim) { - list.add(0); + for(int i = 0; i < list.size(); i++){ + if(list.get(i).dim == dim){ + list.get(i).length += amount; + trim(); + return; } } - //shift the value at dimention dim by ammount - list.set(dim, list.get(dim) + amount); - //trim off any zeros, this only matters if we shift so the last value is 0 - trim(); + list.add(new Vector(dim, amount)); } //this outputs the dimention as (a,b,c...) @Override public String toString() { - //start pos - String listString = "("; - //add ints - for (int i : list) { - listString += i + ", "; + //add a starting { + String listString = "{"; + //add the peices + for (Vector v : list) { + listString += v+"|"; + } + //remove last "|" + if (listString.length() != 1){ + listString = listString.substring(0, listString.length() - 1); } - //remove last ", " - listString = listString.substring(0, listString.length() - 2); - //add last ) - listString += ")"; + //add closeing } + listString += "}"; return listString; } } diff --git a/src/ndballsim/Simulator.java b/src/ndballsim/Simulator.java index 3ed7c45..8bb82fd 100644 --- a/src/ndballsim/Simulator.java +++ b/src/ndballsim/Simulator.java @@ -4,6 +4,7 @@ //this class simulates the ball in n-dim space and run ther program based on a list if instructions package ndballsim; +import java.util.Arrays; import java.util.Scanner; public class Simulator { @@ -19,7 +20,6 @@ public static void run(String file, int max, boolean doLog, boolean step, boolea parseTime = System.nanoTime() - parseStartTime;//stop measuring parcer time //begine messuring Sim time startTime = System.nanoTime(); - log = doLog; Scanner in = new Scanner(System.in); //the scanner used for input from console String input; // this will be used to hold the input @@ -195,19 +195,16 @@ public static void run(String file, int max, boolean doLog, boolean step, boolea //actaly move the ball based on the movement ball.shift(movement[0], movement[1]); //this will only throw an error if the current dimention were movinth through is erased aka (0), in which case the check wont detect anything anyway - try { //error if the ball hits the wall - if (ball.getPos().get(movement[0]) > 4) { + if (ball.getLength(movement[0]) > 4) { error("The ball hit the wall at " + ball + " and shatterd into a thousand peices"); System.exit(1); } - if (ball.getPos().get(movement[0]) == -1) { + if (ball.getLength(movement[0]) == -1) { error("The ball hit the wall at " + ball + " and shatterd into a thousand peices"); System.exit(1); } - } catch (IndexOutOfBoundsException e) { - } stepsDone++; log("Step " + stepsDone + " done"); if (max >= 0 && stepsDone >= max) {