J2ME pseudo master pioneer opening - the design of the mine game
First of all, I have to install like a master, pretend to analyze the system slightly.
Generally, according to Java development model, this procedure is generally divided into three modules to develop.
The following three:
The primary file for a program operates, 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 what, then you should be a CommandListener message listening class (everyone can understand the Java's message monitoring as a thread, always Like a look, it is obsessed with the eye, of course, here is what he can touch. When you receive a message, you will call a abstract function public void commandression (Command C, Displayable D), and this Abstract function allows us to deal with the message received by his implementation, ie message response)
Of course, it is a logic unit that is not related to the interface, where we define the logic of the entire game, and do logic and interface. This is my biggest harvest in Java, huh, huh.
First officially start the first lecture
My idea is that the minetic map is generally a rectangle, because the phone of the circular screen looks abreast, there is no need to move him, so I can fully represent the entire map with a 2D array of A * B.
There is a map in the map in the map naturally some of them is the landmine. In this case, it is better to
/ *** 20 Symbol This location is the number of mine * <= 10 indicates that the unopened square and the number of mines around *> = 10 indicates that the number that has been opened and the number of mines around * * /
The method is said, and the logic is clear.
I want to open a block, as long as he adds him 10.
Java programmed first step, of course, you want to Class first.
Package Games; import java.util.random; import java.lang.math; class gamelogic {/ ** represents a 10 * 10 chessboard * / private int [] [] PAN = new int; private random = Random variables, main roles are used to specify which locations are mines private int bombnum = 0; // Statistical mine total / ** game end * / private boolean Gameover;
The next step is to initialize the map, the map first want to throw a thunder, how to call the mine, after throwing the landlore Next, it is of course a map (we are still very kind, we have to tell the mines with comrades, somewhere A certain location, such as this: "There is a mine in the middle of 01, 01, 12 points, there is a hen, there is a hen, 2 o'clock in the direction of 2 o'clock,").
/ ** 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 is ground}}
/ ** Data on the board * / 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, there is no mine, the surrounding mine number IF (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 mine}}}}}}}}} {IF (PAN [i - 1] [j] == 20) {count = 1; // Detect whether above space is mine}} = ((i - 1> = 0) && (j 1 <= 7 )) {IF (PAN [I - 1] == 20) {count = 1; // Detect whether the right above is mine}} = ((j - 1> = 0)) {IF ( PAN [i] [j - 1] == 20) {count = 1; // Detect the left to the mine}} = ((i> = 0) && (j 1 <= 7)) {IF (PA N [i] [j 1] == 20) {count = 1; // Right}}}}} ((j - 1> = 0) && (i 1 <= 7)) {IF (PAN [i 1] [J - 1] == 20) {count = 1; // Left}}}} ((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;}}}}
/ ** Detection has been uncovered position * @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;}}}}}}}}}}}} / ** detection Whether the victory * @return is victory boolean value * / public boolean iswin () {// system.out.println (bombnum " countOpen ()); if (((Bombnum countOpen ()) == 64) {THIS Else}}}} / ** Select the position on the board, and open * @Param Matrix location * / public void OpenPan (int Matrix) {switch (getBomb (multiX)) {Case 20: // When the position is selected as the mine, the game endsGameOver (); Break; Case 0: isnull (Matrix); // When the location is empty, turn off the surrounding map BREAK; default: THIS. ISNOTNULL (Matrix); / / Otherwise, open the current position, and open the tag}} / ** When the position is empty, turn off the surrounding 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;}} ((i - 1> = 0) && (j - 1> = 0)) {// Detect whether the 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;}}
IF ((i - 1> = 0)) {// detection whether the above 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;}} IF ((i - 1> = 0) && (J 1 <= 7)) {// Whether the right is empty (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 Whether it is empty IF ( 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));}} (PAN [I] [J 1] <9) {PAN [I] [J 1] = 10;}} IF ((j - 1> = 0) && (i 1 <= 7)) {// 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);}} (PAN [i 1] [j] <9) {PAN [i 1] [J] = 10;}}
IF ((J 1 <= 7) && (i 1 <= 7)) {// Right lower IF (PAN [i 1] [J 1] == 0) {isnull ((i 1) * 8 (J 1));} f (PAN [i 1] [J 1] <9) {PAN [i 1] [J 1] = 10;}} / ** selection The position on the chessboard and open the current location * @Param Matrix location * / private void isnotnull (int matrix) {INT I, J; i = matrix / 8; j = matrix% 8; PAN [i] [j] = 10;} / ** Number of data available * @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];} / ** Detect whether the game ends * @Return Boolean game ended * / public boolean isgameOver () {return gameover;} / ** Set game end * / private void setgameover () {GameOver = true;} / ** Refunction * / public void setnewgame () {this.gameOver = false;} / ** Specify whether the location is unveiled * @Param Matrix location * @return boolean Return if Unveiled * / public boolean isfree (int matrix) {INT I, J; i = matrix / 8; j = matrix% 8; Return PAN [i] [j] <8 || PAN [i] [j] = = 20;}} public void openpan (int Matrix) is the core of the entire program, and he describes all the actions.
1. It is possible that you step on it, step on it, it is of course GameOver.
2, it is possible to step on the money, there is a big piece of empty place to play
3, life, no death, continue to explore
Program source code:
//gamecCanvans.javapackage games; import java.lang.System *;. import java.util.Random; import java.util.Vector; import javax.microedition.midlet *;. import javax.microedition.lcdui *;. / * Games action * * / class gameCanvasextends Canvasimplements CommandListener {/ ** black * / private static final int bLACK = 0x00000000; / ** white * / private static final int wHITE = 0x00FFFFFF; / ** red * / private static final int rED = 0x00FF0000; / ** Blue * / private static final int blue = 0x000000FF; / ** No Moved flag * / private static final int No_move = -1; / ** Main window * / private final control MIDlet; / ** Dogic 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; / ** height of the screen * / private int screenHeight; / ** boardCellSize square cell size, location * boardTop top of the board, * boardLeft board left position * / private int boardCellSize, Boardtop, BoardLeft; / ** PrecursorPosition Previous Cursor Select location, CursorPosition Current Cursor Select Location * / Private Int PrecursorPosition, CursorPosition; / ** Tagged Light Location * / Private Vector BombVector = New Vector ( ); Private boolean isrestart; / ** constructor * @Param MIDLET main window * / public gamecanvas (control midlet) {this.midlet = midlet; game = new gamelogic (); initializeboard (); / ** Initialization Menu * / EXITCOMMAND = New Command ("Exit", Command.exit, 1); NewGameCommand = New Command ("New Game", Command.Screen, 2); addcommand; addcommand; setcommandlistener (this) ; / ** Start the game * / initialize ();} / ** Add a mine position mark * @ param Matrix location tag * / private void addbomb (int matrix) {BombVector.Addelement (Integer.tostring (MA TRIX));} / ** Delete a mine position mark * @ param Matrix location tag * / private void Delbomb (int Matrix) {BombVector.RemoveElement (Integer.tostring (Matrix));}
/ ** Search if this location is tagged * @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 vidth = 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. To restart * / private vid initialize () {PrecursorPosition = CursorPosition = 0 Game.setnewgame (); game.initarray (); isrestart = true; bombVector.removeAllelements (); repaint ();} / ** Heavy painting canvas * @Param g heavy graphics object * / Public void paint (Graphics g) {game.isWin (); if {paintGame (g);} else {paintGameOver (g);} (game.isGameOver ()!)} OF: Unknown posted from: everenter.com