Skip to content

Commit

Permalink
Added support for "| K a f q n H" instructions. Added a moveMatch fun…
Browse files Browse the repository at this point in the history
…ction to test if movement is matching a specific char int combo. updated -d tag with docs for new instructions
  • Loading branch information
aspwil committed Nov 18, 2020
1 parent 7cf2ae6 commit 8bbd678
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 25 deletions.
12 changes: 11 additions & 1 deletion src/ndballsim/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public static void main(String[] args) {
+ "\n"
+ ">n :ball moves forward on dim n\n"
+ "<n :ball moves backward on dim n\n"
+ "| :a mirror, direction of ball is reversed, dimention remains unchanged\n"
+ "Kmov :a one way mirror, direction of ball is reversed unless the balls movment matches mov\n"
+ "\n"
+ "if the ball hits a wall the program ends\n"
+ "\n"
Expand All @@ -85,6 +87,7 @@ public static void main(String[] args) {
+ "VALUE CHANGE:\n"
+ "+ :increase the ball's value\n"
+ "- :decrease the ball's value\n"
+ "R :sets the ball's value to a random number from 0 to 255 (inclusive)"
+ "\n"
+ "the balls value wraps\n"
+ "\n"
Expand All @@ -101,7 +104,14 @@ public static void main(String[] args) {
+ "\n"
+ "#mov :a memory cell that holds a value (0-255), mov is a movement statement (ex. >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"
Expand Down
36 changes: 35 additions & 1 deletion src/ndballsim/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
101 changes: 78 additions & 23 deletions src/ndballsim/Simulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.Scanner;
import java.util.HashMap;
import java.util.Random;

public class Simulator {

Expand All @@ -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<Integer, Integer> 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) {
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"
Expand All @@ -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");
Expand Down Expand Up @@ -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)));
}
}

0 comments on commit 8bbd678

Please sign in to comment.