J2ME--The design of the mine-clearing game

Source: Internet
Author: User
Tags abstract exit empty final inheritance integer interface tostring
Design J2ME Pseudo-master pioneer--The design of mine-clearing game


First I want to act like a master, to pretend to analyze the system a little bit.

Generally, according to Java development model, this program is generally divided into three modules to develop.

The following three:

The main file that a program operates, namely a midlet inheritance;

An interface of the presentation class, that is, a canvas inheritance, the interface should have some menus, such as new, exit, and so on, it should be implements a Commandlistener message listening class (You can take the Java message listening as a thread, has always been like Japanese to see the pleasing things crouched, of course, this refers to the message he can touch, when received the message, will call an abstract function public void commandaction (Command C, displayable D), And this abstract function allows us to process the received message, i.e. the message response, through his implementation.

The last one, of course, is the interface-independent logic unit, where we define the logic of the entire game, separating the logic from the interface. This is I learn Java the biggest harvest, hehe.

First to start the first lecture < The logic of mine clearance game >

My idea is that the map of mine clearance is generally a rectangle, because, the circular screen of the mobile phone looks very abnormal, there is no need to accommodate him, so I use a a*b two-dimensional array can completely represent the entire map.

With the map after the class in the map of nature is a part of the mine, since then, it is better than this < nonsense come, children do not learn >

/**

* 20 sign that location is mine

* &LT;=10 figures indicate the number of open squares and surrounding mines

* &GT;=10 figures indicate the number of open squares and surrounding mines

* */

The method is coming out, and the logic is clear.

I want to open a block, just add him to 10.

The first step of Java programming, of course, is to first class AH

Package games;

Import Java.util.Random;

Import Java.lang.Math;

Class Gamelogic {

/** represents a 10*10 board.

Private int[][] pan = new int;

Private Random random;//A random variable, the main role is to specify which locations for the mine

private int bombnum = 0; Statistics of total mines

/** whether the game is over or not

Private Boolean Gameover;
The next step is to initialize the map, the map first to throw a ray on the top ah, how else would it be called mine clearance, after the mines have been thrown? Next of course is to traverse the map (we are still very benevolent, we have to tell the mine-clearing comrades, certain position, how many thunder, such as: "01, 01, 12 points in the direction of the mine, 14 o'clock in the direction of the chicken, 2 o'clock the direction of the east and the like Ah ").

/** initialize array, generate map

public void Initarray () {
for (int i = 0; i < 8; i++) {
for (int j = 0; J < 8; J + +) {
PAN[I][J] = 0;
}
}

Randomarray ();
Countbomb ();
Bombnum = Bomb ();
}


/** statistics on total mines
* Total Return of mines @return Int * *

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 determines the location of mines.

private void Randomarray () {
int I, j, K;

First throw 15 or so mines, note that there is not necessarily 15, because the random value may repeat, I do not care
for (int r = 0; r < r++) {
K = Java.lang.Math.abs (Random.nextint ())% 64; Random.nextint (100);
i = K/8;
j = k% 8;
THIS.PAN[I][J] = 20; Specifies that the location is a mine
}
}


/** statistics on the board of data * *

private void Countbomb () {
for (int i = 0; i < 8; i++) {
for (int j = 0; J < 8; J + +) {
int count = 0;

Count the number of mines around when the cells that need to be detected are mine-free
if (Pan[i][j]!= 20) {
if ((i-1 >= 0) && (j-1 >= 0)) {
if (pan[i-1][j-1] = = 20) {
Count + 1; Detect if the upper left space is a mine
}
}

if ((i-1 >= 0)) {
if (pan[i-1][j] = = 20) {
Count + 1; Detect if above space is a mine
}
}

if ((i-1 >= 0) && (j + 1 <= 7)) {
if (pan[i-1][j + 1] = = 20) {
Count + 1; Detect if the top right is a mine
}
}

if ((j-1 >= 0)) {
if (pan[i][j-1] = = 20) {
Count + 1; Detect if the left is a mine
}
}

if ((i >= 0) && (j + 1 <= 7)) {
if (pan[i][j + 1] = = 20) {
Count + 1; Right
}
}

if ((j-1 >= 0) && (i + 1 <= 7)) {
if (pan[i + 1][j-1] = = 20) {
Count + 1; Lower left
}
}

if ((i + 1 <= 7)) {
if (pan[i + 1][j] = = 20) {
Count + 1; Under
}
}

if ((j + 1 <= 7) && (i + 1 <= 7)) {
if (pan[i + 1][j + 1] = = 20) {
Count + 1; Lower right
}
}

PAN[I][J] = count;
}
}
}
}
/** detection has been uncovered by the sum of the positions

* @return Return 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] < && pan[i][j] > 9) {
Count + 1;
}
}
}

return count;
}



/** Test whether victory

* @return Whether the Boolean value of victory * *

public Boolean Iswin () {

System.out.println (Bombnum + "" + Countopen ());
if ((Bombnum + countopen ()) = = 64) {
This. Gameover = true;
return true;
}
else {
return false;
}
}

/** Select the position on the board and Open

* @param Matrix Position * *

public void Openpan (int matrix) {
Switch (Getbomb (matrix)) {
Case 20://Elected position for mine, game over
Setgameover ();
Break
Case 0:
IsNull (matrix); The location of the election is empty, then open the surrounding map
Break
Default
This.isnotnull (matrix); Otherwise, open the current position and make the mark
}
}

/** the location of the election is empty, then turn around the map

* @param Matrix Position * *

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;
}
}


if ((i-1 >= 0)) {//detect 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)) {//detect whether the upper right 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 ((j-1 >= 0)) {//detect whether the left is empty
if (pan[i][j-1] = = 0) {
IsNull (I * 8 + (j-1));
}
if (Pan[i][j-1] < 9) {
PAN[I][J-1] + + 10;
}
}

if ((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;
}
}

if ((i + 1 <= 7)) {//Next
if (pan[i + 1][j] = = 0) {
IsNull ((i + 1) * 8 + j);
}
if (Pan[i + 1][j] < 9) {
Pan[i + 1][j] + = 10;
}
}
if (j + 1 <= 7) && (i + 1 <= 7)) {//lower right
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 chessboard and open the current position

* @param Matrix Position * *

private void Isnotnull (int matrix) {
int I, J;
i = MATRIX/8;
j = Matrix% 8;
PAN[I][J] + + 10;
}


/** get the data at the specified location

* @param matrix position

* @return INT Data * *

public int Getbomb (int matrix) {
int I, J;
i = MATRIX/8;
j = Matrix% 8;
return THIS.PAN[I][J];
}


/** detection game is over

* @return Whether a Boolean game is over or not

public Boolean isgameover () {
return gameover;
}


/** Set Game End * *

private void Setgameover () {
Gameover = true;
}


/** to open a new bureau * *

public void Setnewgame () {
This. Gameover = false;
}


/** whether the specified location is uncovered

* @param matrix position

* @return Boolean return can be opened * *

public boolean isfree (int matrix) {
int I, J;
i = MATRIX/8;
j = Matrix% 8;
Return PAN[I][J] < 8 | | PAN[I][J] = = 20;
}

}
The 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 the excrement, step on the excrement of course Gameover

2, it is possible to step on the money, there is a large open space for you to play

3, Good life, did not kill you, continue to explore
Program Source:

Gameccanvans.java

Package games;

Import java.lang.system.*;

Import Java.util.Random;

Import Java.util.Vector;

Import javax.microedition.midlet.*;

Import javax.microedition.lcdui.*;

/** Game Action Class * *

Class Gamecanvas

Extends Canvas

Implements 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 moving sign.
private static final int no_move =-1;

/** main Form Class * *
Private final control MIDlet;

/** Logic Class * *
Private final gamelogic game;

/** Exit Menu * *
Private final Command Exitcommand;

/** Restart Menu
Private final Command Newgamecommand;

/** Random Number * *
Private final Random Random = new Random ();
/** Screen Width * *
private int screenwidth;

/** Screen Height * *
private int screenheight;


/**boardcellsize the size of a square cell,
* Boardtop the position of the top of the board,
* Boardleft Chessboard Left position * *
private int boardcellsize, boardtop, Boardleft;


/**precursorposition the position of the previous cursor selection, cursorposition the current cursor selection
private int precursorposition, cursorposition;

The location of the/** used to store the marked mines * *
Private vector bombvector = new vector ();

Private Boolean Isrestart;


/** Builder

* @param midlet Main form class * *

Public Gamecanvas (Control MIDlet) {
This.midlet = MIDlet;
Game = new Gamelogic (random);
Initializeboard ();

/** Initialization Menu * *
Exitcommand = new Command ("Exit", Command.exit, 1);
Newgamecommand = new Command ("Game", Command.screen, 2);
AddCommand (Exitcommand);
AddCommand (Newgamecommand);
Setcommandlistener (this);

/** Start the game
Initialize ();
}



/** Add a mine location marker

* @param Matrix Position Mark * *

private void Addbomb (int matrix) {
Bombvector.addelement (integer.tostring (matrix));
}



/** Delete a mine location marker

* @param Matrix Position Mark * *

private void Delbomb (int matrix) {
Bombvector.removeelement (integer.tostring (matrix));
}


/** search for whether the location is marked

* @param matrix position mark

* @return Boolean whether the position is recorded and false to be recorded.

Private Boolean searchbomb (int matrix) {
Return Bombvector.indexof (integer.tostring (matrix)) = =-1; -1 indicates no information was found for the location
}


/** Initialize the screen and get the initial position of the board.

private void Initializeboard () {
ScreenWidth = GetWidth (); Get screen width
ScreenHeight = GetHeight (); Get 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;
}
}



/** initializes games and screens. Make the game restart * *

private void Initialize () {
precursorposition = cursorposition = 0;
Game.setnewgame ();
Game. Initarray ();
Isrestart = true;
Bombvector.removeallelements ();
Repaint ();
}



/** Redraw Canvas

* @param g Graphics Objects * *

public void Paint (Graphics g) {
Game.iswin ();

if (!game.isgameover ()) {
Paintgame (g);
}
else {
Paintgameover (g);
}
}

Author: Unknown paste from: everenter.com




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.