j2me-midp1.0 game Complete implementation-two-person Minesweeper 1.0 (ii)

Source: Internet
Author: User
Tags abs array exit final garbage collection variables prepare range
Author: yinowl
February 2005

Help interface
The Help interface is very simple, the need to line up the text width in a string array, and then draw on the screen, if a screen can not put the button to increase the response to turn the screen, in fact, just to redraw the number of groups before or after a few values, the source code is as follows:

Add the following code to the Miningcanvas.java
Final string[] Strgamehelp =new string[10];
Public Miningcanvas (Miningmidlet miningmidlet) {
...
Strgamehelp[0]= "This game for two people vs game";
Strgamehelp[1]= ", unlike the classic minesweeper, this";
Strgamehelp[2]= "In a game, we have to dig out";
Strgamehelp[3]= "Thunder, dig the wrong Reize exchange player."
Strgamehelp[4]= "A total of 52 thunder, 256 lattice";
Strgamehelp[5]= "son, first dig to 27 thunder person";
Strgamehelp[6]= "win. 1. Upper, lower, left, right points";
Strgamehelp[7]= "Don't for number key 2,8,4,6, dig";
Strgamehelp[8]= "Thunder for 5;2. Screen frame of Yan";
strgamehelp[9]= "color for the current next player;";
}
private void Painthelpscreen (Graphics g) {
G.setcolor (0X00FFFFFF);
G.fillrect (0,0,canvasw,canvash);
G.setfont (Lowfont);
G.setcolor (0x00000000);
for (int i=0;i<strgamehelp.length;i++) {
g.DrawString (strgamehelp[i],5,5+ (Lowfont.getheight () +3) *i,graphics.top| Graphics.left);
}
}
Add in the switch structure in the Keypressed method
Case gamestate_help://If the game is now in a state of help, that will jump to the main menu no matter which button the player presses
Gamestate=gamestate_menu;
Break


Game Main interface
The drawing of the main interface is divided into several parts, one is the plotting of the minefield board, drawing the whole chessboard according to the value of the variable in each bomb object in the two-bit array of mined area, if the Hasfound value is false, if the Hasfound value is true and the Isbomb value is false, Then you can draw the number of thunder around this minefield 、、、、、、; if the Hasfound value is true and the Isbomb value is true, then the ray is drawn by which player, respectively, and. The second part is the player's selection box. The third part is the information box of the game, that is, two players currently score, there are a few ray, information box picture. The last part is a reminder of which player is currently in the mine, the color of the player in the outer frame of the minefield, and the banner of the player in the message box.
Here are a few points to note:
1. The mined area cannot be all painted on the screen, so it scrolls through the scroll. The whole minefield is 16x16, and our screens are ready to draw 10x12, using two variables (Paintx and Painty) to control the mined coordinates at the upper-left corner of the mined area, for example, the values of these two variables are 3 and 4, then a ray of 3-10, vertical axis 4-13 will be drawn. Select the coordinates of the box (Selectedx and Selectedy) do not worry, when he exceeds the screen, only need to increase or decrease the paintx and painty values, as long as the coordinates of the selection box and paintx/ The y coordinates are worth less than 9 (10-1) and 11 (12-1) away. To be very careful here, all of our coordinates are exactly the opposite of the coordinates of the two-bit array bombs in the minefield, i.e. the selection box (5,6) is a class-bit bombs[6][5].
2. When creating a two-dimensional Bomb array of mined areas, the size of the two-bit array is 18x18 (new bomb[miningmapgrid+2][miningmapgrid+2]), with the aim of adding a circle of minefields in the true mined area of the size 16x16 to avoid bombout () Methods and other perimeter search for the location of the mine does not appear beyond the boundary anomaly.
3. Because the index 1-16 in the bombs array is the mine that really represents the coordinates in the minefield (1,1) to (16,16), it is important to be careful about all the coordinates and array index values in the entire program, and do not place errors
4. The value of the coordinate variable (player1x/y,player2x/y,bombnowx/y) for the number of values to be drawn in the message box is relative to the upper-left corner of the entire message box, and we can define a single pair of variables for the coordinates of the message box, which is clearer.
The source code is as follows:

Add the following code to the Miningcanvas.java
int empty;
Static final int miningmapgrid=16;//mined area is 16 lattice x16
Static final int bombnum=52;//Total 52 Thunder
int miningmaplength,mininggridlength;//The length of the entire minefield, and the length of the sides of each of the mined squares
The upper-left corner coordinates of the int miningmapx,miningmapy;//mined area
int selectedx,selectedy;//The coordinates of the selection box, which is the coordinates in the mined area (1-16)
int player1found,player2found;//Two player's score, that is, the number of mines that have been found
int paintx,painty;//the coordinates of the upper left corner of the area to be drawn, which is the coordinates in the minefield (1-16)
int bombleft;//The remaining number of mines not swept out
Static final int player1x=10,player1y=30;//information box to draw the player a fraction of the coordinates position, relative to the information box in the upper left corner
Static final int player2x=10,player2y=90;//information box to draw the position of the player's two fractions, relative to the top left corner of the information box
Static final int bombnowx=8,bombnowy=52;//information box to draw the coordinate position of the remaining number of mines, relative to the upper left corner of the information box
Static final int bombmapw=10,bombmaph=12;//The number of mined bits that can be plotted on the screen
Bomb[][] bombs;
Boolean isplayer1;//whether the player is currently in a minesweeper
Alert winalert;//Alert object showing winning and losing information
String winstring;//the information to output after winning or losing
Does Boolean sbwon;//win or lose?
Static final Font font = Font.getfont (Font.face_monospace,font.style_plain,font.size_small);
Fonts used in message boxes
Image Infoimg,splashimage;
Image unfoundgroundimg;
Image foundgroundimg;
Image player1bombimg,player2bombimg,player1turnimg,player2turnimg;
Image bomb1img,bomb2img,bomb3img,bomb4img,bomb5img,bomb6img,bomb7img,bomb8img;
All the pictures you want to use
Public Miningcanvas (Miningmidlet miningmidlet) {
...
try{//the image object that is needed in the instantiation game
Unfoundgroundimg=image.createimage ("/unfoundgroundbig.png");
Foundgroundimg=image.createimage ("/foundgroundbig.png");
Player1bombimg=image.createimage ("/player1bombbig.png");
Player2bombimg=image.createimage ("/player2bombbig.png");
Bomb1img=image.createimage ("/bomb1big.png");
Bomb2img=image.createimage ("/bomb2big.png");
Bomb3img=image.createimage ("/bomb3big.png");
Bomb4img=image.createimage ("/bomb4big.png");
Bomb5img=image.createimage ("/bomb5big.png");
Bomb6img=image.createimage ("/bomb6big.png");
Infoimg=image.createimage ("/info.png");
Splashimage=image.createimage ("/occo.png");
Player1turnimg=image.createimage ("/player1turn.png");
Player2turnimg=image.createimage ("/player2turn.png");
}catch (IOException e) {}
isplayer1=true;//Initialize player order
mininggridlength=14;//initializes the side lengths of each of the mined bits.
miningmaplength=14*12;//the side length of the entire minesweeper board
miningmapx= (canvasw-miningmaplength)/2;//the x-coordinate of the upper-left corner of the chessboard on the screen
Miningmapy= (canvash-miningmaplength)/2;//the y-coordinate of the upper-left corner of the chessboard on the screen
The coordinates of the selectedx=selectedy=miningmapgrid/2;//initialization selection box are
player1found=player2found=0;//Initialize the score of two players
paintx=painty=3;//initialize the entire minefield. A range that was initially displayed on the screen
sbwon=false;//initialization no player wins
bombleft=bombnum;//The total number of mines to initialize the remaining number of mines
Bombs=new bomb[miningmapgrid+2][miningmapgrid+2];
Bombinit ()//initializing mined area
}
private void Paintgamescreen (Graphics g) {
Paintplayer (G,isplayer1);
Paintminingmap (g);
Paintinfo (g);
Paintselected (g);
}
protected void Paintinfo (Graphics g) {
G.drawimage (Infoimg,miningmapx+bombmapw*mininggridlength+1,
miningmapy,graphics.top| Graphics.left);
G.setfont (font);
G.setcolor (0X00FFFFFF);
g.DrawString (String.valueof (bombleft), MININGMAPX+BOMBMAPW*MININGGRIDLENGTH+BOMBNOWX,
miningmapy+bombnowy,graphics.top| Graphics.left);
g.DrawString (String.valueof (Player1found), miningmapx+bombmapw*mininggridlength+player1x,
miningmapy+player1y,graphics.top| Graphics.left);
g.DrawString (String.valueof (Player2found), miningmapx+bombmapw*mininggridlength+player2x,
miningmapy+player2y,graphics.top| Graphics.left);
The next code in this method is to draw a small map in the information box, which is the area of the mine that appears on the screen throughout the minefield.
The location in
G.setcolor (0x00777777);
G.fillrect (Miningmapx+bombmapw*mininggridlength+1,miningmapy+8*mininggridlength+1,
2*MININGGRIDLENGTH-2,2*MININGGRIDLENGTH-1);
G.setcolor (0x00000000);
G.drawrect (Miningmapx+bombmapw*mininggridlength+1,miningmapy+8*mininggridlength+1,
2*MININGGRIDLENGTH-2,2*MININGGRIDLENGTH-1);
G.setcolor (0X00BBBBBB);
G.fillrect (miningmapx+bombmapw*mininggridlength+4+2* (paintX-1),
miningmapy+8*mininggridlength+4+2* (paintY-1), 12,17);
G.setcolor (0X00FFFFFF);
G.drawrect (miningmapx+bombmapw*mininggridlength+4+2* (paintX-1),
miningmapy+8*mininggridlength+4+2* (paintY-1), 12,17);
}
protected void Paintplayer (Graphics g,boolean isPlayer1) {
if (isPlayer1)//Draw the outer frame of the player's color on the outside of the chessboard
G.setcolor (0X000000FF);
Else
G.setcolor (0x00ff0000);
for (int i=1;i<=5;i++) {
G.drawrect (Miningmapx-i,miningmapy-i,miningmaplength+2*i,miningmaplength+2*i);
}
if (isPlayer1)//In the information box to draw the flag that represents the player
G.drawimage (player1turnimg,miningmapx+ (bombmapw+1) *mininggridlength+1,
miningmapy+11*mininggridlength,graphics.hcenter| Graphics.vcenter);
Else
G.drawimage (player2turnimg,miningmapx+ (bombmapw+1) *mininggridlength+1,
miningmapy+11*mininggridlength,graphics.hcenter| Graphics.vcenter);
}
public void Paintminingmap (Graphics g) {
for (int i=0;i<bombmaph;i++) {
for (int j=0;j<bombmapw;j++) {//Draw a different picture from the value of the variable in each bomb object
if (!bombs[i+painty+1][j+paintx+1].hasfound) {
G.drawimage (Unfoundgroundimg,miningmapx+j*mininggridlength,
miningmapy+i*mininggridlength,graphics.top| Graphics.left);
}
else {
if (!bombs[i+painty+1][j+paintx+1].isbomb) {
Switch (bombs[i+painty+1][j+paintx+1].bombaround) {
Case 0:
G.drawimage (Foundgroundimg,miningmapx+j*mininggridlength,
miningmapy+i*mininggridlength,graphics.top| Graphics.left);
Break
Case 1:
G.drawimage (Bomb1img,miningmapx+j*mininggridlength,
miningmapy+i*mininggridlength,graphics.top| Graphics.left);
Break
Case 2:
G.drawimage (Bomb2img,miningmapx+j*mininggridlength,
miningmapy+i*mininggridlength,graphics.top| Graphics.left);
Break
Case 3:
G.drawimage (Bomb3img,miningmapx+j*mininggridlength,
miningmapy+i*mininggridlength,graphics.top| Graphics.left);
Break
Case 4:
G.drawimage (Bomb4img,miningmapx+j*mininggridlength,
miningmapy+i*mininggridlength,graphics.top| Graphics.left);
Break
Case 5:
G.drawimage (Bomb5img,miningmapx+j*mininggridlength,
miningmapy+i*mininggridlength,graphics.top| Graphics.left);
Break
Case 6:
G.drawimage (Bomb6img,miningmapx+j*mininggridlength,
miningmapy+i*mininggridlength,graphics.top| Graphics.left);
Break
Default
G.drawimage (Foundgroundimg,miningmapx+j*mininggridlength,
miningmapy+i*mininggridlength,graphics.top| Graphics.left);
Break
}
}
else {
if (bombs[i+painty+1][j+paintx+1].isplayer1) {
G.drawimage (Player1bombimg,miningmapx+j*mininggridlength,
miningmapy+i*mininggridlength,graphics.top| Graphics.left);
}
else{
G.drawimage (Player2bombimg,miningmapx+j*mininggridlength,
miningmapy+i*mininggridlength,graphics.top| Graphics.left);
}
}
}
}
}
}
public void paintselected (Graphics g) {
G.setcolor (0x00ff0000)//Draw a selection box to note its relationship with the total mined area and the area that can be displayed on the screen
G.drawroundrect (miningmapx+ (selectedx-paintx) *mininggridlength-1,
miningmapy+ (Selectedy-painty) *mininggridlength-1,
mininggridlength+1,mininggridlength+1,2,2);
}
Add in the switch structure in the Keypressed method
Case gamestate_gameing:
{
if (keycode==fullcanvas.key_softkey1) {
Gamestate=gamestate_gamemenu;
}
else if (action = = fullcanvas.left) {
selectedx= (--selectedx+miningmapgrid)% (Miningmapgrid);
}
else if (action = = fullcanvas.right) {
selectedx= (++selectedx)% (Miningmapgrid);
}
else if (action = = fullcanvas.up) {
selectedy= (--selectedy+miningmapgrid)% (Miningmapgrid);
}
else if (action = = Fullcanvas.down) {
selectedy= (++selectedy)% (Miningmapgrid);
}
else if (action = = Fullcanvas.fire) {
if (!bombs[selectedy+1][selectedx+1].hasfound) {
if (Bombs[selectedy+1][selectedx+1].isbomb) {
Bombs[selectedy+1][selectedx+1].hasfound=true;
Bombs[selectedy+1][selectedx+1].isplayer1=this.isplayer1;
if (isPlayer1)
player1found++;
Else
player2found++;
bombleft--;
Checkwin ()//Every time a player digs the Thunder, it monitors whether it wins.
}
else{
Bombout (selectedy+1,selectedx+1);
If there is no thunder in the position and around it, open all the same position and the perimeter of the area.
Isplayer1=!isplayer1;
}
}
}
The following lines of code are the coordinates of the area that is displayed on the screen, lest the selection box run out of the screen range
if ((Selectedx-paintx) <0)
Paintx=selectedx;
else if ((selectedx-paintx) &GT;=BOMBMAPW)
paintx=selectedx-bombmapw+1;
if ((selectedy-painty) <0)
Painty=selectedy;
else if ((selectedy-painty) >=bombmaph)
painty=selectedy-bombmaph+1;
Break
}


Here are a few ways to use
Checkwin () Method: We put the call on this method when the player dug into the Thunder, whenever a player dug to the Ray, will change the value of Player1found and Player2found, this time to detect the game's winning and losing, when the player's score is greater than half the total number of thunder, Winning and losing on the draw, this time with the Alert object display scores and win-lose information, but also for the next game to prepare---game initialization (gameinit ()).
Gameinit () Method: This method is used to initialize the game, in which the initialization game is invoked in two situations, one is to restart the game, and the other is to prepare for the beginning of another inning after the end of the one inning. Each time you need to initialize the game, you need to have all the variables initialized
Bombinit () Method: Initializes a bomb-type two-dimensional table representing the entire minefield, randomly arranging the location of 52 mines.
Bombout () Method: Classic Minesweeper, if we point to a ray, not the ray itself, and there is no thunder around, the computer will open its own around all the similar thunder and the area of the most outer ring of a circle of thunder, this is the effect of this method. I'm using recursion here, and of course we just need to monitor the Fasfound value as false, or we'll all search.

Add the following code to the Miningcanvas.java
public void Gameinit () {
Sbwon=false;
Bombinit ();
Player1found=0;player2found=0;
SELECTEDX=MININGMAPGRID/4;SELECTEDY=MININGMAPGRID/4;
Bombleft=bombnum;
System.GC ()//Manual garbage collection
}
public void Bombinit () {
int bombindex=0;
int x=0,y=0;
Random random=new Random ();
for (int i=0;i<miningmapgrid+2;i++) {
for (int j=0;j<miningmapgrid+2;j++) {
Bombs[i][j]=new Bomb ();
}
}
while (Bombindex<bombnum) {
X=math.abs (Random.nextint ()%16+1);
Y=math.abs (Random.nextint ()%16+1);
if (!bombs[x][y].isbomb && x<=16 && x>=1 && y<=16 && y>=1) {
try{
Bombs[x][y].isbomb=true;
bombindex++;
bombs[x-1][y].bombaround++;
bombs[x-1][y-1].bombaround++;
bombs[x][y-1].bombaround++;
bombs[x+1][y-1].bombaround++;
bombs[x+1][y].bombaround++;
bombs[x+1][y+1].bombaround++;
bombs[x][y+1].bombaround++;
bombs[x-1][y+1].bombaround++;
}catch (ArrayIndexOutOfBoundsException e) {}
}
}
}
public void bombout (int x,int y) {
if (bombs[x][y].hasfound==false && x>=1 && y>=1 &&
X<=miningcanvas.miningmapgrid && Y<=miningcanvas.miningmapgrid) {
Bombs[x][y].hasfound=true;
if (bombs[x][y].bombaround==0) {
This.bombout (X-1,y);
This.bombout (x-1,y-1);
This.bombout (x,y-1);
This.bombout (x+1,y-1);
This.bombout (X+1,y);
This.bombout (x+1,y+1);
This.bombout (x,y+1);
This.bombout (x-1,y+1);
}
}
}
public void Checkwin () {
if (player1found>26) {
Winstring= "Player1 has won!";
Sbwon=true;
}
if (player2found>26) {
Winstring= "Player2 has won!";
Sbwon=true;
}
if (Sbwon) {
Winalert=new Alert ("", winstring+ "\nplayer1" +player1found
+ ":" +player2found+ "Player2", null,alerttype.info);
Gameinit ();
Winalert.settimeout (Alert.forever);
MiningMIDlet.display.setCurrent (Winalert);
}
}



Game time Menu
In the game, we need to have several features: Restart the game, open the help, return to the main menu, exit the game, of course, into the menu will also be able to return to the game in progress. This game is very similar to the menu and main menu, I do not explain in detail, but there is a flaw, the game when the menu to select Help in the Help interface to choose to return, will return to the main menu, and will not return to the game when the menu, of course, add a variable can solve the problem, the original code is as follows:

Add the following code to the Miningcanvas.java
static final int game_return = 0;
static final int game_restart = 1;
static final int game_help = 2;
static final int game_menu = 3;
static final int game_exit = 4;
static final int game_menu_item_count = 5;
Static string[] Gamemenu = new String[game_menu_item_count];
static int gamemenuidx;
Public Miningcanvas (Miningmidlet miningmidlet) {
...
gamemenuidx=0;
Gamemenu[0] = "return";
GAMEMENU[1] = "restart";
GAMEMENU[2] = "help";
GAMEMENU[3] = "Menu";
GAMEMENU[4] = "EXIT";
}
private void Paintgamemenuscreen (Graphics g) {
for (int i=0;i<gamemenu.length;i++) {
if (I==GAMEMENUIDX) {
G.setcolor (Highbgcolor);
G.fillrect (0,startheight+i* (Lowfont.getheight () +spacing)
-(Highfont.getheight ()-lowfont.getheight ())/2,
Canvasw,highfont.getheight ());
G.setfont (Highfont);
G.setcolor (Highcolor);
g.DrawString (Gamemenu[i], (Canvasw-highfont.stringwidth (Gamemenu[i))/2,
Startheight+i* (Lowfont.getheight () +spacing)-(Highfont.getheight ()
-lowfont.getheight ())/2,graphics.top| Graphics.left);
} else {
G.setfont (Lowfont);
G.setcolor (Lowcolor);
g.DrawString (Gamemenu[i], (Canvasw-lowfont.stringwidth (Gamemenu[i))/2,
Startheight+i* (Lowfont.getheight () +spacing), graphics.top| Graphics.left);
}
}
}
Add in the switch structure in the Keypressed method
Case Gamestate_gamemenu:
{
if (getgameaction (keycode) = = fullcanvas.up && gamemenuIdx-1 >= 0) {
gamemenuidx--;
}
else if (getgameaction (keycode) = = Fullcanvas.down && gamemenuidx + 1 < gamemenu.length) {
gamemenuidx++;
}
else if (getgameaction (keycode) = = Fullcanvas.fire) {
Switch (GAMEMENUIDX) {
Case Game_return:
gamestate=gamestate_gameing;
Break
Case Game_restart:
Gameinit ();
gamestate=gamestate_gameing;
Break
Case GAME_HELP:
Gamestate=gamestate_help;
Break
Case Game_menu:
Gamestate=gamestate_menu;
Break
Case Game_exit:
Miningmidlet.destroyapp (FALSE);
Break
}
}
Break
}


End-code Completion
Here, according to the functional parts of the game, the main code is basically given, in a little bit to add some code is the whole game of the complete program, need to add as follows:

Modifying the switch structure in the Keypressed method
Switch (gamestate) {

...

Default
Gamestate=gamestate_menu;
}
Repaint ();


Summarize
The whole game has been completed, we must feel very simple, but it is already a complete game, I hope to help some friends. Of course, we can do some extensions, such as adding sound, plus Bluetooth game function, so that the games slowly improve, and has commercial value.
The article slowly wrote here is almost the end, suddenly found that the whole article completely deviated from the way I originally expected and effect, I originally intended to write an article from the game's vision, to design, to coding, to the whole process of debugging, most importantly, the idea of the mind, including the wrong, imperfect, and subsequent modifications, So that I can do the real game of the whole process and experience to tell you. Did not expect to write to the end or no effect, I hope this is only the experience of writing articles, the next time will be as far as possible to improve, please understand more.
Article finished will inevitably have some mistakes, and each time a finish, always can't wait to paste up to please everyone, although every time to restrain themselves to tell themselves in check to see if there is a mistake, occasionally will be a little lazy, hehe. There is because of "j2me-midp1.0 game complete Realization-two people minesweeper (a)" A few days ago posted up, see the code of a game can not half see it, rush to put this half written out, originally intended to add an interface and variables of the map and a game thinking process, then write a postscript it, By the way, pack up the original code and paste it together.



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.