Java: |
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
public class Minesweeper
{
public static void main (String[] args )
{
JFrame j = new JFrame ();
j. setDefaultCloseOperation (JFrame. EXIT_ON_CLOSE);
j. getContentPane (). add (new MinesweeperPanel (), BorderLayout. CENTER);
j. setSize (1025, 745);
j. setVisible (true);
}
}
class MinesweeperPanel extends JPanel implements MouseListener
{
TroysGrid2 grid;
int rows, columns;
int[] [] game;
boolean[] [] clicked;
Mine m;
Font font1;
boolean lose, win;
public MinesweeperPanel ()
{
setBackground (Color. black);
font1 = new Font ("Arial", 1, 77);
rows = 3;
columns = 4;
game = new int [rows + 2] [columns + 2];
clicked = new boolean [rows + 2] [columns + 2];
initMinesweeper ();
debug ();
m = new Mine ();
grid = new TroysGrid2 ();
grid. setDimensions (rows, columns );
grid. setCellSize (70);
grid. setPosition (55, 55);
grid. setLineThickness (5);
grid. setFillColor (Color. gray);
grid. setColor (Color. white);
addMouseListener (this);
}
public void paintComponent (Graphics g )
{
super. paintComponent (g );
grid. draw (g );
for (int r = 1 ; r <= rows ; r++ )
{
for (int c = 1 ; c <= columns ; c++ )
{
if (clicked [r ] [c ] == true)
{
if (game [r ] [c ] == 9) //if position has a 9 its a mine
{
m. setColor (Color. red);
m. setPosition (grid. getCellX (c ), grid. getCellY (r ));
m. setHeight (70);
m. draw (g );
}
else
{
g. setColor (Color. black);
g. setFont (font1 );
g. drawString ("" + game [r ] [c ], grid. getCellX (c ), grid. getCellBottomY (r ));
}
}
}
}
if (lose == false)
{
System. out. println ("win");
}
if (win == false)
{
System. out. println ("lose");
}
if (win == true)
{
Font f = new Font ("Arial", 1, 88);
g. setFont (f );
g. setColor (Color. blue);
g. drawString ("You Win", 0, 88);
}
if (lose == true)
{
Font f = new Font ("Arial", 1, 88);
g. setFont (f );
g. setColor (Color. green);
g. drawString ("You Lose", 0, 88);
}
requestFocus ();
}
// To fulfill our obligations as a MouseListner, we implement the following...
public void mouseClicked (MouseEvent e )
{
if (lose == false && win == false)
{
int mRow = grid. getRow (e. getY ());
int mCol = grid. getColumn (e. getX ());
clicked [mRow ] [mCol ] = true;
//determine if we lose
if (game [mRow ] [mCol ] == 9)
{
lose = true;
}
//determine if we win
int count = 0;
for (int r = 1 ; r <= rows ; r++ )
{
for (int c = 1 ; c <= columns ; c++ )
{
if (clicked [r ] [c ] == true)
{
count++;
}
}
}
if (count == 9)
{
win = true;
}
}
debug ();
repaint ();
}
public void mouseEntered (MouseEvent e )
{
}
public void mousePressed (MouseEvent e )
{
}
public void mouseReleased (MouseEvent e )
{
}
public void mouseExited (MouseEvent e )
{
}
public void debug ()
{
System. out. println ("Contents of the game array:");
for (int r = 1 ; r <= rows ; r++ )
{
for (int c = 1 ; c <= columns ; c++ )
{
System. out. print (game [r ] [c ] + " ");
}
System. out. println ();
}
System. out. println ();
System. out. println ("Contents of the clicked array:");
for (int r = 1 ; r <= rows ; r++ )
{
for (int c = 1 ; c <= columns ; c++ )
{
System. out. print (clicked [r ] [c ] + " ");
}
System. out. println ();
}
System. out. println ();
}
public void initMinesweeper ()
{
}
|
in the initMinesweeper i have to do these things:
// 1. Assign mines to positions in the grid
// 1.0 repeat these steps for desired number of mines
// 1.1 random row & column
// 1.2 if the cell does not already contain a mine
// 1.3 assign mine to the cell in game array
// 2. Count the number of mines adjourning each cell and store that number
// 2.1 for all cells
// 2.2 if the cell does not contain a mine
// 2.3 count the number of mines in adjourning cells
// 2.4 store the count in the cell in the game array
can you help me a little because im stuck and dont know what to do?
Mod edit: Code tags are cool, mkay? |