SPECIFICATION
Minesweeper
is a program for single-player game. The object of the game is to clear the
minefield without detonating a mine. This is a puzzle that uses numbers to
determine where could be the mines are located or planted.
Minesweeper cannot always be solved with 100% certainty, and
may require the occasional use of probability to flag or mark the button most
likely to have a mine. In other words, one must sometimes guess to solve a
Minesweeper puzzle.
This is the Frame at
the beginning of the game. The flag or
bomb counter is shown at the north side of the window and the buttons in the minefield is also presented. It starts by guessing the
bomb location to avoid it and click
a button which bomb shouldn’t
be exist there.
A mark is to assume the button to be the
bomb. It is use to mark all the bomb when there’s nothing left to click except
the remaining bombs and finish the game.
Playing the game uses
numbers to identify the nearest bomb; the higher the numbers, the higher the
chance of locating the bomb beside the number. Use mark to avoid clicking the
button accidentally.
Clicking a bomb in the minefield would make
the bomb explode, means losing the game.
An error message will
appear that tells “You Lose”. Then the program exits after clicking OK or
pressing ENTER.
Otherwise
if not losing the game, it continues until you’ve clicked all the remaining
buttons and marked the assumed bombs. Then an Information message appears telling
“YOU WIN!”. Then the program exits after clicking OK or pressing ENTER.
The
code of our machine problem follows:
1.
import
javax.swing.*;import java.awt.*;import java.awt.event.*;
2.
public
class minesweeper extends JFrame
3.
{
4.
static
Font small1 = new Font("DIGITAL-7", 4, 40);
5.
static
int x = -41,i=0,y=100,j=0,H=0,BLANK1=0,BLANK2=0,
6.
I=0,NUM1=0,NUM4=0,BOMBS=15,BOX=25;
7.
static int [] count1=new int[40],HOLD=new
int[40],
8.
bombs
= {0,1,4,7,15,17,21,24,27,28,30,32,33,35,39};
9.
static
String mes1 = " YOU
LOSE!",mes = " YOU
WIN!";
10.
private
static final int WIDTH = 425,HEIGHT = 400;
11.
static
ImageIcon img,im,in;
12.
static
JButton [] b=new JButton[40];
13.
static
JTextField T1;
14.
static
JLabel ttle1,BG;
15.
private
static Icon B,A;
16.
public
minesweeper()
17.
{
18.
A
= new ImageIcon("BUTTON.jpg");
19.
B
= new ImageIcon("BUTTON2.jpg");
20.
for(i=0;i<40;i++){
21.
b[i]=new
JButton(A);
22.
b[i].setActionCommand(""+i);
23.
b[i].setBorder(null);
24.
}
25.
T1
= new JTextField(""+BOMBS,10);
26.
T1.setFont(small1);T1.setOpaque(false);
27.
T1.setHorizontalAlignment(SwingConstants.RIGHT);
28.
T1.setForeground(Color.green);
29.
T1.setEditable(false);T1.setBorder(null);
30.
//++++++++++++MOUSE
ADAPTER++++++++++++++++++++++++++++
31.
b[0].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
32.
if
(e.getButton() == MouseEvent.BUTTON1) {I=0;NUM4=1;check();}else
33.
if
(e.getButton() == MouseEvent.BUTTON3) {H=0;change_button();}}});
34.
b[1].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
35.
if
(e.getButton() == MouseEvent.BUTTON1) {I=1;NUM4=1;check();}else
36.
if
(e.getButton() == MouseEvent.BUTTON3) {H=1;change_button();}}});
37.
b[2].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
38.
if
(e.getButton() == MouseEvent.BUTTON1) {I=2;NUM1=1;check();}else
39.
if
(e.getButton() == MouseEvent.BUTTON3) {H=2;change_button();}}});
40.
b[3].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
41.
if
(e.getButton() == MouseEvent.BUTTON1) {I=3;NUM1=1;check();}else
42.
if
(e.getButton() == MouseEvent.BUTTON3) {H=3;change_button();}}});
43.
b[4].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
44.
if
(e.getButton() == MouseEvent.BUTTON1) {I=4;NUM4=1;check();}else
45.
if
(e.getButton() == MouseEvent.BUTTON3) {H=4;change_button();}}});
46.
b[5].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
47.
if
(e.getButton() == MouseEvent.BUTTON1) {I=5;NUM1=1;check();}else
48.
if
(e.getButton() == MouseEvent.BUTTON3) {H=5;change_button();}}});
49.
b[6].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
50.
if
(e.getButton() == MouseEvent.BUTTON1) {I=6;NUM1=1;check();}else
51.
if
(e.getButton() == MouseEvent.BUTTON3) {H=6;change_button();}}});
52.
b[7].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
53.
if
(e.getButton() == MouseEvent.BUTTON1) {I=7;NUM4=1;check();}else
54.
if
(e.getButton() == MouseEvent.BUTTON3) {H=7;change_button();}}});
55.
b[8].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
56.
if
(e.getButton() == MouseEvent.BUTTON1)
57.
{I=8;NUM1=1;check();}else
58.
if
(e.getButton() == MouseEvent.BUTTON3) {H=8;change_button();}}});
59.
b[9].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
60.
if
(e.getButton() == MouseEvent.BUTTON1) {I=9;NUM1=1;check();}else
61.
if
(e.getButton() == MouseEvent.BUTTON3) {H=9;change_button();}}});
62.
b[10].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
63.
if
(e.getButton() == MouseEvent.BUTTON1) {I=10;NUM1=1;check();}else
64.
if
(e.getButton() == MouseEvent.BUTTON3) {H=10;change_button();}}});
65.
b[11].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
66.
if
(e.getButton() == MouseEvent.BUTTON1) {I=11;NUM1=1;check();}else
67.
if
(e.getButton() == MouseEvent.BUTTON3) {H=11;change_button();}}});
68.
b[12].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
69.
if
(e.getButton() == MouseEvent.BUTTON1) {I=12;NUM1=1;check();}else
70.
if
(e.getButton() == MouseEvent.BUTTON3) {H=12;change_button();}}});
71.
b[13].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
72.
if
(e.getButton() == MouseEvent.BUTTON1) {I=13;NUM1=1;check();}else
73.
if
(e.getButton() == MouseEvent.BUTTON3) {H=13;change_button();}}});
74.
b[14].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
75.
if
(e.getButton() == MouseEvent.BUTTON1) {I=14;NUM1=1;check();}else
76.
if
(e.getButton() == MouseEvent.BUTTON3) {H=14;change_button();}}});
77.
b[15].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
78.
if
(e.getButton() == MouseEvent.BUTTON1) {I=15;NUM4=1;check();}else
79.
if
(e.getButton() == MouseEvent.BUTTON3) {
80.
H=15;change_button();}}});
81.
b[16].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
82.
if
(e.getButton() == MouseEvent.BUTTON1) {I=16;NUM1=1;check();
83.
}else
if (e.getButton() == MouseEvent.BUTTON3) {H=16;change_button();}}});
84.
b[17].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
85.
if
(e.getButton() == MouseEvent.BUTTON1) {I=17;NUM4=1;check();}else
86.
if
(e.getButton() == MouseEvent.BUTTON3) {H=17;change_button();}}});
87.
b[18].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
88.
if
(e.getButton() == MouseEvent.BUTTON1) {I=18;NUM1=1;check();}else
89.
if
(e.getButton() == MouseEvent.BUTTON3) {H=18;change_button();}}});
90.
b[19].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
91.
if
(e.getButton() == MouseEvent.BUTTON1)
92.
{I=19;NUM1=1;check();}else
93.
if
(e.getButton() == MouseEvent.BUTTON3) {H=19;change_button();}}});
94.
b[20].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
95.
if
(e.getButton() == MouseEvent.BUTTON1) {I=20;NUM1=1;check();}else
96.
if
(e.getButton() == MouseEvent.BUTTON3) {H=20;change_button();}}});
97.
b[21].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
98.
if
(e.getButton() == MouseEvent.BUTTON1)
99.
{I=21;NUM4=1;check();}else
100.
if
(e.getButton() == MouseEvent.BUTTON3) {H=21;change_button();}}});
101.
b[22].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
102.
if
(e.getButton() == MouseEvent.BUTTON1) {I=22;NUM1=1;check();}else
103.
if
(e.getButton() == MouseEvent.BUTTON3) {H=22;change_button();}}});
104.
b[23].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
105.
if
(e.getButton() == MouseEvent.BUTTON1) {I=23;NUM1=1;check();}else
106.
if
(e.getButton() == MouseEvent.BUTTON3) {H=23;change_button();}}});
107.
b[24].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
108.
if
(e.getButton() == MouseEvent.BUTTON1) {I=24;NUM4=1;check();}else
109.
if
(e.getButton() == MouseEvent.BUTTON3) {H=24;change_button();}}});
110.
b[25].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
111.
if
(e.getButton() == MouseEvent.BUTTON1) {I=25;NUM1=1;check();}else
112.
if
(e.getButton() == MouseEvent.BUTTON3) {H=25;change_button();}}});
113.
b[26].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
114.
if
(e.getButton() == MouseEvent.BUTTON1) {I=26;NUM1=1;check();}else
115.
if
(e.getButton() == MouseEvent.BUTTON3) {H=26;change_button();}}});
116.
b[27].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
117.
if
(e.getButton() == MouseEvent.BUTTON1)
118.
{I=27;NUM4=1;check();}else
119.
if
(e.getButton() == MouseEvent.BUTTON3) {H=27;change_button();}}});
120.
b[28].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
121.
if
(e.getButton() == MouseEvent.BUTTON1) {I=28;NUM4=1;check();}else
122.
if
(e.getButton() == MouseEvent.BUTTON3) {H=28;change_button();}}});
123.
b[29].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent
e) {
124.
if
(e.getButton() == MouseEvent.BUTTON1) {I=29;NUM1=1;check();}else
125.
if
(e.getButton() == MouseEvent.BUTTON3) {H=29;change_button();}}});
126.
b[30].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
127.
if
(e.getButton() == MouseEvent.BUTTON1) {I=30;NUM4=1;check();}else
128.
if
(e.getButton() == MouseEvent.BUTTON3) {H=30;change_button();}}});
129.
b[31].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
130.
if
(e.getButton() == MouseEvent.BUTTON1) {I=31;NUM1=1;check();}else
131.
if
(e.getButton() == MouseEvent.BUTTON3) {H=31;change_button();}}});
132.
b[32].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
133.
if
(e.getButton() == MouseEvent.BUTTON1) {I=32;NUM4=1;check();}else
134.
if
(e.getButton() == MouseEvent.BUTTON3) {H=32;change_button();}}});
135.
b[33].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
136.
if
(e.getButton() == MouseEvent.BUTTON1) {I=33;NUM4=1;check();}else
137.
if
(e.getButton() == MouseEvent.BUTTON3) {H=33;change_button();}}});
138.
b[34].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
139.
if
(e.getButton() == MouseEvent.BUTTON1) {I=34;NUM1=1;check();}else
140.
if
(e.getButton() == MouseEvent.BUTTON3) {H=34;change_button();}}});
141.
b[35].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
142.
if
(e.getButton() == MouseEvent.BUTTON1) {I=35;NUM4=1;check();}else
143.
if
(e.getButton() == MouseEvent.BUTTON3) {H=35;change_button();}}});
144.
b[36].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
145.
if
(e.getButton() == MouseEvent.BUTTON1) {I=36;NUM1=1;check();}else
146.
if
(e.getButton() == MouseEvent.BUTTON3) {H=36;change_button();}}});
147.
b[37].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
148.
if
(e.getButton() == MouseEvent.BUTTON1) {I=37;NUM1=1;check();}else
149.
if
(e.getButton() == MouseEvent.BUTTON3) {H=37;change_button();}}});
150.
b[38].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
151.
if
(e.getButton() == MouseEvent.BUTTON1) {I=38;NUM1=1;check();}else
152.
if
(e.getButton() == MouseEvent.BUTTON3) {H=38;change_button();}}});
153.
b[39].addMouseListener(new
MouseAdapter() {public void mouseClicked(MouseEvent e) {
154.
if
(e.getButton() == MouseEvent.BUTTON1) {I=39;NUM4=1;check();}else
155.
if
(e.getButton() == MouseEvent.BUTTON3) {H=39;change_button();}}});
156.
//+++++++++MOUSE
ADAPTER+++++++++++++++++++++
157.
im
= new ImageIcon(getClass().getResource("boom.jpg"));
158.
ttle1=
new JLabel(im);
159.
ttle1.setVisible(false);
160.
//Set
the title of the window
161.
setTitle("minesweeper");
162.
//Get
the container & create JDesktopPane
163.
Container
C = getContentPane();
164.
C.setLayout(null);
165.
C.setBackground(Color.BLACK);
166.
//add
to container
167.
C.add(T1);
168.
C.add(ttle1);
169.
for(i=0;i<40;i++){
170.
C.add(b[i]);
171.
}
172.
//setting
bounds
173.
ttle1.setBounds(14,
10, 397, 80);
174.
T1.setBounds(154,
28, 75, 40);
175.
for(i=0;i<40;i++){
176.
if(j<8){
177.
b[i].setBounds(x=x+50,
y, 50, 50);
178.
j++;
179.
if(j==8){
180.
x=-41;
181.
y=y+50;
182.
}}
183.
else
184.
if(j<16){b[i].setBounds(x=x+50,
y, 50, 50);j++;
185.
if(j==16){y=y+50;j=0;x=-41;
186.
}}}
187.
setSize(WIDTH,
HEIGHT);
188.
setVisible(true);
189.
setResizable(false);
190.
setDefaultCloseOperation(EXIT_ON_CLOSE);
191.
img
= new ImageIcon(getClass().getResource("BG.jpg"));
192.
BG
= new JLabel(img);
193.
C.add(BG);
194.
BG.setBounds(00,
00, 425, 387);
195.
}//end
of public minesweeper
196.
//====================================================
197.
public
static void change_button()
198.
{
199.
if(count1[H]==0){
200.
if(BOMBS>0)
201.
{
202.
BOMBS--;
203.
T1.setText(""+BOMBS);
204.
HOLD[H]=1;
205.
b[H].setIcon(B);
206.
count1[H]++;}
207.
}
208.
else
if(count1[H]!=0)
209.
{
210.
BOMBS++;
211.
T1.setText(""+BOMBS);
212.
b[H].setIcon(A);
213.
count1[H]=0;
214.
HOLD[H]=0;
215.
}
216.
winorlose();
217.
}
218.
//============================================
219.
public
static void check()
220.
{
221.
if
(HOLD[I]!=1){
222.
if(NUM1==1){
223.
b[I].setVisible(false);
224.
NUM1=0;
225.
BOX--;
226.
}
227.
else
228.
if(NUM4==1){
229.
ttle1.setVisible(true);
230.
T1.setVisible(false);
231.
for
(i=0;i<15;i++){
232.
b[bombs[i]].setVisible(false);
233.
NUM4=0;
234.
}
235.
JOptionPane.showMessageDialog(null,mes1,"",JOptionPane.ERROR_MESSAGE);
236.
System.exit(0);
237.
}
238.
}
239.
winorlose();
240.
}
241.
public
static void winorlose(){
242.
if(BOX==0){
243.
JOptionPane.showMessageDialog(null,mes,"",JOptionPane.INFORMATION_MESSAGE);
244.
System.exit(0);}
245.
}
246.
//=======================================================
247.
public
static void main(String[] args){ minesweeper
MS = new minesweeper();}}
.ikaw na gyud jai..
ReplyDeleteSalamat Nan Carreon for the nice Comment.
Delete