Java implements simple mine clearance

Source: Internet
Author: User
Package COM. test. swing; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. AWT. event. mouseevent; import Java. AWT. event. mouselistener; import javax. swing. jbutton; import javax. swing. jframe; import javax. swing. jlabel;/*** this is a simple example of mine clearance. It was just written in swing and is suitable for beginners to practice * This program uses setbounds (X, Y, W, H) for details about the control Layout *, refer to the self-contained clearance function of Win XP. Of course, the write function has not been implemented yet. * some other features also have bugs ** @ author ping_qc */public class test E Xtends jframe implements actionlistener, runnable, mouselistener {Private Static final long serialversionuid =-encrypted; private final int empty = 0; private final int mine = 1; private final int checked = 2; private Final int mine_count = 10; // number of mines private final int button_border = 50; // size of each point private final int mine_size = 10; // interface specification, 20x20private final int start_x = 20; // start position xprivate Fina L int start_y = 50; // start position: yprivate Boolean flag; private jbutton [] [] JB; private jlabel JL; private jlabel Showtime; private int [] [] map; /*** check whether there is a ray around a point. The coordinates of the surrounding points can be calculated from this array */private int [] [] MV ={{-1, 0 }, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1,-1}, {0,-1 }, {-1,-1 }};/*** randomly generate a set number of mines */Public void makemine () {int I = 0, TX, Ty; (; I <mine_count;) {Tx = (INT) (math. random () * mine_size ); Ty = (INT) (math. random () * mine_size); If (Map [TX] [ty] = empty) {map [TX] [ty] = mine; I ++; // do not remember the repeatedly generated record }}/ *** put the button array on the frame, corresponding to the map [] [] array */Public void makebutton () {for (INT I = 0; I <mine_size; I ++) {for (Int J = 0; j <mine_size; j ++) {JB [I] [J] = new jbutton (); // If (Map [I] [J] = mine) // JB [I] [J]. settext (I + "," + J); // listener addjb [I] [J]. addactionlistener (this); JB [I] [J]. addmouselistener (Th Is); JB [I] [J]. setname (I + "_" + J); // click it to determine which button is clicked. // Font font = new font (font. serif, Font. bold, 10); // JB [I] [J]. setfont (font); // JB [I] [J]. settext (I + "," + J); JB [I] [J]. setbounds (J * button_border + start_x, I * button_border + start_y, button_border, button_border); this. add (JB [I] [J]) ;}} public void Init () {flag = false; Jl. settext ("Welcome to test, a total of" + mine_count + "Thunder"); Jl. setvisible (true); Jl. setbounds (20, 20,500, 30); this. add (jl); showtime. settext ("in use: 0 seconds"); showtime. setbounds (400, 20,100, 30); this. add (Showtime); makemine (); makebutton (); this. setsize (550,600); this. setlocation (700,100); this. setresizable (false); this. setdefaclocloseoperation (exit_on_close); this. setvisible (true);} public test (String title) {super (title); this. setlayout (null); // No layout manager is used. Set JB = new jbutton [mine_size] [mine for the position of each control using setbounds _ SIZE]; JL = new jlabel (); Showtime = new jlabel (); map = new int [mine_size] [mine_size]; // map the button to the array} public static void main (string [] ARGs) {test = new test ("Hello miner! "); Test. init (); test. run () ;}@ overridepublic void actionreceivmed (actionevent e) {object OBJ = E. getsource (); int X, Y; If (OBJ instanceof jbutton) = false) {showmessage ("error", "internal error"); return ;} string [] tmp_str = (jbutton) OBJ ). getname (). split ("_"); X = integer. parseint (tmp_str [0]); y = integer. parseint (tmp_str [1]); If (Map [x] [Y] = mine) {showmessage ("death", "you stepped on a mine ~~~ "); Flag = true; showmine (); return;} DFS (X, Y, 0); checksuccess () ;}/ *** after each click, determine whether all the reams are found to determine the number of buttons in the computing state of enable */private void checksuccess () {int CNT = 0; For (INT I = 0; I <mine_size; I ++) {for (Int J = 0; j <mine_size; j ++) {If (JB [I] [J]. isenabled () {CNT ++ ;}} if (CNT = mine_count) {string tmp_str = showtime. gettext (); tmp_str = tmp_str.replaceall ("[^ 0-9]", ""); showmessage ("victory", "Total time used for this mine clearance:" + TM P_str + "seconds"); flag = true; showmine () ;}} private int DFS (int x, int y, int D) {map [x] [Y] = checked; int I, TX, Ty, CNT = 0; for (I = 0; I <8; I ++) {Tx = x + MV [I] [0]; ty = Y + MV [I] [1]; if (TX> = 0 & TX <mine_size & ty> = 0 & ty <mine_size) {If (Map [TX] [ty] = mine) {CNT ++; // ray count statistics near the point} else if (Map [TX] [ty] = empty ){;} else if (Map [TX] [ty] = checked) {;}} if (CNT = 0) {for (I = 0; I <8; I ++) {T X = x + MV [I] [0]; ty = Y + MV [I] [1]; if (TX> = 0 & TX <mine_size & ty> = 0 & ty <mine_size & map [TX] [ty]! = Checked) {DFS (TX, Ty, D + 1) ;}} else {JB [x] [Y]. settext (CNT + "");} JB [x] [Y]. setenabled (false); Return CNT;}/*** display some information on the JL tag ** @ Param title * @ Param info */private void showmessage (String title, string info) {Jl. settext (Info); system. out. println ("in functino showmessage ():" + info);} public void run () {int T = 0; while (true) {If (FLAG) {break ;} try {thread. sleep (1000);} catch (interruptedexception e) {e. printstacktrace ();} t ++; showtime. settext ("in use:" + T + "seconds");} // showmine ();} private void showmine () {// icon iconmine = new imageicon ("E: /mine.jpg "); For (INT I = 0; I <mine_size; I ++) {for (Int J = 0; j <mine_size; j ++) {If (Map [I] [J] = mine) {JB [I] [J]. settext ("#"); // JB [I] [J]. seticon (iconmine) ;}}}@ overridepublic void mouseclicked (mouseevent e) {If (E. getbutton () = 3) {object OBJ = E. getsource (); If (OBJ instanceof jbutton) = false) {showmessage ("error", "internal error"); return;} string [] tmp_str = (jbutton) OBJ ). getname (). split ("_"); int x = integer. parseint (tmp_str [0]); int y = integer. parseint (tmp_str [1]); If ("{1} quot ;. equals (JB [x] [Y]. gettext () {JB [x] [Y]. settext ("");} else {JB [x] [Y]. settext ("{1} quot;);}/* If (JB [x] [Y]. getIcon () = NULL) {JB [x] [Y]. seticon (New imageicon ("E:/flag.jpg");} else {JB [x] [Y]. seticon (null);} */} @ overridepublic void mousepressed (mouseevent e) {// todo auto-generated method stub} @ overridepublic void mousereleased (mouseevent E) {// todo auto-generated method stub} @ overridepublic void mouseentered (mouseevent e) {// todo auto-generated method stub} @ overridepublic void mouseexited (mouseevent E) {// todo auto-generated method stub }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.