Tutorials to .com

Tutorials to .com » Jsp » Skills » The preparation of mine clearance with Java games - code for Thought

The preparation of mine clearance with Java games - code for Thought

Print View , by: iSee ,Total views: 18 ,Word Count: 2227 ,Date: Mon, 24 Aug 2009 Time: 10:06 AM

import java.awt .*;
import java.awt.event .*;
import javax.swing .*;

/ * Button class * /

class Bomb extends JButton
(

public int num_x, num_y; / / the first of several box No.
public int BombRoundCount; / / around the mine a few
public boolean isBomb; / / whether the mine
public boolean isClicked; / / if clicked
public int BombFlag; / / mine detection marker
public boolean isRight; / / if right-click

public Bomb (int x, int y)
(
BombFlag = 0;
num_x = x;
num_y = y;
BombRoundCount = 0;
isBomb = false;
isClicked = false;
isRight = false;
)
)
/ * Window and algorithm implementation class * /

class MainBomb extends JFrame implements ActionListener, MouseListener
(

public JTextField text;
public Label nowBomb, setBomb;
public int BlockNum, BombNum; / / the current number box the number of the current mine
public Icon icon_bomb = new ImageIcon ( "Bomb.gif"); / / step on mine
public Icon icon_bomb_big = new ImageIcon ( "bomb_big.gif"); / / step on mine tags
public Icon icon_flag = new ImageIcon ( "flag.gif"); / / Ray tag
public Icon icon_question = new ImageIcon ( "question.gif"); / / wondering whether there is mine
public JButton start = new JButton ( "Start");
public Panel MenuPamel = new Panel ();
public Panel mainPanel = new Panel ();
public Bomb [] [] bombButton;

/ * interface Design * /

public MainBomb ()
(
super ( "Mine Aaron2004 produced 2004.8");
BlockNum = 64;
BombNum = 10;
Container c = getContentPane ();
c.setBackground (Color.gray);
c.setLayout (new BorderLayout ());
text = new JTextField ( "10", 3);
nowBomb = new Label ( "Number of the current mine" + "" + BombNum +"");
setBomb = new Label ( "Set the number of mines");
start.addActionListener (new ActionListener () (
public void actionPerformed (ActionEvent e)
(
BombNum = Integer.parseInt (text.getText (). Trim ());
if (BombNum> = 10 & & BombNum <50)
replay ();
else
(
JOptionPane msg = new JOptionPane ();
JOptionPane.showMessageDialog (null, "you set the number of mines are too many, please reset!", "Error", 2);
)

)
));
MenuPamel.add (setBomb);
MenuPamel.add (text);
MenuPamel.add (start);
MenuPamel.add (nowBomb);
c.add (MenuPamel, "North");

mainPanel.setLayout (new GridLayout ((int) Math.sqrt (BlockNum), (int) Math.sqrt (BlockNum)));
bombButton = new Bomb [(int) Math.sqrt (BlockNum)] [];
for (int i = 0; i <(int) Math.sqrt (BlockNum); i + +)
(
bombButton [i] = new Bomb [(int) Math.sqrt (BlockNum)];
)
for (int i = 0; i <(int) Math.sqrt (BlockNum); i + +)
for (int j = 0; j <(int) Math.sqrt (BlockNum); j + +)
(
bombButton [i] [j] = new Bomb (i, j);
bombButton [i] [j]. setForeground (Color.gray);
bombButton [i] [j]. addActionListener (this);
bombButton [i] [j]. addMouseListener (this);
)
for (int i = 0; i <(int) Math.sqrt (BlockNum); i + +)
for (int j = 0; j <(int) Math.sqrt (BlockNum); j + +)
mainPanel.add (bombButton [i] [j]);
c.add (mainPanel, "Center");
startBomb ();
setSize (400,400);
setLocation (350,200);
setResizable (false);
)

/ * Bray * /

public void startBomb ()
(

for (int i = 0; i <BombNum; i + +)
(
int x = (int) (Math.random () * (int) (Math.sqrt (BlockNum) -1));
int y = (int) (Math.random () * (int) (Math.sqrt (BlockNum) -1));

if (bombButton [x] [y]. isBomb == true)
i -;
else
bombButton [x] [y]. isBomb = true;
)
)

/ * Start again * /

public void replay ()
(
nowBomb.setText ( "Current mine a few" + "" + BombNum +"");
for (int i = 0; i <(int) Math.sqrt (BlockNum); i + +)
for (int j = 0; j <(int) Math.sqrt (BlockNum); j + +)
(
bombButton [i] [j]. isBomb = false;
bombButton [i] [j]. isClicked = false;
bombButton [i] [j]. setEnabled (true);
bombButton [i] [j]. setText ("");
bombButton [i] [j]. setIcon (null);
)
startBomb ();
)

/ * If all the digging finished mine * /

public void isWin ()
(
int findBomb = 0; / / number of mines found

for (int i = 0; i <(int) Math.sqrt (BlockNum); i + +)
for (int j = 0; j <(int) Math.sqrt (BlockNum); j + +)
(
if (bombButton [i] [j]. isBomb == true & & bombButton [i] [j]. isRight == true)
findBomb + +;
)
if (findBomb == Integer.parseInt (text.getText (). trim ()))
(
JOptionPane msg = new JOptionPane ();
JOptionPane.showMessageDialog (this, "you are finished digging all of mine, you won!", "You won", 2);
)
)

/ * Calculate the number of boxes around the mine * /

public void CountRoundBomb ()
(
for (int i = 0; i <(int) Math.sqrt (BlockNum); i + +) (
for (int j = 0; j <(int) Math.sqrt (BlockNum); j + +) (
int count = 0;
/ / When you need to detect the cell itself, a mine-free circumstances, the statistics surrounding the number of mines
if (bombButton [i] [j]. isBomb! = true) (
if ((i - 1> = 0) & & (j - 1> = 0)) (
if (bombButton [i - 1] [j - 1]. isBomb == true) (
count + = 1; / / test whether it is mine the upper left space
)
)
if ((i - 1> = 0)) (
if (bombButton [i - 1] [j]. isBomb == true) (
count + = 1; / / test whether a space at the top of mine
)
)
if ((i - 1> = 0) & & (j + 1 <= (int) Math.sqrt (BlockNum) -1)) (
if (bombButton [i - 1] [j + 1]. isBomb == true) (
count + = 1; / / test whether the top right of mine
)
)
if ((j - 1> = 0)) (
if (bombButton [i] [j - 1]. isBomb == true) (
count + = 1; / / test whether the mine on the left
)
)
if ((i> = 0) & & (j + 1 <= (int) Math.sqrt (BlockNum) -1)) (
if (bombButton [i] [j + 1]. isBomb == true) (
count + = 1; / / right
)
)
if ((j - 1> = 0) & & (i + 1 <= (int) Math.sqrt (BlockNum) -1)) (
if (bombButton [i + 1] [j - 1]. isBomb == true) (
count + = 1; / / bottom left
)
)
if ((i + 1 <= (int) Math.sqrt (BlockNum) -1)) (
if (bombButton [i + 1] [j]. isBomb == true) (
count + = 1; / / Next
)
)
if ((j + 1 <= (int) Math.sqrt (BlockNum) -1) & & (i + 1 <= Math.sqrt (BlockNum) -1)) (
if (bombButton [i + 1] [j + 1]. isBomb == true) (
count + = 1; / / bottom right
)
)
bombButton [i] [j]. BombRoundCount = count;
)
)
)
)

/ ** Elected to the position is empty, then turn around the map ** /

public void isNull (Bomb [] [] bombButton, Bomb ClickecButton)
(
int i, j;
i = ClickecButton.num_x;
j = ClickecButton.num_y;

if (ClickecButton.isBomb == true) (

)
else (

if ((i - 1> = 0) & & (j - 1> = 0)) (/ / test whether the empty spaces in the upper left
if (bombButton [i - 1] [j - 1]. isBomb == false & & bombButton [i - 1] [j - 1]. isClicked == false & & bombButton [i - 1] [j - 1]. isRight = = false) (
bombButton [i - 1] [j - 1]. setText ((bombButton [i - 1] [j - 1]. BombRoundCount )+"");
bombButton [i - 1] [j - 1]. setEnabled (false);
bombButton [i - 1] [j - 1]. isClicked = true;
)
)

if ((i - 1> = 0)) (/ / test whether the empty space above
if (bombButton [i - 1] [j]. isBomb == false & & bombButton [i - 1] [j]. isClicked == false & & bombButton [i - 1] [j]. isRight == false) (
bombButton [i - 1] [j]. setText ((bombButton [i - 1] [j]. BombRoundCount )+"");
bombButton [i - 1] [j]. setEnabled (false);
bombButton [i - 1] [j]. isClicked = true;
)
)
if ((i - 1> = 0) & & (j + 1 <= ((int) Math.sqrt (BlockNum) -1))) (/ / test whether the upper right is empty
if (bombButton [i - 1] [j + 1]. isBomb == false & & bombButton [i - 1] [j + 1]. isClicked == false & & bombButton [i - 1] [j + 1]. isRight = = false) (
bombButton [i - 1] [j + 1]. setText ((bombButton [i - 1] [j + 1]. BombRoundCount )+"");
bombButton [i - 1] [j + 1]. setEnabled (false);
bombButton [i - 1] [j + 1]. isClicked = true;
)

)
if ((j - 1> = 0)) (/ / test on the left is empty
if (bombButton [i] [j - 1]. isBomb == false & & bombButton [i] [j - 1]. isClicked == false & & bombButton [i] [j - 1]. isRight == false) (
bombButton [i] [j - 1]. setText ((bombButton [i] [j - 1]. BombRoundCount )+"");
bombButton [i] [j - 1]. setEnabled (false);
bombButton [i] [j - 1]. isClicked = true;
)

)
if ((i> = 0) & & (j + 1 <= ((int) Math.sqrt (BlockNum) -1))) (/ / test whether the right is empty space
if (bombButton [i] [j + 1]. isBomb == false & & bombButton [i] [j + 1]. isClicked == false & & bombButton [i] [j + 1]. isRight == false) (
bombButton [i] [j + 1]. setText ((bombButton [i] [j + 1]. BombRoundCount )+"");
bombButton [i] [j + 1]. setEnabled (false);
bombButton [i] [j + 1]. isClicked = true;
)
)
if ((j - 1> = 0) & & (i + 1 <= ((int) Math.sqrt (BlockNum) -1))) (/ / test whether the lower left box is empty
if (bombButton [i + 1] [j - 1]. isBomb == false & & bombButton [i + 1] [j - 1]. isClicked == false & & bombButton [i + 1] [j - 1]. isRight = = false) (
bombButton [i + 1] [j - 1]. setText ((bombButton [i + 1] [j - 1]. BombRoundCount )+"");
bombButton [i + 1] [j - 1]. setEnabled (false);
bombButton [i + 1] [j - 1]. isClicked = true;
)
)
if ((i + 1 <= ((int) Math.sqrt (BlockNum) -1))) (/ / test whether it is empty space below
if (bombButton [i + 1] [j]. isBomb == false & & bombButton [i + 1] [j]. isClicked == false & & bombButton [i + 1] [j]. isRight == false) (
bombButton [i + 1] [j]. setText ((bombButton [i + 1] [j]. BombRoundCount )+"");
bombButton [i + 1] [j]. setEnabled (false);
bombButton [i + 1] [j]. isClicked = true;
)
)
if ((j + 1 <= ((int) Math.sqrt (BlockNum) -1)) & & (i + 1 <= ((int) Math.sqrt (BlockNum) -1))) (/ / test right below whether it is empty space
if (bombButton [i + 1] [j + 1]. isBomb == false & & bombButton [i + 1] [j + 1]. isClicked == false & & bombButton [i + 1] [j + 1]. isRight = = false) (
bombButton [i + 1] [j + 1]. setText ((bombButton [i + 1] [j + 1]. BombRoundCount )+"");
bombButton [i + 1] [j + 1]. setEnabled (false);
bombButton [i + 1] [j + 1]. isClicked = true;
)
)
if ((i - 1> = 0) & & (j - 1> = 0)) / / test left
isNull (bombButton, bombButton [i - 1] [j - 1]);
if ((i - 1> = 0))
isNull (bombButton, bombButton [i - 1] [j ]);// detect the top of the
if ((i - 1> = 0) & & (j + 1 <= (int) Math.sqrt (BlockNum) -1))
isNull (bombButton, bombButton [i - 1] [j + 1 ]);// test the upper-right
if ((j - 1> = 0))
isNull (bombButton, bombButton [i] [j - 1 ]);// detected on the left
if ((i> = 0) & & (j + 1 <= ((int) Math.sqrt (BlockNum) -1)))
isNull (bombButton, bombButton [i] [j + 1 ]);// test on the right
if ((j - 1> = 0) & & (i + 1 <= ((int) Math.sqrt (BlockNum) -1)))
isNull (bombButton, bombButton [i + 1] [j - 1]); / / detection of the lower-left
if ((i + 1 <= ((int) Math.sqrt (BlockNum) -1))) / / test the next
isNull (bombButton, bombButton [i + 1] [j]);
if ((j + 1 <= ((int) Math.sqrt (BlockNum) -1)) & & (i + 1 <= ((int) Math.sqrt (BlockNum) -1))) / / test the lower right
isNull (bombButton, bombButton [i + 1] [j + 1]);

)
)

public void actionPerformed (ActionEvent e)
(

CountRoundBomb ();

if (((Bomb) e.getSource ()). isBomb == false & & ((Bomb) e.getSource ()). isClicked == false)
(
((Bomb) e.getSource ()). SetText ((((Bomb) e.getSource ()). BombRoundCount )+"");
((Bomb) e.getSource ()). IsClicked = true;
((Bomb) e.getSource ()). SetIcon (null);
((Bomb) e.getSource ()). SetEnabled (false);
if ((((Bomb) e.getSource ()). BombRoundCount) == 0)
isNull (bombButton, (Bomb) e.getSource ());
isWin ();
)
else if (((Bomb) e.getSource ()). isBomb == true)
(

for (int i = 0; i <(int) Math.sqrt (BlockNum); i + +)
for (int j = 0; j <(int) Math.sqrt (BlockNum); j + +)
(
if (bombButton [i] [j]. isBomb == true)
bombButton [i] [j]. setIcon (icon_bomb);
)

((Bomb) e.getSource ()). SetIcon (icon_bomb_big);

JOptionPane msg = new JOptionPane ();
JOptionPane.showMessageDialog (this, "you have stepped on landmines, and press OK over again," "You stepped on a landmine", 2);
replay ();
)
)

public void mouseClicked (MouseEvent e)
(
Bomb bombSource = (Bomb) e.getSource ();
boolean right = SwingUtilities.isRightMouseButton (e);

if ((right == true) & & (bombSource.isClicked == false))
(
bombSource.BombFlag = (bombSource.BombFlag + 1)% 3;
if (bombSource.BombFlag == 1)
(

if (BombNum> 0 & & bombSource.isRight == false) (
bombSource.setIcon (icon_flag);
bombSource.isRight = true;
BombNum -;
)
isWin ();
nowBomb.setText ( "Current mine a few" + "" + BombNum +"");
)
else if (bombSource.BombFlag == 2)
(

if ((BombNum! = 0) | | (BombNum == 0 & & (bombSource.getIcon () == icon_flag)))
BombNum + +;
bombSource.setIcon (icon_question);
nowBomb.setText ( "Current mine a few" + "" + BombNum +"");
)
else if (bombSource.BombFlag == 0)
(
bombSource.setIcon (null);
bombSource.isRight = false;
)
)
)

public void mouseEntered (MouseEvent e)
()
public void mouseReleased (MouseEvent e)
()
public void mouseExited (MouseEvent e)
()
public void mousePressed (MouseEvent e)
()
)

/ * Main class * /

public class Main
(
public static void main (String args [])
(
(new MainBomb ()). show ();

)
)



Java skills and code Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.