Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Seeking help with minesweeper style program.

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Seeking help with minesweeper style program.

    Alright I was assigned to make a program very similar to mine sweeper. I developed a 10*10 grid, I have a random int for Column and a random int for row. The program tells you where you clicked although it seems a little off(example: you click in the square 5,2 it tells you 5,3.) My question is i've set a value for mouseX and mouseY to draw the you win but no matter where i click the string isn't working. Any general advice would help me on where to go from here. I've been messing around with it but some advice from more practiced hands is appreciated.
    import java.awt.*;
    import java.applet.Applet;
    import java.awt.event.*;
     
    public class bunnyfoofoostart extends Applet implements MouseListener, KeyListener
    {
     
    		int mouseX;
    		int mouseY;
    		int secretcol=(int)(Math.random()*900+101);
    		int secretrow=(int)(Math.random()*900+101);
     
    	public void init()
    	{
    		addMouseListener(this);
    		addKeyListener(this);
    	}
     
    	public void paint(Graphics g)
    	{
     
    		for(int col=1; col<=10; col++)
    		{
    			for(int row=1; row<=10; row++)
    			{
    				int x=col*50;
    				int y= row*50;
    				g.setColor(Color.green);
    				g.fillRect(x,y,25,25);
     
    			}
    		}
    		if (mouseX !=0|| mouseY !=0)
    	g.drawString("You clicked at"+mouseX/50+","+ (1+mouseY/50),600,200);
    		if (mouseX > (secretrow-30)  && mouseX <  (secretrow + 30)&&  mouseY > (secretcol-30) && mouseY < (secretcol+30)) //win
    		{
     
    						g.setColor(Color.black);
    						g.drawString("You Win",700,600);
     
    		}
    		if (mouseX>600 && mouseY<200)
    		{
    			System.out.println("Secret col is"+secretcol/100);
    			System.out.println("Secret row is"+secretrow/100);
    		}
     
    	}
     
    	public void mouseExited(MouseEvent me)
    	{
    	}
     
    	public void mouseEntered(MouseEvent me)
    	{
    	}
    	public void mousePressed(MouseEvent me)
    	{
    	}
    	public void mouseReleased(MouseEvent me)
    	{
    	}
    	public void mouseClicked(MouseEvent me)
    	{
    		mouseX=me.getX();
    		mouseY=me.getY();
    		repaint();
    	}
     
    	public void keyPressed(KeyEvent ke)
    	{
    	}
    	public void keyReleased(KeyEvent ke)
    	{
    	}
    	public void keyTyped(KeyEvent ke)
    	{
     
    		repaint();
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Seeking help with minesweeper style program.

    Try debugging the code by adding printlns to show the values of the variables as the program executes. For example show the x,y of the mouse click and the secret values and the x,y values

    Take a piece of graph paper and draw the grid. Map the x,y locations of each of the squares.
    Then figure out the formula you need to compute each square's location given the location of the mouse click.
    Last edited by Norm; February 25th, 2012 at 08:00 AM.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Seeking help with minesweeper style program.

    I've got printout's to see the secret ints and where the mouse was clicked but i'm stilling have trouble selecting a specific square. Any advice is or guidance where to go would be great! Thank You

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Seeking help with minesweeper style program.

    have trouble selecting a specific square
    Can you explain what the problem is?

    Do you have the x,y and width,height of each square?
    Do you have the x,y of where the mouse was clicked?
    Given those data points, you should be able to compute which square was clicked on.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Seeking help with minesweeper style program.

    I'm having trouble explaining it clearly i guess. Could you copy and paste the code in and just run it to see what I'm trying to explain. Appreciate your help, wish i was being a little clearer.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Seeking help with minesweeper style program.

    I have executed your code and see that your computations are wrong. You need to redo the logic and come up with some new forumulas for computing the values you want.

    Do you have the x,y and width,height of each square?

    Do you have the x,y of where the mouse was clicked?

    For example if a square is at 20,20 with a width and height of 10 and the mouse clicks at 25, 22 then is that click inside of that square?

    You need to work out the logic for this using a piece of paper and pencil. Draw the squares and mark all of their x,y locations.
    Then given a mouse's click at any x,y you should know which square the click was in.

  7. The Following User Says Thank You to Norm For This Useful Post:

    Gravs2889 (February 25th, 2012)

  8. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Seeking help with minesweeper style program.

    Alright I will draw it all out and see if I can figure it out from there. Thank you very much for your help.

Similar Threads

  1. Beginner seeking help with quick problem in program
    By Gravs2889 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: February 23rd, 2012, 11:59 PM
  2. GUI minesweeper help
    By naitomea4664 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 23rd, 2012, 08:33 PM
  3. Replies: 6
    Last Post: February 21st, 2012, 09:41 PM
  4. NetBeans-style GUI
    By susieferrari in forum Java Theory & Questions
    Replies: 7
    Last Post: March 31st, 2011, 09:40 AM
  5. confused and seeking advice
    By mrgreaper in forum Java Theory & Questions
    Replies: 11
    Last Post: January 1st, 2011, 07:10 AM