Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    java minesweep game

    why does this program not end?

    Code:
    import java.io.*;
    
    public class MineSweep1
    {
    	static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    	static private Bomb[] bmbArry = new Bomb[3];
    	static private char matrix[][];
    
    	public static void main(String[] args) throws IOException
    	{
    		char x,y;
    		matrix = new char[5][5];
    
    		for(int z = 0; z < 3; z++)
    			bmbArry[z] = new Bomb();
    		boolean explode;
    		explode = false;
    
    		for(x=0;x<5;x++)
    			for(y=0;y<5;y++)
    			matrix[x][y] = '*';
    			System.out.println("");
    
    		while (! (explode))
    		{
    			for(x=0;x<5;x++)
    			{
    			   for(y=0;y<5;y++)
    			      System.out.print(matrix[x][y]+" ");
    			System.out.println("");
    			}
    			explode = GetUserInput();
    		}
    	}
    
    	static boolean GetUserInput() throws IOException
    	{
    		int userX, userY;
    		System.out.println("Please enter an X position");
    		userX = Integer.parseInt(input.readLine());
    		System.out.println("Please enter an Y position");
    		userY = Integer.parseInt(input.readLine());
    
    		return IsItABomb(userX, userY);
    	}
    
    	static boolean IsItABomb(int X, int Y) throws IOException
    	{
    		for(int x=0;x<bmbArry.length;x++)
    		{
    		if((bmbArry[x].GetXpos() == X) && (bmbArry[x].GetYpos() == Y))
    
    			return GetUserInput();
    		}
    	return false;
    
    	}
    }
    when i run it and i hit a bomb, it is supposed to say "press any key to continue...." but it doesnt say that ever.

  • #2
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you have a loop there that will never say there is a bomb...when your program should return true for IsItABomb you just call getUserInput again and that goes back and calls IsItABomb again...there is no "return true;" for a bomb...


    Jason


  •  

    Posting Permissions

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