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.
Code :
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();
}
}
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.
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
Re: Seeking help with minesweeper style program.
Quote:
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.
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.
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.
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.