Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2010
    Posts
    8
    Rep Power
    0

    Default I can't get this to run correctly

    I have made two classes, and it runs, just with errors. After making the array and placing the mouse on the "island," when I try and move the mouse, it doesn't move (I know this by printing the array after placing the mouse and printing it again after I "move" the mouse). Also, my RandomMove() method doesn't work. Any help would be greatly appreciated.
    Here is my first class:
    Java Code:
    public class MouseIsland
    {
    	int[][] island;
    	int xvalue = 0, yvalue = 0, mouse = 1, MoveCount = 0, StarveCount = 0;
    	int DrownCount = 0, EscapeCount = 0;
    	public MouseIsland()
    	{
    		island = new int[0][0];
    	}
    	public MouseIsland(int size)
    	{
    		island = new int [size][size];
    		xvalue = size;
    		yvalue = size;
    		for(int i=0; i<island.length; ++i)
    			for(int j=0; j<island[i].length; ++j)
    			{
    				if(i==0 || i==(island.length-1))
    					island [i][j] = -1;
    				else if(j==0 || j==(island[i].length-1))
    						island [i][j] = -1;
    				else
    					island [i][j] = 0;
    			}
    	}
    	public void PlaceBridge(int x, int y)
    	{
    		if(x<0 || x>= xvalue)
    			System.err.println("Please enter a 'x' integer from 0-"+(xvalue-1));
    		else if(y<0 || y>= yvalue)
    			System.err.println("Please enter a 'y' integer from 0-"+(yvalue-1));
    		else
    			for(int i=0; i<island.length; ++i)
    				for(int j=0; j<island[i].length; ++j)
    					island[x][y] = 999;
    	}
    	public void PlaceMouse(int x, int y)
    	{
    		if(x<=0 || x>= xvalue-1)
    			System.err.println("Please enter a 'x' integer from 1-"+(xvalue-2));
    		else if(y<=0 || y>= yvalue-1)
    			System.err.println("Please enter a 'y' integer from 1-"+(yvalue-2));
    		else
    			for(int i=0; i<island.length; ++i)
    				for(int j=0; j<island[i].length; ++j)
    					island[x][y] = mouse;
    	}
    	public void MoveLeft()
    	{
    		int[][] temp = island;
    		if(MoveCount == 100)
    		{
    			System.out.println("The mouse starved");
    			StarveCount++;
    			MoveCount = 0;
    			System.out.println("\n Start Over by making a new array");
    		}
    		for(int i=0; i<island.length; ++i)
    			for(int j=0; j<island[i].length; ++j)
    				if(MoveCount == 0 && island[i][j] == 1)
    				{
    					temp[i][j] = island[i][j-1]; 
    					MoveCount++;
    					if(temp[i][j] == -1)
    					{
    						System.out.println("The mouse drowned after moving "+MoveCount+" times.");
    						DrownCount++;
    						MoveCount = 0;
    						System.out.println("\n Start Over by making a new array");
    						break;
    					}
    					else if(temp[i][j] == 999)
    					{
    						System.out.println("The mouse found a bridge and escaped after "+MoveCount+" times.");
    						EscapeCount++;
    						MoveCount = 0;
    						System.out.println("\n Start Over by making a new array");
    						break;
    					}
    					mouse = temp[i][j];
    					temp[i][j] = MoveCount+1;
    					
    				}	
    				else if(island[i][j]==mouse && island[i][j]!=999)
    				{
    					temp[i][j] = island[i][j-1];
    					MoveCount++;
    					if(temp[i][j] == -1)
    					{
    						System.out.println("The mouse drowned after moving "+MoveCount+" times.");
    						DrownCount++;
    						MoveCount = 0;
    						System.out.println("\n Start Over by making a new array");
    						break;
    					}
    					else if(temp[i][j] == 999)
    					{
    						System.out.println("The mouse found a bridge and escaped after "+MoveCount+" times.");
    						EscapeCount++;
    						MoveCount = 0;
    						System.out.println("\n Start Over by making a new array");
    						break;
    					}
    					temp[i][j] = MoveCount+1;
    					mouse = temp[i][j];
    				}	
    		island = temp;		
    	}
    	public void MoveRight()
    	{
    		int[][] temp = island;
    		if(MoveCount == 100)
    		{
    			System.out.println("The mouse starved");
    			StarveCount++;
    			MoveCount = 0;
    			System.out.println("\n Start Over by making a new array");
    		}
    		for(int i=0; i<island.length; ++i)
    			for(int j=0; j<island[i].length; ++j)
    				if(MoveCount == 0 && island[i][j] == 1)
    				{
    					temp[i][j] = island[i][j+1]; 
    					MoveCount++;
    					if(temp[i][j] == -1)
    					{
    						System.out.println("The mouse drowned after moving "+MoveCount+" times.");
    						DrownCount++;
    						MoveCount = 0;
    						System.out.println("\n Start Over by making a new array");
    						break;
    					}
    					else if(temp[i][j] == 999)
    					{
    						System.out.println("The mouse found a bridge and escaped after "+MoveCount+" times.");
    						EscapeCount++;
    						MoveCount = 0;
    						System.out.println("\n Start Over by making a new array");
    						break;
    					}
    					temp[i][j] = MoveCount+1;
    					mouse = temp[i][j];
    				}	
    				else if(island[i][j]==mouse && island[i][j]!=999)
    				{
    					temp[i][j] = island[i][j+1];
    					MoveCount++;
    					if(temp[i][j] == -1)
    					{
    						System.out.println("The mouse drowned after moving "+MoveCount+" times.");
    						DrownCount++;
    						MoveCount = 0;
    						System.out.println("\n Start Over by making a new array");
    						break;
    					}
    					else if(temp[i][j] == 999)
    					{
    						System.out.println("The mouse found a bridge and escaped after "+MoveCount+" times.");
    						EscapeCount++;
    						MoveCount = 0;
    						System.out.println("\n Start Over by making a new array");
    						break;
    					}
    					temp[i][j] = MoveCount+1;
    					mouse = temp[i][j];
    				}	
    		island = temp;
    	}
    	public void MoveUp()
    	{
    		int[][] temp = island;
    		if(MoveCount == 100)
    		{
    			System.out.println("The mouse starved");
    			StarveCount++;
    			MoveCount = 0;
    			System.out.println("\n Start Over by making a new array");
    		}
    		for(int i=0; i<island.length; ++i)
    			for(int j=0; j<island[i].length; ++j)
    				if(MoveCount == 0 && island[i][j] == 1)
    				{
    					temp[i][j] = island[i-1][j]; 
    					MoveCount++;
    					if(temp[i][j] == -1)
    					{
    						System.out.println("The mouse drowned after moving "+MoveCount+" times.");
    						DrownCount++;
    						MoveCount = 0;
    						System.out.println("\n Start Over by making a new array");
    						break;
    					}
    					else if(temp[i][j] == 999)
    					{
    						System.out.println("The mouse found a bridge and escaped after "+MoveCount+" times.");
    						EscapeCount++;
    						MoveCount = 0;
    						System.out.println("\n Start Over by making a new array");
    						break;
    					}
    					temp[i][j] = MoveCount+1;
    					mouse = temp[i][j];
    				}	
    				else if(island[i][j]==mouse && island[i][j]!=999)
    				{
    					temp[i][j] = island[i-1][j];
    					MoveCount++;
    					if(temp[i][j] == -1)
    					{
    						System.out.println("The mouse drowned after moving "+MoveCount+" times.");
    						DrownCount++;
    						MoveCount = 0;
    						System.out.println("\n Start Over by making a new array");
    						break;
    					}
    					else if(temp[i][j] == 999)
    					{
    						System.out.println("The mouse found a bridge and escaped after "+MoveCount+" times.");
    						EscapeCount++;
    						MoveCount = 0;
    						System.out.println("\n Start Over by making a new array");
    						break;
    					}
    					temp[i][j] = MoveCount+1;
    					mouse = temp[i][j];
    				}	
    		island = temp;
    	}
    	public void MoveDown()
    	{
    		int[][] temp = island;
    		if(MoveCount == 100)
    		{
    			System.out.println("The mouse starved");
    			StarveCount++;
    			MoveCount = 0;
    			System.out.println("\n Start Over by making a new array");
    		}
    		for(int i=0; i<island.length; ++i)
    			for(int j=0; j<island[i].length; ++j)
    				if(MoveCount == 0 && island[i][j] == 1)
    				{
    					temp[i][j] = island[i+1][j]; 
    					MoveCount++;
    					if(temp[i][j] == -1)
    					{
    						System.out.println("The mouse drowned after moving "+MoveCount+" times.");
    						DrownCount++;
    						MoveCount = 0;
    						System.out.println("\n Start Over by making a new array");
    						break;
    					}
    					else if(temp[i][j] == 999)
    					{
    						System.out.println("The mouse found a bridge and escaped after "+MoveCount+" times.");
    						EscapeCount++;
    						MoveCount = 0;
    						System.out.println("\n Start Over by making a new array");
    						break;
    					}
    					temp[i][j] = MoveCount+1;
    					mouse = temp[i][j];
    				}	
    				else if(island[i][j]==mouse && island[i][j]!=999)
    				{
    					temp[i][j] = island[i+1][j];
    					MoveCount++;
    					if(temp[i][j] == -1)
    					{
    						System.out.println("The mouse drowned after moving "+MoveCount+" times.");
    						DrownCount++;
    						MoveCount = 0;
    						System.out.println("\n Start Over by making a new array");
    						break;
    					}
    					else if(temp[i][j] == 999)
    					{
    						System.out.println("The mouse found a bridge and escaped after "+MoveCount+" times.");
    						EscapeCount++;
    						MoveCount = 0;
    						System.out.println("\n Start Over by making a new array");
    						break;
    					}
    					temp[i][j] = MoveCount+1;
    					mouse = temp[i][j];
    				}	
    		island = temp;
    	}
    	public void RandomMove(int z)
    	{	
    		for(int i=0; i<z; i++)
    		{
    			int randomNumber = 1+(int)(Math.random()*(4));
    			if(randomNumber == 1);
    			{
    				MoveUp();
    				System.out.println("The Mouse moved up");
    			}	
    			if(randomNumber == 2);
    			{
    				MoveDown();
    				System.out.println("The Mouse moved down");
    			}
    			if(randomNumber == 3);
    			{
    				MoveLeft();
    				System.out.println("The Mouse moved left");
    			}
    			if(randomNumber == 4);
    			{
    				MoveRight();
    				System.out.println("The Mouse moved right");
    			}
    		}	
    	}
    	public String printArray()
    	{
    		String output = "";
    		for(int i=0; i<island.length; ++i)
    		{
    			for(int j=0; j<island[i].length; ++j)
    				output += " "+island[i][j]+"\t";
    			output+="\n";
    		}
    		return output;
    	}
    	public String printStats()
    	{
    		return "The mouse starved this many times: "+StarveCount+
    				"\nThe mouse escaped this many times: "+EscapeCount+
    				"\nThe mouse drowned this many times: "+DrownCount;
    	}
    }
    Here is my second class

    Java Code:
    import java.util.Scanner;
    public class Project2
    {
    	private static Scanner scan = new Scanner(System.in);
    	private static MouseIsland island;
    	public static void main(String[] args)
    	{
    		printMenu();
    		int choice = scan.nextInt();
    		while(choice!= 0)
    		{
    			dispatch(choice);
    			printMenu();
    			choice = scan.nextInt();
    		}
    		System.out.println("Bye!");
    		System.out.println("Coded by Colby Porta");
    	}
    	public static void dispatch(int choice)
    	{
    		int loc;
    		switch(choice)
    		{
    			case 0:
    				break;
    			case 1:
    				System.out.println("Enter a number between 0 and 11");
    				int size = scan.nextInt();
    				if(size <=0 || size >10)
    				{
    					System.out.println("That number was not in the range");
    					break;
    				}
    				else
    					island = new MouseIsland(size);
    				break;
    			case 2:
    				System.out.println("Pick where the 1st bridge will go using the format: 'x y'");
    				int x = scan.nextInt(), y = scan.nextInt();
    				island.PlaceBridge(x, y);
    				System.out.println("Pick where the 2nd bridge will go using the format: 'x y'");
    				x = scan.nextInt();
    				y = scan.nextInt();
    				island.PlaceBridge(x, y);
    				break;
    			case 3:
    				System.out.println("Pick where the mouse will go using the format: 'x y'");
    				x = scan.nextInt(); y = scan.nextInt();
    				island.PlaceMouse(x, y);
    				break;
    			case 4:
    				island.MoveUp();
    				break;
    			case 5:
    				island.MoveDown();
    				break;
    			case 6:
    				island.MoveLeft();
    				break;
    			case 7:
    				island.MoveRight();
    				break;
    			case 8:
    				System.out.println("Enter an integer to tell how many times to randomly move");
    				x = scan.nextInt(); 
    				island.RandomMove(x);
    				break;
    			case 9:
    				System.out.print(island.printArray());
    				break;
    			case 10:
    				System.out.print(island.printStats());
    				break;
    			default:
    				System.out.println("Sorry, invalid choice");	
    		}
    	}
    	public static void printMenu()
    	{
    		System.out.println("\n	Menu	");
    		System.out.println("		====	");
    		System.out.println("0:	Quit");
    		System.out.println("1:	Initialize a new Square Array");
    		System.out.println("2:	Choose where the two bridges go in 'x y' format");
    		System.out.println("3:	Choose where the mouse will go in 'x y' format");
    		System.out.println("4:	Move the Mouse Up");
    		System.out.println("5:	Move the Mouse Down");
    		System.out.println("6:	Move the Mouse Left");
    		System.out.println("7:	Move the Mouse Right");
    		System.out.println("8:	Randomly move the mouse Left, Right, Up, or Down 'x' amount of times");
    		System.out.println("9:	Print the Array");
    		System.out.println("10:	Print the Total amount of Drowns, Escapes, and Deaths by Starvation");
    		System.out.println("\n Enter your choice:	");
    	}
    }

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Java Code:
    int[][] temp = island;
    what will happen to island if temp is changed?

  3. #3
    Join Date
    Apr 2010
    Posts
    8
    Rep Power
    0

    Default

    I honestly don't know. I don't know if temp and island are pointing to the same array or if changing temp automatically changes island... Do you know and you are just trying to make me think, or were you truly asking?

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    You answered correctly, changing temp will change island, keep thinking and keep trying DO NOT GIVE UP you have good instincts.

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

    Default

    Qualify that one. using temp[x][y]=blah will change island, using temp=newarray will NOT change island.

    You're probably having trouble because temp[i][j]=island[i+1][j] and other things like it change the island array as well. Try writing a method that copies the array using Arrays.copy and returns a COPY of the island array, then do temp = arrayCopyFunc(). If you copy it that way, instead of copying the array handle, temp[i][j]=island[i+1][j] will NOT change the island array. Should help :)

    Also, the RandomMove function would be having trouble because 1+(int)(Math.random()*(4)) should probably be 1+(int)(Math.random()%(4)). Your multiplying by four, instead, you want to get a number less than four, which the modulo (%) operator will give you. (Modulo gives the remainder of a divison, which is always less than the number you are dividing by)

    Small critique: Methods and variables should not start with capitals. Makes them look like class names, and occasionally it looks like you are calling static methods. i.e. MoveCount should be moveCount, RandomMove should be randomMove, etc. Simple conventions, but they make your code easier for us to read. Also, why add one onto the random number. You're adding at least 1, probably 2 or 3 instructions to the assembly code for the expression. Not really a lot in the grand scheme of things, but you really need to get used to working off indexes starting with zero, and the expression looks odd, to me at least.
    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. Why doesn't my program function correctly?
    By robertbob in forum New To Java
    Replies: 7
    Last Post: 05-06-2010, 02:03 AM
  2. Did I do this THREAD correctly?
    By TimHuey in forum New To Java
    Replies: 4
    Last Post: 04-24-2010, 06:54 AM
  3. how to scale correctly ?
    By h9h in forum Java 2D
    Replies: 10
    Last Post: 10-29-2009, 08:06 AM
  4. If statement not executing correctly
    By gligor_kot in forum New To Java
    Replies: 5
    Last Post: 08-03-2009, 02:46 AM
  5. [SOLVED] \t not working correctly?
    By Gakusei in forum New To Java
    Replies: 5
    Last Post: 05-06-2008, 05:45 PM

Posting Permissions

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