From 8bbd678e8c07b0eac3464c79dc3550364318e5b3 Mon Sep 17 00:00:00 2001 From: aspwil Date: Wed, 18 Nov 2020 01:37:19 -0700 Subject: [PATCH] Added support for "| K a f q n H" instructions. Added a moveMatch function to test if movement is matching a specific char int combo. updated -d tag with docs for new instructions --- src/ndballsim/Main.java | 12 ++++- src/ndballsim/Parser.java | 36 ++++++++++++- src/ndballsim/Simulator.java | 101 +++++++++++++++++++++++++++-------- 3 files changed, 124 insertions(+), 25 deletions(-) diff --git a/src/ndballsim/Main.java b/src/ndballsim/Main.java index ce2a353..201e1c7 100644 --- a/src/ndballsim/Main.java +++ b/src/ndballsim/Main.java @@ -77,6 +77,8 @@ public static void main(String[] args) { + "\n" + ">n :ball moves forward on dim n\n" + "3), if the ball's movement is the same as the cell direction then the memory cell is written to, becoming the balls value. if not the cell is read and the ball becomes the value of the memory cell. they start with a value of 0\n" + "\n" - + "SPECIAL INSTR:\n" + + "APIOFORMS:\n" + + "these instructions allows you to keep a single value apart from the balls value and change it\n" + + "a :an apioform joins the hive, increasing the have value by 1\n" + + "f :an apioform leaves the hive to polinate flowers, decreasing the value of the hive by 1\n" + + "q :the queen leaves the hive, taking all apioforms with her, hive value is now 0\n" + + "n :nector attract apioforms to or away from the hive untill its value matches the ball" + + "H :the hive itself, when run into the ball`s value becomes the hive value" + + "SPECIAL INSTR:\n" + "E end program\n" + "\n" + "Check out the wiki for more info https://esolangs.org/wiki/NDBall" diff --git a/src/ndballsim/Parser.java b/src/ndballsim/Parser.java index 8d9dd3b..7f2d031 100644 --- a/src/ndballsim/Parser.java +++ b/src/ndballsim/Parser.java @@ -255,7 +255,7 @@ public static Instr[] parse(String file) { error(lineNum, "Memory cell requires a direction ex: #>12"); } //add the memory cell to the instruction list - list.add(new Instr(pos, "#", 0, "" + line.charAt(1), dim)); + list.add(new Instr(pos, "#", 0, line.charAt(1), dim)); break; //if we cant find an instruction we error //input a char @@ -266,6 +266,40 @@ public static Instr[] parse(String file) { case 'p': list.add(new Instr(pos, "p")); break; + //mirror instruction, reverses direction + case '|': + list.add(new Instr(pos, "|")); + break; + //random instruction sets the balls value to (0-255) + case 'R': + list.add(new Instr(pos, "R")); + break; + //apioform instruction add 1 to hive + case 'a': + list.add(new Instr(pos, "a")); + break; + //flower instruction remove 1 from hive + case 'f': + list.add(new Instr(pos, "f")); + break; + //queen instruction, set hive value to 0 + case 'q': + list.add(new Instr(pos, "q")); + break; + //hive cell + case 'H': + list.add(new Instr(pos, "H")); + break; + case 'K': + try { + list.add(new Instr(pos, "K", line.charAt(1), Integer.parseInt(line.substring(2,line.length())))); + }catch (NumberFormatException e) { + error(lineNum, "\"" + line.substring(2, line.length()) + "\" could not be converted into a number"); + } + break; + case 'n': + list.add(new Instr(pos, "n")); + break; default: error(lineNum, "Unknown Instruction \"" + line.charAt(0) + "\""); break; diff --git a/src/ndballsim/Simulator.java b/src/ndballsim/Simulator.java index 23a01d3..91a2722 100644 --- a/src/ndballsim/Simulator.java +++ b/src/ndballsim/Simulator.java @@ -6,6 +6,7 @@ import java.util.Scanner; import java.util.HashMap; +import java.util.Random; public class Simulator { @@ -16,34 +17,42 @@ public class Simulator { // ... the code being measured ... public static void run(String file, int max, boolean doLog, boolean step, boolean infoTag) { + //timeing stuff and long parseStartTime = System.nanoTime();//start measuring parcer time Instr[] instrs = Parser.parse(file);//this is the sorted list of instructions parseTime = System.nanoTime() - parseStartTime;//stop measuring parcer time //begine messuring Sim time startTime = System.nanoTime(); + //this hash map maps the highest dimention to the position of the first ocourance of that dmention in the instr list HashMap startPos = new HashMap<>(); //assemble the hash map - for(int i = 0; i < instrs.length; i++){ - if(!startPos.containsKey(instrs[i].pos.getHighestDim())){ + for (int i = 0; i < instrs.length; i++) { + if (!startPos.containsKey(instrs[i].pos.getHighestDim())) { startPos.put(instrs[i].pos.getHighestDim(), i); } } - + + //varaibles and stats 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); + //object used in simulation + Random ran = new Random(); + Scanner in = new Scanner(System.in); //the scanner used for input from console + + //log the max amount of steps + log("MAX: " + max); + + //simulation values 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"); + int hiveVal = 0;//this is the value of the hive cell + int newVal; //this is use to store a vlue for later use in the program + log("Attempting parsing"); log("Parsing completed"); log("Starting Simulation"); while (true) { @@ -57,7 +66,7 @@ public static void run(String file, int max, boolean doLog, boolean step, boolea //check if there is instruction at balls position for (int i = startPos.get(ball.getHighestDim()); i < instrs.length; i++) { - if(ball.getHighestDim() != instrs[i].pos.getHighestDim()){ + if (ball.getHighestDim() != instrs[i].pos.getHighestDim()) { break; } //we found a matching instruction @@ -157,7 +166,7 @@ public static void run(String file, int max, boolean doLog, boolean step, boolea //data cell case "#": //check if the balls movement matches the memeory cells writing direction - if ((int) instrs[i].info[2] == movement[0] && (((String) instrs[i].info[1]).equals(">") && movement[1] == 1 || (((String) instrs[i].info[1]).equals("<") && movement[1] == -1))) { + if (matchMove(movement,(char) instrs[i].info[1],(int) instrs[i].info[2])) { //write to the cell instrs[i].info[0] = ballVal; log("MEM CELL WRITTEN Val:" + ballVal + " Pos:" + instrs[i].pos); @@ -189,6 +198,48 @@ public static void run(String file, int max, boolean doLog, boolean step, boolea case "p": System.out.print((char) ballVal); break; + //mirror instruction, reverses direction + case "|": + //set the direction of movement to its oppisite + movement[1] = -movement[1]; + break; + //one way mirror + case "K": + //if the movement of ball matches defined movemnt of mirror + if (matchMove(movement,(char)instrs[i].info[0],(int)instrs[i].info[1])) { + //let the ball travel though + break; + } else { + //reverse movement direction + movement[1] = -movement[1]; + } + break; + //random instruction sets the balls value to (0-255) + case "R": + ballVal = ran.nextInt(256);//set ball to random int + break; + //apioform instruction add 1 to hive + case "a": + hiveVal=(hiveVal+1)%255; + break; + //flower instruction remove 1 from hive + case "f": + hiveVal--; + if(hiveVal == -1){ + hiveVal = 255; + } + break; + //queen instruction, set hive value to 0 + case "q": + hiveVal = 0; + break; + //hive cell + case "H": + ballVal = hiveVal; + break; + case "n": + hiveVal = ballVal; + break; //the parcer spit out an unknown instruction default: error("Unkown Internal Instruction.\n" @@ -212,15 +263,15 @@ 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 - //error if the ball hits the wall - 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.getLength(movement[0]) == -1) { - error("The ball hit the wall at " + ball + " and shatterd into a thousand peices"); - System.exit(1); - } + //error if the ball hits the wall + 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.getLength(movement[0]) == -1) { + error("The ball hit the wall at " + ball + " and shatterd into a thousand peices"); + System.exit(1); + } stepsDone++; log("Step " + stepsDone + " done"); @@ -280,14 +331,18 @@ private static String memoryStats() { Runtime instance = Runtime.getRuntime(); // used memory return "Active Mem: ~" + (instance.totalMemory() - instance.freeMemory()) / kb + "KB\n"; - + } + //this allows up to get input and pause timer for time waiting for input - private static String getInput(Scanner scan){ + private static String getInput(Scanner scan) { long start = System.nanoTime(); String input = scan.nextLine(); - timeToRemove+= (System.nanoTime() - start); + timeToRemove += (System.nanoTime() - start); return input; } + private static boolean matchMove(int[] mov,char dir, int dim){ + return (dim == mov[0] && ((dir == '>' && mov[1] == 1) || (dir == '<' && mov[1] == -1))); + } }