Results 1 to 3 of 3

Thread: Help with an exersice

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    5

    Post Help with an exersice

    I want helpp about that exersice:

    (Turtle Graphics) The Logo language made the concept of turtle graphics famous. Imagine a mechanical turtle that walks around the room under the control of a Java application. The turtle holds a pen in one of two positions, up or down. While the pen is down, the turtle traces out shapes as it moves, and while the pen is up, the turtle moves about freely without writing anything. In this problem, you will simulate the operation of the turtle and create a computerized sketchpad.
    Use a 20-by-20 array floor that is initialized to zeros. Read commands from an array that contains them. Keep track of the current position of the turtle at all times and whether the pen is currently up or down. Assume that the turtle always starts at position (0, 0) of the floor with its pen up. The set of turtle commands your application must process are shown in the figure.
    Command Meaning
    1 Pen up
    2 Pen down
    3 Turn right
    4 Turn left
    5, 10 Move forward 10 spaces (replace 10 for a different number of spaces)
    6 Display the 20-by-20 array
    9 End of data (sentinel)

    Suppose that the turtle is somewhere near the center of the floor. The following "program" would draw and display a 12-by-12 square, leaving the pen in the up position:
    2
    5,12
    3
    5,12
    3
    5,12
    3
    5,12
    1
    6
    9
    As the turtle moves with the pen down, set the appropriate elements of array floor to 1s. When the 6 command (display the array) is given, wherever there is a 1 in the array, display an asterisk or any character you choose. Wherever there is a 0, display a blank.
    Write an application to implement the turtle graphics capabilities discussed here.

    I have problems with navigating into the two dimensional array.Can anyone help me plz.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Help with an exersice

    Hope this helps
    Code:
    import java.util.Scanner;
    
    public class Turtle {
    	/**
    	 * Directions: 0 right, 1 down, 2 left, 3 up
    	 */
    	private static short direction = 0;
    	private static boolean penDown;
    	private static int turtleX = 0, turtleY = 0;
    	private static int[][] floor = new int[20][20];
    
    	public static void main(String[] args) {
    		initFloor(floor);
    		Scanner in = new Scanner(System.in);
    		printMenu();
    		int nextCommand = in.nextInt();
    		while (nextCommand != 9) {
    			switch (nextCommand) {
    			case 1:
    				penDown = false;
    				break;
    			case 2:
    				penDown = true;
    				break;
    			case 3:
    				direction++;
    				break;
    			case 4:
    				direction--;
    				break;
    			case 5:
    				System.out.println("How many steps do you want to move?");
    				int move = in.nextInt();
    				if (move <= 10)
    					while (--move != 0)
    						move();
    				break;
    			case 6:
    				printArray();
    				break;
    			default:
    				System.err.println("Unknow command, please try again:\n");
    				break;
    			}
    			move();
    			System.out.println("What's next?");
    			nextCommand = in.nextInt();
    		}
    	}
    
    	private static void initFloor(int[][] floor) {
    		for (int i = 0; i < floor.length; i++) {
    			for (int j = 0; j < floor[i].length; j++) {
    				floor[i][j] = 0;
    			}
    		}
    	}
    
    	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 to 10 Move forward 10 spaces (replace 10 for a different number of spaces)\n"
    						+ "\t6 Display the 20-by-20 array\n"
    						+ "\t9 End of data (sentinel)Please enter a command number:\n");
    	}
    
    	private static void printArray() {
    		for (int i = 0; i < floor.length; i++) {
    			for (int j = 0; j < floor[i].length; j++) {
    				System.out.print(floor[i][j]);
    				System.out.print("  ");
    			}
    			System.out.println();
    		}
    	}
    
    	private static void move() {
    		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;
    			move();
    			break;
    		}
    		if (penDown) {
    			if (turtleX < 20 && turtleY < 20)
    				floor[turtleX][turtleY] = 1;
    			else {
    				direction -= 2;
    				move();
    			}
    		}
    	}
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    5

    Thumbs up Re: Help with an exersice

    Thanks for your help, but is there any way to solve it without recursion.
    Because i didnt learn about recursion yet.
    Thanks for your help anyway.

Posting Permissions

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



Click Here to Expand Forum to Full Width