Java - Turtle Graphics - trouble with Array size vs co-ordinates

Hi

I am in a bit of a fuddle over array size vs co-ordinates.

I am learning Java (not really included in this forum) and the question requires that I move the user around an array using a variety of commands.

To some extent, however, this is a general array problem that is applicable to any language that uses a 2d array.

Using the below code, I can get the turtle to move to square 14, but am unable to progress as all the other commands are taken up.

The examples I see on the web import a lot of java utilities that are beyond my level of knowledge.

The key difficulty, is that the array is 20 int[20][20], which really means that the maximum x co-ordinate is 19. so if I get the user to take 10 steps, they wil be at co-ordinate [0][10] and another 10 steps will take them off the array.

I then can’t seem to get my ‘turtle’ to move to square 19. Using the commands as given. Is this correct?

To keep the commands relativey less complex for the forum, I have only included the eastward direction (x++) of the turtle (although the north, south and west are just the same, albeit with moving x or y oppositely).

Below are the commands:

 // 1 = pen up (i.e. not writing to the array as pass)
                // 2 = pen down
                // 4 = direction++ (change direction)
                // 5 = move forward that many spaces
                //6 = display array
                // 10 = move forward that many spaces

My code is as follows:

import java.util.Scanner;

public class e7_21 {
        public static void main(String[] args) {

 Scanner input = new Scanner(System.in);
                int command = 0; // command to turtle
                int pen = 0; // down = 1 = true = write
                int floor[][] = new int[20][20]; // this is the map of the floor
                int y = 0; // row co-ordinate
                int maxY = 19; // maximum co-ordinate
                int x = 0 ; // column co-ordinate
                int maxX = 19; // maximum co-ordinate
                int direction = 0; // where 0 = north, 1 = east, 2, south, 3 west

	System.out.print("Please enter command: ");
		command=input.nextInt();
		while (command != -1) {
		// change pen up
		if (command == 1) 
		{
			pen = 0; 
		}
		// change pen down
		if (command == 2)
		{
			pen = 1;
		}
		// change direction
		if (command == 4)
		{
			if (direction == 3) {
				direction = 0;
			}
			else direction++;
		}
		// move spaces
		if (command == 5 || command == 10) {
                        // if facing east, it is moving plus x 
			if (direction == 1) {
				//move off the board
				if (command+x > maxX) System.out.print("Not able to move that far! Please change direction"); 

				// ok to move
				else {
					// if pen is down write it
					if (pen == 1) {
						// is moving plus x
						// number of moves not starting position
						// not changing rows, just columns 
						// put the i at the starting column and increment
						for (int i = x; i <= command+x; i++) {
							floor[y][i] = 1;
						}
						x+=command;

					}
					else x+=command;
				}// end else
			}

                System.out.print("command = " + command+"\n");
                System.out.print("pen= " + pen+"\n");
                System.out.print("direction= " + direction + "\n");
                System.out.print("x= " + x + "\n");
                System.out.print("y= " + y + "\n");
                System.out.print("Please enter pen position: ");
                } // end command 6
                System.out.print("\n\n"+"results"+"\n");
                System.out.print("command = " + command+"\n");
                System.out.print("pen= " + pen+"\n");
                System.out.print("direction= " + direction + "\n");
                System.out.print("x= " + x + "\n");
                System.out.print("y= " + y + "\n");
                System.out.print("Array\n");
                for (int i = 0; i < 20; i++) {
                        for (int j = 0; j < 20; j++)  {
                                System.out.printf("%d", floor[i][j]);
                        }
                        System.out.println();
                }

                System.out.print("Please enter command: ");
                command=input.nextInt();

                }// end while command != -1

        } // end main
} // end class

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.