I have just created a simple class that can "draw" integers on 20x20 2d array. (some of you may be familiar with the Turtle.java assignment, well, I'm just making a really simple version of that right now).
Currently I issue commands through the main method as follows:
Turtle Romina = new Turtle();
Romina.setStage();
Romina.moveEast(5);
Romina.moveNorth(5);
Romina.setPenState("u");
Romina.moveWest(5);
Romina.setPenState("d");
Romina.moveSouth(5);
System.out.println(Romina.getArray());
this would output somthing like a 20x20 array of 0's with a square in the middle drawn with 1's (excluding the top part of the square because the "pen" is lifted during that part).
The output appears as expected. Perfect.
Now, I would like to issue those commands through an external file.
The commands would be as follows:
u = pen up
d = pen down
n10 = move up 10 spaces
e100 = move right 100 spaces
...
...
etc.
I can read from the external file, but I am not sure how to handle the data. Currentley I wrote this bit of code that breaks the file down by line:
try {
/**Read the specified file to give the turtle Directions*/
BufferedReader turtleBrain = new BufferedReader(new FileReader("C:\\iTurtle.txt"));
/**Seperate the file by line*/
String thought;
while ((thought = turtleBrain.readLine()) != null){
//just for testing
System.out.println(thought);
}
} catch (FileNotFoundException ex) {
System.out.println("File not found");
} catch (IOException ex){
System.out.println("IO ERROR");
}
Now, in that while loop I will check each line (i was thinking of using a switch/case) to call a certain method.
i.e. case "u" will call setPenState with the parameter "u".
The above is pretty straight forward, I don't have any trouble with that... What I would like some guidance on are the movements:
for example:
the line read = n8
I will have: case "n" that calls moveNorth and sends it the parameter "8".
Right now I a messing around with grabbing the line as a string, remove and check the first charater using RegEx, and if the first char is n, s, e, or w (North, South, East, West) parseInt the following number and send it to the appropriate method.
I have a feeling there is a better way to do this.
The iTurtle.txt file would contain somthing like:
u
d
n13
s10
e3
w9
u
e3
n5
d
s7
u
q
Thanks