Results 1 to 5 of 5
  1. #1
    cbear315 is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default 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();
    }
    }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    This spot needs to change
    Java Code:
    System.out.print(grid[i][j]);

  3. #3
    cbear315 is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default

    it needs to change to what? can i just plug in " * " for i and " " for b?

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  2. Eclipse "import not resolved" package problem?
    By spamsickle in forum New To Java
    Replies: 3
    Last Post: 08-24-2009, 11:44 AM
  3. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •