Results 1 to 5 of 5
Thread: need help with "turtle" problem
- 09-17-2009, 07:14 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 4
- Rep Power
- 0
need help with "turtle" problem
I am doing an assignment and am brand new to java. I just need to know where in this coding I can tweak the 1's to display as * and the 0's to disappear when option 6 is chosen. Any help would be great! Thanks.
//DrawingTurtle.java
//Allows user to draw on a 20 x 20 grid by command
import java.util.Scanner;
public class DrawingTurtle {
private static short direction = 0;
private static boolean penDown;
private static int turtleX = 0, turtleY = 0; //initializes the turtle to 0,0
private static int[][] grid = new int[20][20]; //grid is 20 x 20
public static void main(String[] args) {
initGrid(grid);
Scanner in = new Scanner(System.in);
printMenu(); //calls for function to display options
int nextCommand = in.nextInt();
while (nextCommand != 9) {
switch (nextCommand) {
//option for pen down
case 1:
penDown = false;
break;
//option for pen up
case 2:
penDown = true;
break;
//option to turn right
case 3:
direction++;
break;
//option to turn right
case 4:
direction--;
break;
//asks the user when 5 is entered to obtain number of steps
case 5:
System.out.println("How many spaces?");
int move = in.nextInt();
if (move <= 20)
while (--move != 0)
turtleMove();
break;
//displays the grid
case 6:
printArray();
break;
default:
System.err.println("Please enter valid command:\n");
break;
}
turtleMove();
System.out.println("Next command: ");
nextCommand = in.nextInt();
}
}
//shows the user what the options are the entire time the program is running
private static void printMenu() {
System.out.println("Commands List:"
+ "\n\n\t1 Pen up\n"
+ "\t2 Pen down\n"
+ "\t3 Turn right\n"
+ "\t4 Turn left\n"
+ "\t5 Move forward an inputted number\n"
+ "\t6 Display the 20 x 20 array\n"
+ "\t9 End of data (sentinel)\n"
+ "Please enter a command number: ");
}
//keeps track of the turtle on the grid
private static void initGrid(int[][] grid) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
grid[i][j] = 0;
}
}
}
//moves the turtle in a direction that is inputted
private static void turtleMove() {
switch (direction) {
case 0:
turtleX++;
break;
case 1:
turtleY++;
break;
case 2:
turtleX--;
break;
case 3:
turtleY--;
break;
default:
if (direction < 0)
direction = 3;
else
direction = 4;
turtleMove();
break;
}
if (penDown) {
if (turtleX < 20 && turtleY < 20)
grid[turtleX][turtleY] = 1; /*applies a 1 to the spot
/*if the pen is down*/
else {
direction = 0;
turtleMove();
}
}
}
//displays the grid as the user has made it so far
private static void printArray() {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
System.out.print(grid[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
}
- 09-17-2009, 07:26 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
This spot needs to change
Java Code:System.out.print(grid[i][j]);
- 09-17-2009, 07:53 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 4
- Rep Power
- 0
it needs to change to what? can i just plug in " * " for i and " " for b?
- 09-17-2009, 09:07 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
You want a space for zero and * for one right? So add an if statement to look at the value and then print the proper value, of course.
- 09-18-2009, 01:23 AM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Put an if statement in checking if the option is selected, if it is, use this line as the print statement, and dont use the other one (put in an else block). The code assumes only 1 and 0 are in the array, otherwise you will need additional if-else statements.
Java Code:System.out.print(grid[i][j]==0?' ':'*');
Last edited by Singing Boyo; 09-18-2009 at 01:29 AM.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
Similar Threads
-
Java, Military Format using "/" and "%" Operator!!
By sk8rsam77 in forum New To JavaReplies: 11Last Post: 02-26-2010, 03:03 AM -
Eclipse "import not resolved" package problem?
By spamsickle in forum New To JavaReplies: 3Last Post: 08-24-2009, 11:44 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
"Jumble" or "Scramble" Program
By Shadow22202 in forum Java AppletsReplies: 8Last Post: 04-30-2008, 03:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks