JAVA game works - mine
Abstract: JAVA game works - mine
*************************** Programming ideas ******************** ***********
The fry are familiar with the game, the mouse click box mine, if it is mine, Manpanjieshu, if not mine, the mine opened boxes, showing that the number of minefields around. If this box around lap mine is not mine, the mine should be around the boxes are opened, and were in accordance with the above rules on iterative cycle, this cycle should come to an end in the face of boxes containing Lei Lei there, or all the minefields boxes have been detecting End, the difficulty of programming it here, we pay more attention to bones_box under the category of the left mouse button click events and bones_triggers () process, with special attention to
Is_clicked this Boolean value, it is precisely because he makes the use of iterative As for the smooth conduct of mine, I immediately adopted the way, it was said that the company's mines is very sophisticated, I do not know that the algorithm, as if still a very deep Mathematical problems, you who had nothing to feed on the bar
************************************************** *****************/
Import java.awt .*;
Import java.awt.event .*;
Import javax.swing .*;
Import javax.swing.border .*;
Import java.util .*;
Public class bones_game / / Lord of the game, initialization interface, generating minefields
(
Public static int clicked_counter = 450; / / counters, 0:00 to the successful conclusion of that mine
JFrame MainFrame; / / main form
JButton button_refresh;
Bones_box bones_box [];// Lei box
Public bones_game () / / constructor
(
Int i;
Bones_box new bones_box = [450];
Button_refresh = new JButton ( "re-start the game");
MainFrame = new JFrame ( "mine games");
MainFrame.getContentPane (). SetLayout (new BorderLayout ());
Container box_grid = new Container ();
Box_grid.setLayout (new GridLayout (15th and 30th));
For (i = 0; i <450; i ++)// draw all minefields box
(
Bones_box [i] = new bones_box (i, this);
Box_grid.add (bones_box [i]);
)
Bones_sts_refresh ();// updated information
MainFrame.getContentPane (). Add (button_refresh, BorderLayout.NORTH);
MainFrame.getContentPane (). Add (box_grid, BorderLayout.CENTER);
MainFrame.pack ();
MainFrame.show ();
MainFrame.addWindowListener (new WindowAdapter ()
(
Public void windowClosing (WindowEvent e) (
System.exit (0);
)
));
Button_refresh.addMouseListener (new MouseAdapter ()
(
Public void mouseClicked (MouseEvent e)
(
Bones_sts_refresh ();
)
));
)
Public static void main (String [] argus)
(
Games start new bones_game ();//
)
Public void bones_sts_refresh () / / set all minefields boxes, layout information
(
Int i, j;
Clicked_counter = 450;
For (i = 0; i <450; i + +)
(
Bones_box [i]. SetEnabled (true);
Bones_box [i]. Is_clicked = false;
Bones_box [i]. SetVisible (true);
Bones_box [i]. Is_bone = false;
Bones_box [i]. Number_of_real_bone = 0;
Bones_box [i]. Flag = "";
Bones_box [i]. SetText ( "");
Bones_box [i]. SetBorder (BorderFactory.createBevelBorder (BevelBorder.RAISED, Color.white, Color.black ));// processes for the initial state
Bones_box [i]. SetBackground (Color.gray);
)
For (i = 0; i <90; i ++)// random minefields
(
Bones_box [(int) (Math.random () * 449)]. Is_bone = true;
)
For (i = 0; i <450; i ++)// statistics were each Lei Lei the number of boxes around
(
If (bones_box [i]. Is_bone) / / Lei Lei in the box and does not participate in overlapping zone, by the Counter
(
Bones_game.clicked_counter–;
)
For (j = 0; j <8; j + +)
(
If (bones_box [i]. Bones_around [j] == 777)
(
Continue;
)
If (bones_box [bones_box] [i]. Bones_around [j]]. Is_bone)
(
Bones_box [i]. Number_of_real_bone + +;
)
)
/ / Open the code can look at the distribution of mine
/ / If (bones_box [i]. Is_bone)
/ / (
/ / Bones_box [i]. SetText ( "â—");
/ /)
)
)
Public void game_lose ()
(
Int type, k;
For (k = 0; k <450; k + +)
(
If (bones_box [k]. Is_bone)
(
Bones_box [k]. SetText ( "â—");
)
)
Type = JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog (MainFrame, "I am sorry, you lose, I would like to thank !!!"," messages" type);
Bones_sts_refresh ();
)
Public void game_win ()
(
Int type;
Type = JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog (MainFrame, "serious Congratulations, you won, Shaque !!!"," messages" type);
Bones_sts_refresh ();
)
)
Class bones_box extends JTextField / / Lei box
(
Public boolean is_clicked = false; / / check whether the signs
Public boolean is_bone = false; / / Is mine
Public int bone_id; / / Lei box logo,
Public int [] bones_around; / / minefields around the box BONE_ID to form an array of records
Public bones_game frame_handle; / / handle of the game
Public int number_of_real_bone; / / lap around the total number of minefields
Public String flag; / / right-click on the logo "↑": logo for the mine "?": Uncertain state ",": space
Public bones_box (int i, bones_game handle) / / constructor
(
Super ();
This.bone_id = i;
/ / This.setEnabled (false);
This.frame_handle = handle;
This.setCursor (frame_handle.MainFrame.getCursor ());
This.setForeground (Color.cyan);
This.bones_around = find_bones_around ();// access to the ID boxes around the mine, an array of deposit bones_around
This.addMouseListener (new MouseAdapter ()
(
Public void mouseClicked (MouseEvent e)
(
Int j;
/*——————- Bond pronged ———————-
Capture poor results, it gives rise to the slightest deviation below the left or right, do not know if you are better able to accurately capture the way this incident, like Microsoft, did not even fingers the keyboard at the same time, can be captured, if found, could be below code package, and I told DEV一声
(
Int k, li_num;
If (is_clicked)
(
For (k = 0, k <8, k + +)
(
If (frame_handle.bones_box [bones_around] [k]]. Flag == "↑")
(
Li_num + +;
)
)
If (li_num == number_of_real_bone)
(
For (k = 0, k <8, k + +)
(
If (frame_handle.bones_box [bones_around] [k]]. Flag == "↑")
(
Continue;
)
If (frame_handle.bones_box [bones_around] [k]]. Is_clicked)
(
Continue;
)
If (frame_handle.bones_box [bones_around] [k]]. Is_bone) / / failure
(
Frame_handle.game_lose ();
Return;
)
Frame_handle.bones_box [bones_around] [k]]. SetBorder (BorderFactory.createBevelBorder (BevelBorder.LOWERED, Color.white, Color.black ));// Lei box Waxia Show
Frame_handle.bones_box [bones_around] [k]]. SetBackground (Color.WHITE);
Frame_handle.bones_box [bones_around] [k]]. SetEnabled (false);
Frame_handle.bones_box [bones_around] [k]]. SetText (Integer.toString (frame_handle.bones_box [bones_around] [k]]. Number_of_real_bone ));// suggested that the number of minefields around
Frame_handle.bones_box [bones_around] [k]]. Is_clicked = true; / / mark the box in order to detect a mine, and avoid the frequent with each other have infinite loop
Bones_game.clicked_counter–;
)
)
)
Else
(
)
)
————————————————– –* /
If (e.getButton () == e.BUTTON1) / / left click on the
(
If (is_clicked)
(
Return;
)
If (flag == "↑") / / logo of the mine, click the direct return
(
Return;
)
If (is_bone) / / is mine, suggesting that the game failure
(
Frame_handle.game_lose ();
Return;
)
SetBorder (BorderFactory.createBevelBorder (BevelBorder.LOWERED, Color.white, Color.black ));// Lei box Waxia Show
SetBackground (Color.WHITE);
SetEnabled (false);
SetText (Integer.toString (number_of_real_bone ));// suggested that the number of minefields around
Is_clicked = true; / / mark the box in order to detect a mine, and avoid the frequent with each other have infinite loop
Bones_game.clicked_counter–;
Bones_triggers ();// trigger process
If (bones_game.clicked_counter <= 0)
(
Frame_handle.game_win ();
)
)
If (e.getButton () == e.BUTTON3) / / Right
(
If (is_clicked)
(
Return;
)
If (flag == "")
(
SetText ( "↑");
Flag = "↑";
Return;
)
If (flag == "↑")
(
SetText ("?");
Flag = "";
Return;
)
If (flag == "?")
(
SetText ("");
Flag = "";
Return;
)
)
)
));
)
Find_bones_around public int [] () / / in accordance with their own BONE_ID identify minefields lap around the ID box
(
Bones_around int [] = new int [8];
Int i, li_row, li_column;
Li_row = bone_id / 30;
Li_column bone_id-li_row * = 30;
Bones_around [0] = li_column - 1 + (li_row - 1) * 30;
Bones_around [1] = li_column + (li_row - 1) * 30;
Bones_around li_column [2] + = 1 + (li_row - 1) * 30;
Bones_around [3] = 1 + + li_column li_row * 30;
Bones_around li_column [4] + = 1 + (li_row + 1) * 30;
Bones_around [5] = li_column + (li_row + 1) * 30;
Bones_around [6] = li_column - 1 + (li_row + 1) * 30;
Bones_around [7] = li_column - 1 + li_row * 30;
If (li_column == 0) / / first out of the special treatment, because he left the air, and this will be marked as in the 777
(
Bones_around [0] = 777;
Bones_around [6] = 777;
Bones_around [7] = 777;
)
If (li_column == 29) / / Finally a special treatment, because his right is empty
(
Bones_around [2] = 777;
Bones_around [3] = 777;
Bones_around [4] = 777;
)
For (i = 0; i <bones_around.length; i + +)
(
If (bones_around [i] <0 | bones_around [i]> 449)
(
Bones_around [i] = 777;
)
)
Return bones_around;
)
Public void bones_triggers () / / trigger process
(
Int k;
Boolean is_all_free = true;
For (k = 0; k <8; k + +)
(
If (bones_around [k] == 777)
(
Continue;
)
If (frame_handle.bones_box [bones_around] [k]]. Is_bone)
/ / Not judge whether all around the mine, if the condition is not met, there will be no automatic trigger the necessary boxes around the mine, leaving players to hit their own
(
Is_all_free = false;
Break;
)
)
If (is_all_free) / / set up the conditions to begin automatically detect mines around the box
(
For (k = 0; k <8; k + +)
(
If (bones_around [k] == 777)
(
Continue;
)
If (frame_handle.bones_box [bones_around] [k]]. Flag == "↑") / / has been marked as mine, Skip
(
Continue;
)
If (frame_handle.bones_box [bones_around] [k]]. Is_clicked) / / detection, Skip
(
Continue;
)
Bones_game.clicked_counter–;
Frame_handle.bones_box [bones_around] [k]]. SetEnabled (false);
Frame_handle.bones_box [bones_around] [k]]. SetBorder (BorderFactory.createBevelBorder (BevelBorder.LOWERED, Color.white, Color.black));
Frame_handle.bones_box [bones_around] [k]]. SetBackground (Color.WHITE);
Frame_handle.bones_box [bones_around] [k]]. SetText (Integer.toString (frame_handle.bones_box [bones_around] [k]]. Number_of_real_bone));
Frame_handle.bones_box [bones_around] [k]]. Is_clicked = true;
Frame_handle.bones_box [bones_around] [k]]. Bones_triggers ();// iteration trigger continue
)
)
)
)
/*—————————— Edited by Cang, Bashen tools: EditPlus + JDK1.4.2—— ————————-*/
↑ Back
Tags: java game






