Skip to content

Commit

Permalink
1.0.3 update
Browse files Browse the repository at this point in the history
Added step feature, now you can step though the balls movement one piece at a time
Fixed error when an empty line is included in the code
Fixed error when highest dimension was reduced to zero then that position in the array was called.
Added -max tag (default 10k) for max number of step to run
Added step counter to log
Updated tag code to support multiple tags at the same tag.
Added -i flag to list info about program instructions, steps, parse time, sim time, active memory
Fixed mem leak in pos class, no longer create an extra array list with every trim call
Changed the way the program exits when finished with sim
  • Loading branch information
aspwil committed Nov 3, 2020
1 parent 7bd43d8 commit f1615de
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 18 deletions.
10 changes: 8 additions & 2 deletions src/ndballsim/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Main {
public static void main(String[] args) {
boolean step = false;
boolean log = false;
boolean info = false;
int max = 10000;
String version = "V1.0.3";
String help = "NDBall Simulator " + version + "\n"
Expand All @@ -20,7 +21,8 @@ public static void main(String[] args) {
+ " when memory cells are written to etc\n"
+ "-d -docs : This shows some basic documentation about how to program in NDBall\n"
+ "-s -step : goes through the sim one step at a time, automaticly enables log\n"
+ "-m -max (num) : only runs a max number of steps for the ball (default 10k) use a negative number for unlimited steps";
+ "-m -max (num) : only runs a max number of steps for the ball (default 10k) use a negative number for unlimited steps\n"
+ "-i -info : spits out info about the program after it completes";
//no insput strings given
if (args.length == 0) {
System.out.println(help);
Expand Down Expand Up @@ -48,6 +50,10 @@ public static void main(String[] args) {
}
i++;
break;
case "-i":
case "-info":
info = true;
break;
case "-d":
case "-docs":
System.out.println("the ball starts at 0,0,0...\n"
Expand Down Expand Up @@ -102,7 +108,7 @@ public static void main(String[] args) {
+ "");
break;
default:
Simulator.run(args[i], max, log, step);
Simulator.run(args[i], max, log, step, info);
System.exit(0);
break;

Expand Down
8 changes: 5 additions & 3 deletions src/ndballsim/Pos.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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<Integer> list;
private int sum;

//initalization
public Pos(int... ints) {
Expand All @@ -22,7 +23,8 @@ public Pos(int... ints) {
}
//set the object to the array list generated
this.list = arraylist;
//remove excess zeros from the end of the
arraylist = null;//forces this to be garbge collected (jsut in case)
//remove excess zeros from the end of the line
trim();
}

Expand Down Expand Up @@ -60,14 +62,14 @@ public boolean equals(Pos checkPos) {
private void trim() {
//this deals with if the length is zero in all dimntions
//add all the ints in the array together
int sum = 0;
sum = 0;
for (int i : list) {
sum += i;
}
//if sum = 0
if (sum == 0) {
//set list to (0)
list = new ArrayList<>();
list.clear();
list.add(0);
//exit the function
return;
Expand Down
67 changes: 54 additions & 13 deletions src/ndballsim/Simulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,38 @@
public class Simulator {

private static boolean log = false;
private static long startTime;
private static long parseTime;
// ... the code being measured ...

public static void run(String file, int max, boolean doLog, boolean step) {
public static void run(String file, int max, boolean doLog, boolean step, boolean infoTag) {
long startTime = System.nanoTime();//start measuring parcer time
Instr[] instrs = Parser.parse(file);//this is the list of instructions
parseTime = System.nanoTime() - startTime;//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
int stepsDone = 0; //how many teps we have done
log("MAX: "+max);

log("MAX: " + max);

Pos ball = new Pos(0); //the ball itself
int ballVal = 0; //the value of the ball
int[] movement = new int[2]; //this represent the balls movement, its [dimention_number, ammount] so if it moving forwards in dim 4 then its [4,1] and backwards is [4,-1]

int newVal;
log("Attempting parsing");
Instr[] instrs = Parser.parse(file);//this is the list of instructions

log("Parsing completed");
log("Starting Simulation");
while (true) {
//if we are steping through once at a time
if (step) {
//ask for input (pauses program)
System.out.println("next step? enter yes | ctrl+c no");
System.out.println("advance? enter|ctrl+c");
in.nextLine();

}
Expand Down Expand Up @@ -61,8 +69,8 @@ public static void run(String file, int max, boolean doLog, boolean step) {
break;
//end the program
case "E":
log("program ended");
System.exit(0);
log("Program ended");
exit(infoTag, stepsDone, instrs.length);
break;
//this get a input number from the console and set the balls value to it
case "%":
Expand Down Expand Up @@ -201,12 +209,11 @@ public static void run(String file, int max, boolean doLog, boolean step) {

}
stepsDone++;
log("Step "+stepsDone+" done");
if(max >= 0 && stepsDone >= max){
warn("Program terminated: reached max steps ("+max+"), to disable this use -m -1");
System.exit(0);
log("Step " + stepsDone + " done");
if (max >= 0 && stepsDone >= max) {
warn("Program terminated: reached max steps (" + max + "), to disable this use -m -1");
exit(infoTag, stepsDone, instrs.length);
}

}

}
Expand All @@ -228,4 +235,38 @@ private static void log(String str) {
System.out.println("LOG: " + str);
}
}

//this exits the program doing nececary exit stuff
private static void exit(boolean outputInfo, int steps, int instNum) {
if (outputInfo) {
System.out.println("\n\n***** Sim Info *****\n"
+ "Parse Time: "
+ ((double) parseTime / 1_000_000.0)
+ "ms\n"
+ "Sim Time: "
+ ((double) (System.nanoTime() - startTime) / 1_000_000.0)
+ "ms\n"
+ "Steps: "
+ steps
+ "\n"
+ "Num of Instr: "
+ instNum
+ "\n"
+ memoryStats()
);
}
System.exit(0);
}

//memory useage statisitics
public static String memoryStats() {
System.gc();
int kb = 1024;
// get Runtime instance
Runtime instance = Runtime.getRuntime();
// used memory
return "Active Mem: ~" + (instance.totalMemory() - instance.freeMemory()) / kb + "KB\n";

}

}

0 comments on commit f1615de

Please sign in to comment.