Mineching mini program! Java

Source: Internet
Author: User

During this time, in order to review the GUI programming of Java2, I wrote a small program for mine clearance by referring to relevant materials. I just wrote it clearly and hoped to help you! Haha

/** <Br/> * @ (#) block. java <br/> * this class is used to create a block object and put it in each grid in the game area. It stores some attributes of this "Grid: <br/> ************************** <br/> * -- 1. string name --> when it is a ray, store "Ray" <br/> * attribute -- |-2. if Boolean boo --> is a ray, it is true; otherwise, it is false. I call it "Thunder" <br/> * -- 3. int number --> used to store the perimeter, 9. number of mines in eight squares in the bucket <br/> ************************* * <br/> * -- 1. void setname (string name) --> set name <br/> * |-2. void setnumber (int I) --> set the number of surrounding mines <br/> * method -- |-3. int getnumber () --> obtain the number of surrounding mines <br/> * |-4. string getname () --> get name <br/> * |-5. boolean ismine () --> thunder determination <br/> * -- 6. void setismine (Boolean boo) --> setting thunder <br/> ************************* <br/> * <br/> * @ Author song junlin <br/> * @ Version 1.00 2009/4/25 <br/> */</P> <p> public class block {</P> <p> string name; // when it is a ray, the "Ray" is saved <br/> int number; // used to store the perimeter, boolean boo = false <br/> Boolean boo = false; // true indicates the number of mines in the eight squares around the nine return cells; otherwise, false indicates the number of mines. I call it "Thunder" </P> <p> // set name <br/> Public void setname (string name) {<br/> This. name = Name; <br/>}< br/> // get the name <br/> Public String getname () {<br/> return name; <br/>}< br/> // set the number of surrounding mines <br/> Public void setnumber (int I) {<br/> This. number = I; <br/>}< br/> // obtain the number of surrounding mines <br/> Public int getnumber () {<br/> return number; <br/>}< br/> // set thunder <br/> Public void setismine (Boolean boo) {<br/> This. boo = boo; <Br/>}< br/> // thunder determination <br/> Public Boolean ismine () {<br/> return boo; <br/>}</P> <p>/** <br/> * @ (#) mineframe. java <br/> * this class is the main form class. This class uses block class instances and blockview class instances as members, and is responsible for interaction between the two <br/> * @ author song junlin <br/> * @ Version 1.00 2009/4/25 <br/> */</P> <p> Import Java. AWT. *; <br/> Import Java. AWT. event. *; <br/> Import javax. swing. *; </P> <p> public class mineframe extends jframe implements acti Onlistener {</P> <p> jbutton restart; <br/> block [] []; <br/> blockview [] []; <br/> laymines lay; <br/> int ROW = 10, Colum = 12, minecount = 22; <br/> int colorswitch = 0; <br/> jpanel pcenter, pnorth; <br/> Public mineframe () {<br/> restart = new jbutton ("restart "); // create a restart button <br/> pcenter = new jpanel (); // The Central Primary game area panel <br/> pnorth = new jpanel (); // upper control panel <br/> pnorth. setbackground (color. gray); <br/> Block = new block [row] [Colum]; <br/> for (INT I = 0; I <row; I ++) {// instantiate a block array <br/> for (Int J = 0; j <Colum; j ++) {<br/> block [I] [J] = new block (); <br/>}< br/> lay = new laymines (); <br/> lay. layminesforblock (Block, minecount); <br/> blockview = new blockview [row] [Colum]; <br/> pcenter. setlayout (New gridlayout (row, Colum); <br/> for (INT I = 0; I <row; I ++) {<br/> for (Int J = 0; j <Colum; j ++) {<br/> Block View [I] [J] = new blockview (); // instantiate a blockview array <br/> blockview [I] [J]. setname (Block [I] [J]. getname (); <br/> pcenter. add (blockview [I] [J]); <br/> blockview [I] [J]. getblockcover (). addactionlistener (this); // Add a listener to each cell <br/>}< br/> restart. addactionlistener (this); // Add a listener to restart <br/> pnorth. add (restart); // Add the component to the window <br/> Add (pnorth, borderlayout. north); <br/> Add (pcenter, borderlayout. center ); <Br/> setsize (200,232); <br/> setvisible (true); <br/> // pack (); <br/> addwindowlistener (New windowadapter () {// Add window closing event listening <br/> Public void windowclosing (LOGIN wevent e) {<br/> system. exit (0); <br/>}< br/>}); <br/> validate (); <br/>}< br/> Public void actionreceivmed (actionevent e) {<br/> jbutton source = (jbutton) E. getsource (); <br/> If (source! = Restart) {<br/> int M =-1, n =-1; <br/> for (INT I = 0; I <row; I ++) {<br/> for (Int J = 0; j <Colum; j ++) {<br/> // judge the event object, if it is a grid [I] [J], open it, and the object is stored in [m] [N] <br/> If (Source = blockview [I] [J]. getblockcover () {<br/> M = I; <br/> N = J; <br/> break; <br/>}< br/> If (Block [m] [N]. ismine () {<br/> for (INT I = 0; I <row; I ++) {<br/> for (Int J = 0; j <Colum; j ++) {<br/> // If [m] [N] is thunder, the game is stopped, event listening is removed from all grids, and the button action fails. <br/>/ /Open all the thunder buttons at the same time <br/> blockview [I] [J]. getblockcover (). removeactionlistener (this); <br/> If (Block [I] [J]. ismine () <br/> blockview [I] [J]. seeblockname (); <br/>}< br/>} else {<br/> If (Block [m] [N]. getnumber ()> 0) <br/> blockview [m] [N]. seeblockname (); <br/> else {<br/> // if the number of Mines collected is 0, open the surrounding area. <br/> If (Block [m] [N]. getnumber () = 0) {<br/> for (int K = math. max (m-1, 0); k <= math. min (m + 1, row-1); k ++) {<br/> for (INT t = Math. max (n-1, 0); t <= math. min (n + 1, colum-1); t + +) {<br/> blockview [k] [T]. seeblockname (); <br/>}< br/> If (Source = restart) {<br/> for (INT I = 0; I <row; I ++) {<br/> for (Int J = 0; j <Colum; j ++) {<br/> block [I] [J]. setismine (false); <br/>}< br/> lay. layminesforblock (Block, minecount); <br/> for (INT I = 0; I <row; I ++) {<br/> for (Int J = 0; j <Colum; j ++) {<br/> blockview [I] [J]. Setname (Block [I] [J]. getname (); <br/> blockview [I] [J]. seeblockcover (); <br/> blockview [I] [J]. getblockcover (). addactionlistener (this); <br/>}< br/> Public static void main (string ARGs []) {<br/> New mineframe (); <br/>}</P> <p>/** <br/> *@(#) blockview. java <br/> * this class creates a view for the block class. Note that to achieve the clearance effect, you need to divide the attempt into two layers, with buttons on the surface, the bottom layer is a label. <br/> * The cardlayout layout is used. In addition, if you want to add multiple components in a grid, you must consider using a panel container, therefore, this view class <br/> * inherits from panel <br/> * @ author song junlin <br/> * @ Version 1.00 2009/4/25 <br/> * /</P> <p> Import Java. AWT. *; <br/> Import javax. swing. *; </P> <p> public class blockview extends jpanel {<br/> jlabel blockname; <br/> jbutton blockcover; <br/> cardlayout card; </P> <p> blockview () {// initialize each attribute in the constructor <br/> card = new cardlayout (); <br/> setlayout (card ); <br/> blockname = new Jlabel (); <br/> blockcover = new jbutton (); <br/> Add ("cover", blockcover ); // Add blockcover to the layer panel <br/> Add ("name", blockname ); // Add blockname to the layer panel <br/>}< br/> Public void setname (string name) {// set the text of blockname <br/> blockname. settext (name); <br/>}< br/> Public String getname () {// get the text of blockname <br/> return blockname. gettext (); <br/>}< br/> Public void seeblockname () {// display blockname <br/> card. Show (this, "name"); <br/> validate (); <br/>}< br/> Public void seeblockcover () {// display blockcover <br/> card. show (this, "cover"); <br/> validate (); <br/>}< br/> Public jbutton getblockcover () {// obtain blockcover <br/> return blockcover; <br/>}</P> <p>/** <br/> * @ (#) laymines. java <br/> * <br/> ** I feel that the core of the mine clearing algorithm in this class is not a problem if I fix this class, this class has only one primary method <br/> ** ---> void layminesforblock (Block block [] [], int mine Count) <br/> ** where: <br/> ** 1. the block parameter [] [] array stores block objects in the square of the entire game area. A square corresponds to a <br/> ** at the same time, using a two-dimensional array also provides support for counting the number of mines by nine return cells in the surrounding area <br/> ** 2. the minecount parameter stores the number of classes you want to generate <br/> ** 3. the collections list is also used to store all block objects in block, <br/> * @ author song junlin <br/> * @ Version 1.00 2009/4/25 <br/> */</P> <p> Import Java. util. using list; </P> <p> public class laymines {<br/> Public void layminesforblock (Block block [] [], I NT minecount) {<br/> int ROW = block. length; <br/> int column = block [0]. length; <br/> struct list <block> List = new struct list <block> (); // create a list (using a paradigm) <br/> for (INT I = 0; I <row; I ++) {<br/> for (Int J = 0; j <column; j ++) {<br/> list. add (Block [I] [J]); // use a loop, add all blocks to the list <br/>}< br/> while (minecount> 0) {<br/> int size = List. size (); <br/> int randomindex = (INT) (math. random () * size); // generates random numbers and blocks the random numbers. The property of the object is set to mine. <br/> Block B = List. get (randomindex); // obtain the corresponding block object based on the random number <br/> B. setname ("Ray"); // set name to 'Re' <br/> B. setismine (true); // set thunder to true <br/> list. remove (randomindex); // Delete the node with mine in the list to avoid repeated mine Settings <br/> minecount --; <br/>}< br/> for (INT I = 0; I <row; I ++) {<br/> for (Int J = 0; j <column; j ++) {<br/> If (! Block [I] [J]. ismine () {// If block [I] [J] is not a mine, perform the following statistical operations and scan the perimeter <br/> int minenumber = 0; <br/>/** <br/> * I think the following is the core of the core, scanning the eight surrounding nodes (but there may be less than eight grids on the boundary of the game zone ), so here we have to deal with the boundary issue. <br/> * to prevent cross-border scanning, we use a relatively large value method, which is very subtle "math. max (J-1, 0) "; below is the scan diagram: <br/> * [---------------------------] <br/> * [(I-1, J-1) | (I-1, J) | (I-1, J + 1)] <br/> * [---------------------------] <br/> * [(I, J-1) | (I, j) | (I, J + 1)] <br/> * [-------------------------] <br/> * [(I + 1, J-1) | (I + I, j) | (I + 1, J + 1)] <br/> * [-------------------------] <br/> */<br/> for (int K = math. max (I-1, 0); k <= math. min (I + 1, row-1); k ++) {<br/> for (INT T = math. max (J-1, 0); t <= math. min (J + 1, column-1); t ++) {<br/> If (Block [k] [T]. ismine () {<br/> minenumber ++; <br/>}< br/> block [I] [J]. setname ("" + minenumber); // after the name is saved as number, it is displayed. <br/> block [I] [J]. setnumber (minenumber ); // store the number of surrounding mines in number <br/>}</P> <p>}


Related Article

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.