Design of mine-sweeping games (Kjava original)

xiaoxiao2021-03-06  23

Generally, according to Java development model, this procedure is generally divided into three modules to develop. The following three: a master file operated by a program, that is, a MIDLET inheritance; an interface representation class, that is, a Canvas's inheritance, there should be some menus on the interface, such as new, exit, then you should need to implement it. CommandListener Message Listening (You can understand the Java's message listening to a thread, always like a good thing to see the eye, of course, what he can touch, when you receive the news, Will call an abstract function PUBLIC VOID COMMANDACTION (COMMAND C, DISPLAY D), and this abstract function allows us to handle the message received by his implementation, that is, the message response) The last is of course a logical unit that is independent of the interface. Here we define the logic of the entire game and do logic to separately separate. This is my biggest harvest in Java, huh, huh. From: Low-key Male, Time: 2003-10-25 23:28:00, ID: 2252869 | Edit first officially started the first lecture My idea is that the mine map is generally a rectangle, Because the phone's mobile phone looks abreastous, it is not necessary to move him, so I can fully represent the entire map with a two-dimensional array of A * B. There is a map of the map naturally in the map naturally, there is a part of the mine. In this case, it is better to / *** 20 logo This location is the mine * <= 10 number Indicates that the unopened square and the number of mines around *> = 10 indicates that the number that has been opened and the surrounding mine number * * / representation method is, and the logic is clear. I want to open a block, as long as he adds him 10. Java programmed first step, of course, first wants Class ah Package Games; import java.util.random; import java.lang.math; class game gamelogic {/ ** represents a 10 * 10 chessboard * / private int [] [] PAN = new int; // A random variable, the main function is to specify which locations are mines private int bombnum = 0; // Statistical mine total / ** game end * / private boolean gameover; from: From: Low-key male, Time: 2003-10-25 23:38:00, ID: 2252871 | Editor's next step is to initialize the map, the map first want to throw a thunder, or how to call the mine, throw the landmines Next, it is of course a map (we are still very kind, we have to tell the mines with comrades, a certain location, how many thunders, such as this: "There are mines in the middle of 01, 01, 12 points, 14 o'clock幺 鸡, 2 o'clock, there is a good wind, ").

/ ** Initialization array, generate map * / public voidinitaray () {for (int i = 0; i <8; i ) {for (int J = 0; j <8; j ) {PAN [i] [J ] = 0;}} Randomarray (); referencebomb (); bombnum = bomb ();} / ** Statistical mine total * @return int Return mine total number * / private int bomb () {INT count = 0; for (int) i = 0; i <8; i ) {for (int J = 0; j <8; j ) {IF (PAN [i] [j] == 20) {count = 1;}}} Return Count; } / ** Randomly determine the location of the mine * / private vid randomarray () {Int i, j, k; // Throw 15 left mines, note that there is not necessarily 15 this here, because the random value may be repeated I don't care for for (int R = 0; R <15; R ) {k = java.lang.math.abs (random.nextint ())% 64; //random.nextint(100); i = k / 8; j = k% 8; this.pan [i] [j] = 20; // Specify this location as a mine} / ** statistical board data * / private void countbomb () {for (int i = 0; i <8; i ) {for (int J = 0; j <8; j ) {int count = 0; // When the cell needs to be detected, the number of mines around the surrounding mine is statistically (PAN [I] [J]! = 20) {IF ((i - 1> = 0) && (J - 1> = 0)) {IF (PAN [i - 1] [J - 1] == 20 ) {Count = 1; // Detect whether the upper left space is the landlore}} IF ((i - 1> = 0)) {IF (PAN [i - 1] [j] == 20) {count = 1; // Detect whether above space is mine}} = ((i - 1> = 0) && (j 1 <= 7)) {IF (PAN [I - 1] [J 1] == 20) {count = 1; // Detect whether the upper right is mine}} = J - 1> = 0)) {IF (PAN [I] [J - 1] == 20) {count = 1; // Detect whether the left is mine}} = ((i> = 0) && (j 1 <=

7)) {IF (PAN [I] [J 1] == 20) {count = 1; // Right}}}}}} ((j - 1> = 0) && (i 1 <= 7)) {IF (PAN [i 1] [J - 1] == 20) {count = 1; // Left}} IF ((i 1 <= 7)) {IF (PAN [i 1] [ J] == 20) {COUNT = 1; //}} = ((j 1 <= 7) && (i 1 <= 7)) {IF (PAN [i 1] [J 1 ] == 20) {count = 1; // Right}} PAN [i] [j] = count;}}}} from: low-key Male, Time: 2003-10-25 23:41:00 , ID: 2252872 | Edit / ** Detection has been uncovered position sum * @return returns the number of uncovered * / private int countOpen () {INT count = 0; for (int i = 0; i <8; i ) {for (int J = 0; j <8; j ) {IF (PAN [i] [j] 20 && pan [i] [j]> 9) {count = 1;}}} return count } / ** Detection Whether victory * @return Whether the victory boolean value * / public boolean iswin () {// system.out.println (bombnum " countOpen ()); if ((Bombnum country ()) == 64) {this.gameOver = true; return true;} else {returnif Alse;}} from: low-key Male, Time: 2003-10-25 23:42:00, ID: 2252873 | Edit / ** Select the position on the board, turn off * @Param Matrix location * / public void OpenPAN (int Matrix) {switch (getBomb (Matrix)) {case 20: // When the selected position is mine, the game endsGameOver (); Break; cas 0: isnull (matrix); // When the selected position is empty Open the surrounding map Break; default: this.isnotnull (matrix); / / otherwise, turn off the current position, and open the tag}} / ** When the position is empty, turn off the surroundings Map * @Param Matrix Location * / Private Void Isnull (int Matrix) {INT I, J;

i = matrix / 8; j = matrix% 8; if (PAN [i] [j] 9) {PAN [i] [j] = 10;} IF (((i - 1> = 0) && (j - 1> = 0)) {// Detect whether the upper left space is empty IF (PAN [i - 1] [j - 1] == 0) {Isnull ((i - 1) * 8 (j - 1) );} IF (PAN [I - 1] [J - 1] <9) {PAN [I - 1] [J - 1] = 10;}}}}} ((i - 1> = 0)) {/ / Detect whether the space is empty IF (PAN [i - 1] [j] == 0) {ISNULL ((i - 1) * 8 j);} IF (PAN [i - 1] [j] <9 ) {PAN [i - 1] [j] = 10;}} f ((i - 1> = 0) && (j 1 <= 7)) {// Whether or not the right is empty IF (PAN [PAN [ I - 1] [J 1] == 0) {ISNULL ((i - 1) * 8 (j 1));} IF (PAN [i - 1] [J 1] <9) {PAN [i - 1] [j 1] = 10;}}}}} ((j - 1> = 0)) {// Detection is empty if the left is empty (PAN [i] [j - 1] == 0) {ISNULL (i * 8 (j - 1));} IF (PAN [I] [J - 1] <9) {PAN [I] [J - 1] = 10;}}} ((i> = 0) && (J 1 <= 7)) {// Right IF (PAN [I] [J 1] == 0) {ISNULL (i * 8 (j 1));} IF (PAN [i] [j 1] <9) {PAN [i] [j 1] = 10;}} IF ((j - 1> = 0) && (i 1 <= 7)) {// Lower left IF (PAN [i 1] [J - 1] == 0) {ISNULL ((i 1) * 8 (j - 1));} IF (PAN [i 1] [J - 1] < 9) {PAN [i 1] [j - 1] = 10;}}}}} ((i 1 <= 7)) {// under IF (PAN [i 1] [j] == 0) {ISNULL ((i 1) * 8 j);} IF (PAN [i 1] [j] <9) {PAN [i 1] [J] = 10;}}} ((j 1 <= 7) && (i 1 <=

7)) {// Right lower IF (PAN [i 1] [J 1] == 0) {ISNULL ((i 1) * 8 (J 1));} IF (PAN [i 1] [J 1] <9) {PAN [i 1] [J 1] = 10;}}} / ** Select the position on the board, and open the current position * @Param Matrix location * / Private void isnotnull (int Matrix) {INT I, J; i = matrix / 8; j = matrix% 8; PAN [i] [j] = 10;} / ** acquisition of data * @Param Matrix location * @return int data * / public int getBomb (int Matrix) {INT i, j; i = matrix / 8; j = matrix% 8; return this.pan [i] [j];} / ** detection game End * @Return Boolean game ended * / public boolean isgameOver () {return gameover;} / ** set game end * / private void setgameOver () {gameover = true;} / ** Open Bureau * / public Void setnewgame () {this.gameOver = false;} / ** Specify whether the location is uncovered * @Param Matrix location * @Return Boolean Return to whether you can be unveiled * / public boolean isfree (int Matrix) {Int i, j ; I = matrix / 8; j = matrix% 8; Return PAN [i] [j] <8 || PAN [i] [j] == 20;}} From: low-key male, time: 2003-10 -25 23:46:00, ID: 2252881 | Edit Public Void OpenPan (int Matrix) is the core of the entire program, he describes all the actions, 1, it is possible that you step on it, step on it, it is GameOver. 2, it is possible to step on the money, there is a large piece of air to play 3, life, no death, continue to explore : Low-key Male, Time: 2003-10-25 23:47:00, ID: 2252882 | Editor, first, first talk, here, the realization of the next time comes from: low-key men, time: 2003- 10-27 17:38:00, ID: 2255425 | Edit Today's exam, so now I started the second talk, forgive me, but it seems to be interested in people, so I have stopped this example, if I reprint Famous author Liu Kunren From: YANYANDT2, Time: 2003-10-27 17:45:00, ID: 2255447 Can you be implemented on the PC? I also want to learn Java recently, but I don't know what to do with Java.

JBuilder is too big, my machine runs, doing the page and doesn't like it, I hope that the landlord will give confused, thank you! ! From: Low-key Male, Time: 2003-10-27 17:57:00, ID: 2255484 | Editor's JBuilder Configuring J2ME is relatively convenient, almost no configuration, use notepad to write, run with command line, but Put J2SE, J2EE, then configure a bunch of environment variables, then use the command line to compile, review, run, one will be dead, this is the reason I don't have to use, huh, the specific steps you go to http: // www. CNJM.NET/ Go to see: Low-key Male, Time: 2003-10-28 14:19:00, ID: 2257384 | Edit Oh, no one is just a code for code, I have no motivation From: Low-key Male, Time: 2003-10-28 14:222:00, ID: 2257393 | Edited //gameccanvans.javaPackage Games; Import Java.lang.system. *; Import Java.util.random; import java .util.vector; import javax.microedition.midlet. *; import javax.microedition.lcdui. *; / *** Title: Canvas test

*

Description: Interface of the game

*

Copyright: 2003

*

Company: NONE

* @Author Liu Kun * @version 1.0 * // ** Game Action Class * / Class Gamecanvasextends Canvasimplements CommandListener {/ ** Black * / Private Static Final Int Black = 0x00000000; / ** White * / Private Static Final Int White = 0x00fffffff; / ** red * / private static final int red = 0x00FF0000; / ** Blue * / private static final int blue = 0x000000FF; / ** No Moved Sign * / Private Static Final Int NO_MOVE = -1; / * * Main Wind Contament * / Private Final Control MIDLET; / ** Dogic "/ Private Final Gamelogic Game; / ** Exit Menu * / Private Final Command EXITCOMMAND; / ** Re-start Menu * / Private Final Command NewgameCommand; / ** random * / private final random random = new random (); / ** screen width * / private int screenwidth; / ** screen height * / private int screenHeight; / ** BoardCellsize positive single cell size, * Boardtop Play Top location, * BoardLeft board Left position * / private int board, Boardtop, BoardLeft; / ** PrecursorPosition Previous cursor selection location, CursorPosition Current cursor selection location * / private int precursorPosition, cursorPosition; / ** Store tagged mine position * / private vector bombVector = new vector (); private boolean isrestart; / ** constructor * @Param Midlet main window * / public gamecanvas (control midlet) {this.midlet = midlet; game = new gamelogic (random); Initializeboard (); / ** Initialization menu * / EXITCOMMAND = New Command ("Exit", Command.exit, 1); NewGameCommand = New Command ("New Game", Command.Screen, 2); addcommand; addcommand (newgameCommand); setcommandlistener (this); / ** Start game * / Initialize ();} / ** Add a mine position mark * @ param Matrix location tag * / private void addb (int matrix) {BombVector.addelement (Integer.tostring (matrix));} / ** Delete a mine Location Tag * @ param Matrix location tag * / private void Delbomb (int Matrix) {BombVector.RemoveEleMent (Integer.toString (Matrix));

} / ** Search if the location is marked * @Param Matrix location tag * @Return Boolean This location is recorded, false is recorded * / private boolean searchbomb (int Matrix) {return BombVector.indexof (Integer.toString (Matrix )) == -1; // - 1 means no information in this location} / ** Initialization screen, get the initial position of the chessboard * / private void initializeboard () {screenwidth = getWidth (); // get the screen width ScreenHeight = getHeight (); // acquired screen height if (screenWidth> screenHeight) {boardCellSize = (screenHeight - 1) / 8; boardLeft = (screenWidth - (boardCellSize * 8)) / 2; boardTop = 1;} else {boardCellSize = (ScreenWidth - 1) / 8; BoardLeft = 1; Boardtop = (ScreenHeight - Boardcellsize * 8) / 2;}} / ** Initialize the game and screen. Enable the game restart * / private void initialize () {PrecursorPosition = CursorPosition = 0; game.setnewgame (); game.initarray (); isrestart = true; bombVector.removeLlelements (); repaint ();} / ** Heavy painting canvas * @Param g Heavy painted Graphics object * / public void Paint Graphics g) {Game.ISWIN (); if (! Game.isgameOver ()) {PAINTGAME (G); Else {PAINTGAMEOVER (G);}} / ** Overclication Action * @Param g Heavy painted Graphics Object * / Private Void PaintGame (GRAPHICS G) {IF (Isrestart) {/ * Clear Drawboard * / G.SetColor (0xbbbbb); g.fillRect (0, 0, Screenwidth, ScreenHeight); Drawboard (g); Paintall (g); //system.out.println ("" "";//test drawbombtag (g) } DrawCursor (G);} / ** Overhare action at the end of the game, draw the game statistics * @Param g Heavy painting Graphics object * / private void PaintGameOver (Game.isgameOver () ) {IF (Game.ISWIN ()) {GameOver (g);} else {for (INT i = 0; i <8; I ) {for (int J = 0; J <8; J

) {IF (game.getbomb (i * 8 j) == 20) {DrawCircle (G, i * 8 J);}}}}}} / ** Heavy painting action at the end of the game * @Param g Heavy painted graphics object * / private void gameover (graphics g) {string Tallymsg = "You won, congratulations!"; Font font = font.getfont (font.face_system, font.style_plain, font.size_large); int strheight = Font.getHeight (); int tarlymsgwidth = font.stringwidth; int strWidth = TallyMsgWidth; / * Calculate the starting position of the drawing * / int x = (ScreenWidth - Strwidth) / 2; x = x <0? 0 : x; int y = (ScreenHeight - 2 * strheight) / 2; y = y <0? 0: y; / * Clear drawboard * / g.SetColor (White); g.FillRect (0, 0, Screenwidth, ScreenHeight ); / * Draw a text result * / g.SetColor (red); g.drawstring (Tallymsg, x 5, (y 1 strheight), (graphics.top | graphics.left);} / ** Listener Response Interface * @Param c Command * @Param D Message Source * / Public Void CommandAction (Command C, Displayable D) {IF (c == EXITCOMMAND) {MIDlet.quit ();} else if (c == newgameCommand ) { Initialize ();}} / ** painting, representation of mine position * @Param G graphic object * @Param Matrix location * / Private Void DrawCircle (GRAPHICS G, INT Matrix) {Int x, y; y = matrix / 8; x = matrix% 8; g.setColor (red); g.fillarc (x * Boardcellsize BoardLeft 3, Y * Boardcellsize Boardtop 2, Boardcellsize - 2, Boardcellsize - 2, 0, 360);} / ** Picture fork, indicating mine explosion * @Param G graphic item * @Param xx coordinate * @Param YY coordinate * / Private Void Drawcross (Graphics g, int x, int y) {G.SetColor (red); g.drawline (x 1 BoardLeft, Y Boardtop, X Boardcellsize - 4 - 4 BoardLeft, Y

Boardcellsize - 5 Boardtop; g.drawline (x 1 BoardLeft, Y Boardcellsize - 5 Boardtop, X Boardcellsize - 4 - 4 BoardLeft, Y Boardtop);} / ** According to the players and computers Heavy painting chessboard * / private void Doplayermove () {if (game.isfree (cursorPosition);} repaint (); // Draw moving graphic results} / ** Heavy Down Writing * @Param G graphic object * / private void PainTall (graphics g) {for (int i = 0; i <8; i ) {for (int J = 0; j <8; j ) {if (Game .GETBOMB (i * 8 j)> = 10 && game.getbomb (i * 8 j) <20) {PaintNum (g, i * 8 j);}}}} / ** draw text * @ PARAM G graphic item * @Param Matrix location * / private void PaintNum (graphics g, int matrix) {INT i, J, S; font font = font.getfont (font.face_system, font.style_plain, font.size_small); s = Game.GetBomb (Matrix); //system.out.println (S); i = matrix / 8; j = matrix% 8; g.SetColor (white); if (this.searchbomb (matrix)) {IF ( s! = 20) {g.drawstring (String.Valueof (S - 10), BO ardLeft j * boardCellSize 5, boardTop i * boardCellSize - 2, (Graphics.TOP | Graphics.LEFT));}}} / ** * @param keyCode capture PUSH message key codes * / protected void keyPressed (int keyCode ) {/ ** returns * / if (Game.isgameOver ()) {return;} int gameAction = getGameAction (keycode); Switch (CURSorPosition) {////////////////////////////////////////////// If the location is made, this location cannot be unveiled Doplayermove ();} Break; Case Right: DomoveCursor (1, 0); Break; Case Down: Domovecursor (0, 1); Break;

Case Left: Domovecursor (-1, 0); Break; Case Up: DomoveCursor (0, -1); Break; Default: IF (Searchbomb (CursorPosition);} else {DELBOMB (CursorPosition);} Repaint (); break;}} / ** Draw the mine sign on the board * @Param G graphic object * / private void DrawBombTag (GRAPHICS G) {Int S, J, K; G.SetColor (RED); for INT i = 0; i = 0) && (newCursorPosition <64)) if {preCursorPosition = Cursorposition; CursorPosition = NewCursorPosition; Repaint ();}} / ** Draw a select cursor on the board * @Param G graphic object * / private void Drawcursor (Graphics G) {/ ** Clear before selecting cursor * / g.setcolor (0xbbbbbb); g .drawRect ((PrecursorPosition% 8) * Boardcellsize 3 BoardLeft, (PrecursorPosition / 8) * BoardcellSize) 1 BoardTop, Boardcellsize - 2, Boardcellsize - 2); / ** Draw a selection in the current selection location Cursor * / g.setColor (this.blue); g.drawRect ((CursorPosition% 8) * Boardcellsize) 3 BoardLeft, ((CursorPosition / 8) * BoardCellsize) 1 Boardtop, Boardcellsize - 2, Boardcellsize - 2);} / ** draw an empty position * @

PARAM G graphic object * / private void Drawnull (Graphics g) {/ ** Draw the select cursor * / g.SetColor (this.White); g.FillRect ((CursorPosition% 8) * BoardcellSize 3 BoardLeft, ((CursorPosition / 8) * Boardcellsize) 1 Boardtop, Boardcellsize - 2, Boardcellsize - 2);} / ** draw a disk * @Param G graphic object * / private void Drawboard (graphics g) { / ** Remove Disk * / G.SetColor (0xbbbbb); g.FillRect (0, 0, Screenwidth, ScreenHeight); / ** Disc (INT i = 0; i < 9; i ) {g.FillRect (BoardLeft 2, Boardcellsize * i Boardtop, (Boardcellsize * 8) 1, 1); / * Painting four black horizontal lines * / g.fillRect (BoardcellSize * i BoardLeft 2, Boardtop, 1, Boardcellsize * 8); / * Sealed four black vertical lines * /}}} From: low-key Male, Time: 2003-10-28 14:23:00, ID: 2257395 | Editor Package games; import java.io.IOException; import javax.microedition.midlet *;. import javax.microedition.lcdui *;. import javax.microedition.io *;. public class controlextends MIDlet {private gameCanvas gameScreen; / ** constructor * / Public control () {} / ** Start MID Let * / public void startapp () {displayable current = display.getdisplay (this) .getCurrent (); if (current == null) {image logo = null; try {logo = image.createImage ("/ icons / mine. PNG ");} catch (ioException e) {system.err.println (" none image ");} alert splashscreen = new alert (null," mine 1.0 / n Liu Kun production / n email: liukun966122@hotmail.com " , LOGO, ALERTTYPE.INFO); // Shows a logo splashscreen.settimeout (4000); Gamescreen = new gamecanvas (this); Display.getDisplay (this) .SetCurrent (Splashscreen, Gamescreen);

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

New Post(0)