Sweeping source code planing (J2ME)

xiaoxiao2021-03-06  135

Sweeping source code

Note: Sourcecodes are only available for reference only for personal learning research, and must not directly vote in program production.

Source code, a total of two files: Mine.java, Mgame.java, there are some pictures. Create a new project under JBuilder, copy the two source files into the SRC / MINE directory under the project directory, copy all the images to the src / images directory.

Key points analysis

The data structure first exemplifies the array GRID used to store mine information. Grid is a two-dimensional array, a line of the first corresponding table, a second bit corresponding to a list, such as Grid [Y] [x] represents the Y line No. X column. The meaning of each byte is divided into three segments: 0-9 means that it has been dug; 10-19 means still buried; 20-29 means it is tagged. In each paragraph: 0-8 indicates the number of mines around; 9 is the mine.

Newgame () function This is used to start a new game. Since each mine is buried random, new mine information should be generated before starting new games. The first step is to initialize the Grid and set all the contents 10 because all plaids are buried. For (i = 0; i

The second step is to randomly produce mine. Of course, there is a place that has already generated mines to avoid. For (i = 0; i

Digling and Make a tag to see the button response function keypressed (int kcode), press 1 button to dig the action. The first half represents if the current focus is in a burying lattice (between 10 to 19), then the value is reduced by 10, indicating that it is now excavated. Of course, if the underground is not thunder and there is no thunder (value equal to 0), it needs to be automatically expanded, which calls Expand (), (SELX, SELY) is the table coordinates of the current focus. The second half represents if the current focus is on a plaid that has been excavated (the value is less than 10), then the safeeXp () is called to move the lattice around the surrounding. Press 3 to do tag action. If the current grid is a burying, it will be marked (value 10); if the current grid has been tagged, then remove the mark (value 10).

Automatically expand expand () is a nested function, his role is to dig all around the lattice that does not contain mines, and if the excavated part also has a lattice that does not contain mines around, then repeat the previous operation for those plaids Until the relevant plaids are excavated. The order of digging is on the left, upper, upper right, left, right, left, bottom, bottom right, if you encounter a lattice (value of 0), it is immediately nested to modify the plaid. Safeexp () is a function that automatically excavates unbriched lattice. Of course, this feature is to be conditional, that is, the number of lattice that is marked around must be equal to the number of the current de labeled, that is, the player labeled all the mines around (regardless of whether it is wrong). The first part in the function is to do the above conditions. The second part is to dig the surrounding plaid. However, because the player's error may marke the lattice without mine, the lattice with mine is missing, so I have to check the lattice that is not taking the lattice and doing the wrong mark. If there is no such mistake, then you can open it safely, but also check if you have dug to surround the lattice, and if you want to call expand (). The return value of Safeexp () indicates whether the mine is detonated, that is, the true is indicated by, false indicates no. This is mainly used to determine if this game is to end. Class introduction

Image Cache ExtendedImage ExtendedImage is a dedicated class that Siemens has extension, exists only in Java, Siemens. This class is mainly used as an image. Everyone doesn't know if there is any impression. When it is programmed on the PC, since the speed is slower, it will often open a display cache, first draw a realistic content to this cache, all painted, then display again Out, the ExtendedImage class has played this role. In fact, the image class of Java itself can also achieve similar functions, but the speed is not as good as it is, and ExtendedImage is better, so I basically use this class. Of course, this is unfavorable. The main functions of ExtendImage are: Void Clear (Byte Color); populate the entire graphics area with a given color. Void Blittoscreen (int X, int y); attached the cache content to the display, (x, y) is the upper left corner coordinate on the screen. Image getImage (); Returns a standard image class. You can use the Graphics object with which it can be obtained by extendimage.getimage (). GetGraphics (), drawing on the ExtendImage.

=================================== mine.java ================================================================================================================================================================================================================================== =========================

Package mine;

Import com.siemens.mp.game. *; import javax.microedition.midlet. *; import javax.microedition.lcdui. *;

Public class mine experts midlet {private static display display; public static void main (string args []) {mine app = new mine (); app.startapp ();

Public mine () {}

protected void pauseapp () {}

Protected void Startapp () {light.setlighton (); // Make the background light DISPLAY = Display.getDisplay (this);

Mgame pstart = new mgame (this); pstart.activate (display);}

Protected Void DestroyApp (Boolean Parm1) {Display.SetCurrent (NULL); NotifyDestroyed (); Light.Setlightoff (); // Close Background Light Long}

}

=================================== mgame.java =========================================================================================================================================================================== =====================================================================================================================================================

Package mine;

Import java.util. *; import com.siemens.mp.io. *; import com.siemens.mp.game. *; import java.io.ioException; import javax.microedition.lcdui. *;

Public Class Mgame Extends Canvas Implements CommandListener {Private Static Final Int Gridsize = 8; Private Static Final Int Minecount = 15

Private static mine; private static int width, height, selx, sely, leftbomb; private static byte grid [] []; // is used to store mine protection data private static boolean gameover; // End

Private static random rand; // Generate random number

private static ExtendedImage ExScreenImg; // extended image layer private static Graphics Exg; private static Image TitleImg, MineImg, fMineImg, HideImg, fHideImg, FlagImg, fFlagImg, NumImg [], fNumImg []; private static MelodyComposer melody; // // tone Define Menu Private Static Command Contcmd = New Command ("Continue Game", Command.ok, 0); Private Static Command Startcmd = New Command ("New Game", Command.ok, 1); Private Static Command EXITCMD = New Command ( "Exit", Command.exit, 2); Private Static Command Helpcmd = New Command ("Help Information", Command.ok, 3); Private Static Command Okcmd = New Command ("OK", Command.ok, 0);

Final private static string opensnd = "Turn on the sound", closesnd = "turn off the sound", openvib = "turn on the vibration", closevib = "Turn off the vibration"; private static command sndcmd, vibcmd;

Public Static Boolean SND, VIB;

Public mgame (Mine Pmine) {PMAIN = Pmine; width = 88 / gridsize; height = (GetHeight () - 1) / Gridsize

Grid = new byte [height] [width]; rand = new random ((New Date ()). getTime ()); NumIMG = new image [8]; fnumimg = new image [8];

GameOver = true; int i; // Pre-load picture try {titleimg = image.createImage ("images / title.png"); mineImg = image.createImage ("images / mine.png"); fmineImg = image.createImage ("images / minef.png"); hideimg = image.createImage ("images / hide.png"); fHideImg = image.createImage ("images / hidef.png"); flagimg = image.createImage ("Images / flag .png "); fflagimg = image.createImage (" images / flagf.png "); for (i = 1; i <= 8; i ) {NumIMG [i-1] = image.createImage (" iMages / n " i "PNG"); fnumimg [i-1] = image.createImage ("images / n" i "f.png");}} Catch (Exception Exception) {} // initialized image cache (width must For 8 times) EXSCREENIMG = New ExtendedImage (Image.createImage (104, getHeight ()); EXG = EXSCREENIMG.GETIMAGE (). GetGraphics (); melody = new melodycomposer ();

SND = true; VIB = true; / / / read parameter Try {byte bflag [] = new byte [2]; bflag [0] = bflag [1] = 1; file keyfile = new file (); int FID = KeyFile.Open ("para"); if (KeyFile.Length (FID)> 0) KeyFile.read (FID, BFLAG, 0, 2); SND = (BFLAG [0] == 1); VIB = (BFLAG [ 1] == 1); keyfile.close (FID);} catch (ooException oException) {} catch (nullpointers) {} // if (SND) SNDCMD = New Command (Closend, Command.ok, 4); ELSE SNDCMD = New Command (OpenSND, Command.ok, 4); if (vib) Vibcmd = New Command (Closevib, Command.ok, 5); Else Vibcmd = New Command (OpenVIB, Command.ok, 5);

// Add menu addcommand (startcmd); addcommand; addcommand (helpcmd); addcommand (vibcmd);

// Draw headline EXG.DRAWIMAGE (TitleImg, 0, 0, 20);

Public Void Paint (Graphics Grap) {EXSCREENIMG.BLITTSCREEN (0, 0); // Show the image cache content on the screen}

Protected void keypressed (int kcode) // button response {if (GameOver) return;

INT bomb = 0; int = sele; switch (kcode) {case '1': // 1, dig the use of system.gc (); // Release useless resource IF (Grid [SELY] [SELX]> = 10 && Grid [SELY] [SELX] <20) {Grid [SELY] [SELX] - = 10; IF (Grid [SELY] [SELX] == 0) Expand (SELX, SELY);} Else if (Grid [SELY] [SELX] <10) {if (! safeeexp (selex, sely)) bomb = 1;} Break; Case '3': // 3, Mark System.gc (); IF ( Grid [SELY] [SELX]> = 10) {IF (Grid [SELY] [SELX] <20) {grid [sely] [SELX] = 10; leftbomb -;} else {grid [sely] [SELX] - = 10; Leftbomb ;}} Break;

Case '2': // 2 Case -59: // Up -; Break; Case '4': // 4 Case -61: // Left-to-button SELX ---; Break; Case '6': // 6 Case -62: // Right to Kex ; Break; Case '5': // 5 Case '8': // 8 Case -60: // Under Down button SELY ; Break;} IF (SELX <0 SELX = 0; if (SELX> Width-1) SELX = Width-1; IF (SELY <0) SELY = 0; if (SELY> Height-1) SELY = HEIGHT-1;

Drawblock (Oldx, Oldy, False); if (Bomb == 0) Bomb = DrawBlock (SELX, SELY, TRUE); Else DrawBlock (SELX, SELY, TRUE); EXG.SETCOLOR (0xfffffff); EXG.FILLRECT (89, 26, 13, 13); EXG.SETCOLOR (0); EXG.DRAWSTRING (" Leftbomb, 101, 26, Graphics.right | graphics.top);

IF (Bomb == 1) {GameOver = true; SoundPlay (1); if (vib) Vibrator.triggervibrator (150); EXG.DRAWSTRING ("Explosion", 101, 39, Graphics.right | graphics.top); EXG .drawstring ("fried", 101, 52, graphics.right | graphics.top);}} (judge ()) {GameOver = true; SoundPlay (0); EXG.DRAWSTRING ("Cheng", 101, 39, graphics .Right | graphics.top; exg.drawstring ("Power", 101, 52, Graphics.right | graphics.top);} exScreenimg.Blittoscreen (0, 0);

Protected void keyrepeated (int kcode) // button Connection {if (GameOver) Return;

INT OLDX = SELX; INT OLDY = SELY; SWITCH (KCODE) {CASE '2': Case -59: // Up SELY -; Break; Case '4': Case -61: // Left SELX -; Break Case '6': Case -62: // Right SELX ; Break; Case '5': Case '8': Case -60: // Down SELY ; Break;} IF (SELX <0) SELX = 0; IF (SELX> WIDTH-1) SELX = Width-1; IF (SELY <0) SELY = 0; if (SELY> Height-1) SELY = Height-1; DrawBlock (Oldx, Oldy, False); DrawBlock (SELX, SELY, TRUE); exScreenimg.Blittoscreen (0, 0);

Menu selection response // public void commandAction (Command cmd, Displayable displayable) {boolean savepara = false; if (cmd == ExitCmd) pMain.destroyApp (true); else if (cmd == StartCmd) {newGame ();} else IF (cmd == helpcmd) {form phelp = new form ("mine V0.1.0"); PHELP.APpend ("/ u00A9siemens Shanghai Mobile Communication Ltd./N" "button Description: / N2, 4, 6 , 8-moving focus, 1-dig, 3-made tags. "); PHELP.Addcommand (OKCMD); Display.SetCurrent (PHELP); PHELP.SETCOMMANDLISTENER (this);} else if (cmd == okcmd) {Activate (display);} else if (cmd == sndcmd) {SND =! SND; RemoveCommand (SNDCMD); if (SND) SNDCMD = New Command (CloseSnd, Command.ok, 4); Else Sndcmd = New Command (OpenSnd, Command.ok, 4); Addcommand (SNDCMD); SA VEPARA = True;} else if (cmd == vibcmd) {vib =! Vib; RemoveCommand (Vibcmd); if (vib) vibcmd = new Command (Closevib, Command.ok, 5); else vibcmd = new Command (OpenVIB, Command.ok, 5); addcommand (vibcmd); savepara = true;}

IF (Savepara) {// write parameter Try {byte bflag [] = new byte [2]; file keyfile = new file (); int FID = keyfile.open ("para"); bflag [0] = ( BYTE) (SND? 1: 0); bflag [1] = (Byte) (VIB? 1: 0); KeyFile.Write (FID, BFLAG, 0, 2); keyfile.close (FID);} catch (ioException IOEXCEPTION {} catch (nullpointerException npe) {} //}} public void activate (display disk; display.setcurrent (this); // Set display target setcommandListener (this); // Monitor menu selection}

/ / Do not complete the judgment private boolean judge () {if (leftbomb == 0) {INT I, J; for (i = 0; i = 10 && Grid [I] [J] 20) Return False;}} Return true;} else returnaf

// Start the new game private void newgame () {gameover = false; SELX = 0; sely = 0; leftbomb = minecount;

INT I, J, X, Y; For (i = 0; i

For (i = 0; i

IF (Grid [I] [J] == 19) Continue; INT K, L; For (K = -1; K <2; K ) {IF (i k <0) Continue; if (i k> = Height) Continue; for (l = -1; l <2; l ) {if (l j <0) Continue; if (l j> = width) Continue; if (k == 0 &&l == 0) Continue;

IF (Grid [i K] [J L] == 19) GRID [i] [j] ;}}}}

EXSCREENIMG.CLEAR ((byte) 0); for (i = 0; i <= width; i ) {EXG.DRAWLINE (i * gridsize, 0, i * gridsize, height * gridsize);} for (i = 0; I <= height; i ) {EXG.DRAWLINE (0, i * GridSize, Width * Gridsize, i * gridsize);

For (i = 0; i

/ / Draw a lattice // FOCUS indicates whether a lattice is focus, if true, you have to draw an anti-color graphic Private Int DrawBlock (int X, int y, boolean focus) {int RetVal = 0; if (grid [y ] [x] == 0) {if (! FOCUS) EXG.SETCOLOR (0xfffff); EXG.FILLRECT (x * GridSize 1, Y * Gridsize 1, GridSize-1, GridSize-1); if (! Focus EXG.SETCOLOR (0);} else if (grid [y]> 0 && grid [y] [x] <9) {if (focus) EXG.DRAWIMAGE (Fnumimg [Grid [y] [x] -1], x * GridSize 1, Y * Gridsize 1, 20); Else EXG.DRAWIMAGE (NumIMG [Grid [Y] [x] -1], x * GridSize 1, Y * Gridsize 1, 20 } Else if (grid [y] == 9) {INT I, J; for (i = 0; i

Retval = 1;} else if (grid [y]> = 10 && grid [y] [x] <20) {if (focus) EXG.DRAWIMAGE (FhideImg, x * Gridsize 1, y * Gridsize 1, 20); Else EXG.DRAWIMAGE (HideImg, x * Gridsize 1, Y * Gridsize 1, 20);} else {if (focus) EXG.DRAWIMAGE (FFlagimg, x * Gridsize 1, y * Gridsize 1, 20); Else EXG.DRAWIMAGE (Flagimg, x * Gridsize 1, Y * Gridsize 1, 20);} return return; // Return value: 1 - Drawing is mine; 0- is not}

Private void expand (int x, int y) {INT I, J; for (i = -1; i <2; i ) {if (y i <0) Continue; if (y i> = height) Continue For (j = -1; j <2; j ) {IF (x j <0) Continue; if (x j> = width) Continue; if (i == 0 && j == 0) Continue;

IF (Grid [Y I] [x J]> = 10 && Grid [Y I] [x J] <20) {grid [y i] [x j] - = 10; DrawBlock (X J, Y I, FALSE); IF (Grid [Y I] [x J] == 0) Expand (x J, Y i);}}}}

Private Boolean Safeexp (INT X, INT Y) {INT I, J, FLAG = 0; for (i = -1; I <2; I ) {IF (Y i <0) Continue; if (Y i> = HEIGHT) Continue; for (j = -1; j <2; j ) {if (x j <0) Continue; if (x j> = width) Continue; if (i == 0 && j == 0) Continue; IF (Grid [Y I] [X J]> 20) Flag ;}} f (flag! = Grid [y]) Return True; Boolean Retval = True; for (i = -1; I <2; i ) {IF (Y i <0) Continue; if (Y I> = height) Continue; for (j = -1; j <2; j ) {ix (x J <0) Continue; if (x j> = width) Continue; if (i == 0 && j == 0) Continue; IF (Grid [Y i] [x J] == 19) // Turn to mine {grid [y i] [x j] = 9; DrawBlock (x J, y i, true); RetVal = FALSE } Else if (grid [y i] [x j]> 20 && grid [y i] [x j]! = 29) // In the markup wrong place fork {EXG.DRAWLINE ((x J) * Gridsize 1, (y i) * GridSize 1, (x J 1) * GridSize-1, (Y i 1) * GridSize-1); EXG.DRAWLINE ((x J * Gridsize 1, (Y i 1) * GridSize-1, (x J 1) * GridSize-1, (Y i) * GridSize 1);}}}

IF (= -1; i <2; i ) {IF (Y i <0) Continue; if (Y I> = HEIGHT) Continue; for (j = -1; j <2 ; J ) {IF (x j <0) Continue; if (x j> = width) Continue; if (i == 0 && j == 0) Continue; IF (Grid [Y i] [x J]> = 10 && Grid [Y I] [X J] <20) {Grid [Y I] [x J] - = 10; DrawBlock (x J, Y I, FALSE); if (Grid [Y I] [x J] == 0) Expand (x J, Y i);}}}} Return RetVal;

// Game sound private void SoundPlay (int N) {if (! SND) Return; melody.resetMelody (); try {if (n == 0) // clearance {melody.setbpm (110); melody.Appendnote (MelodyComposer .TONE_C2, MelodyComposer.TONELENGTH_1_16); melody.appendNote (MelodyComposer.TONE_E2, MelodyComposer.TONELENGTH_1_16); melody.appendNote (MelodyComposer.TONE_G2, MelodyComposer.TONELENGTH_1_16); melody.appendNote (MelodyComposer.TONE_E2, MelodyComposer.TONELENGTH_1_16); melody.appendNote (MelodyComposer.TONE_G2, MelodyComposer.TONELENGTH_1_16); melody.appendNote (MelodyComposer.TONE_C3, MelodyComposer.TONELENGTH_1_8);} else if (n == 1) // explosion {melody.setBPM (132); melody.appendNote (MelodyComposer.TONE_D1 , Melodycomposer.tonelarth_1_64); melody.AppendnotNot e (MelodyComposer.TONE_E1, MelodyComposer.TONELENGTH_1_32); melody.appendNote (MelodyComposer.TONE_D1, MelodyComposer.TONELENGTH_1_32); melody.appendNote (MelodyComposer.TONE_C1, MelodyComposer.TONELENGTH_1_64); melody.appendNote (MelodyComposer.TONE_A1, MelodyComposer.TONELENGTH_1_64); } else // new game {melody.setBPM (100); melody.appendNote (MelodyComposer.TONE_G3, MelodyComposer.TONELENGTH_1_16); melody.appendNote (MelodyComposer.TONE_G3, MelodyComposer.TONELENGTH_1_16); melody.appendNote (MelodyComposer.TONE_H3, MelodyComposer. TONELENGTH_1_16);

Melody.Appendnote (MelodyComposer.tone_g3_1_8);} melody music = melody.getMelody (); Music.Play (); music = null;} catch (exception exception)}}}

}

转载请注明原文地址:https://www.9cbs.com/read-127241.html

New Post(0)