📄 bomb.java
字号:
package BombPkg;
public class Bomb
{
public static final int _COVERED=-7;
public static final int _REDFLAG=-6;
public static final int _QUESMARK=-5;
public static final int _REDBOMB=-4;
public static final int _XBOMB=-3;
public static final int _BBOMB=-2;
public static final int _BOMBAROUND_0=0;
public static final int _BOMBAROUND_1=1;
public static final int _BOMBAROUND_2=2;
public static final int _BOMBAROUND_3=3;
public static final int _BOMBAROUND_4=4;
public static final int _BOMBAROUND_5=5;
public static final int _BOMBAROUND_6=6;
public static final int _BOMBAROUND_7=7;
public static final int _BOMBAROUND_8=8;
public static final int _UNCOVEREDASBOMB = 9;
public static final int _UNBOMB = 0;
public static final int _BOMB = 1;
public static final int _BORDERLINE=1;
public static final int _SETREDFLAGRIGHT = 1;
public static final int _SETREDFLAGWRONG = 0;
public static final int _SUCCESSFUL = 0;
public static final int _ONGOING = 2;
public static final int _FAILED = 1;
public static final int _ERROR = -100;
public static final int _OK= -99;
public int width;
public int height;
public int cursorX, cursorY;
public int bnum;
private int bombMarked;
private int reallyGot;
private int progress;
public int area[][][];//= new int[20][20];
public Bomb(int iwidth, int iheight,int ibnum, int icursorX, int icursorY)
{
width=iwidth;
height=iheight;
cursorX = icursorX;
cursorY = icursorY;
bnum = ibnum;
area=new int[iheight][iwidth][2];
bombMarked = 0;
reallyGot = 0;
progress = _ONGOING;
initArea();
}
public void reset()
{
bombMarked = 0;
reallyGot = 0;
progress = _ONGOING;
initArea();
}
private void initArea()
{
for(int i = 0;i< height;i++)
{
for(int j=0;j<width;j++)
{
area[i][j][0] =_UNBOMB;
area[i][j][1] =_COVERED;
}
}
int rx,ry;
java.util.Random rnd = new java.util.Random(System.currentTimeMillis());
for(int i=0;i<bnum;)
{
rx = (int) rnd.nextInt(height);
ry = (int) rnd.nextInt(width);
if(area[rx][ry][0]!=_BOMB)
{
area[rx][ry][0]=_BOMB;
i++;
}
}
}
public int getBombMarked() { return bombMarked; }
public int getReallyGot() { return reallyGot; }
public int getProgress() { return progress; }
public int getAround(int x,int y, int flag)
{
int ret = 0;
switch(flag)
{
case _BOMB:
{
for(int i = x-1;i<x+2;i++)
{
for(int j = y-1;j<y+2;j++)
{
if((i==x)&&(j==y))
{
continue;
}
if((i>=0)&&(j>=0)&&(i<height)&&(j<width))
{
if(area[i][j][0] == _BOMB)
{
ret++;
}
}
}
}
return ret;
}
case _REDFLAG:
{
for(int i = x-1;i<x+2;i++)
{
for(int j = y-1;j<y+2;j++)
{
if((i==x)&&(j==y))
{
continue;
}
if((i>=0)&&(j>=0)&&(i<height)&&(j<width))
{
if(area[i][j][1] == _REDFLAG)
{
ret++;
}
}
}
}
return ret;
}
default:
return _ERROR;
}
}
public int isSuccess()
{
if(bnum!=reallyGot)
{
return _ONGOING;
}
for(int i = 0;i< height;i++)
{
for(int j=0;j<width;j++)
{
if(area[i][j][1] ==_COVERED)
{
return _ONGOING;
}
}
}
return _SUCCESSFUL;
}
public int setAsRedFlag(int x,int y)
{
switch(area[x][y][1])
{
case _COVERED:
area[x][y][1]=_REDFLAG;
bombMarked++;
if(area[x][y][0]==_BOMB)
{
reallyGot++;
if(isSuccess()==_SUCCESSFUL)
{
progress = _SUCCESSFUL;
}
return _SETREDFLAGRIGHT;
}
else
{
return _SETREDFLAGWRONG;
}
case _REDFLAG:
area[x][y][1]=_QUESMARK;
bombMarked--;
if(area[x][y][0]==_BOMB)
{
reallyGot--;
return _SETREDFLAGRIGHT;
}
else
{
return _SETREDFLAGWRONG;
}
case _QUESMARK:
area[x][y][1]=_COVERED;
break;
default:
break;
}
return _SETREDFLAGWRONG;
}
public void setAsQuestion(int x,int y)
{
area[x][y][1]=_QUESMARK;
}
private void setFailed(int bombX,int bombY, int nbombX,int nbombY)
{
for(int i = 0;i< height;i++)
{
for(int j=0;j<width;j++)
{
if((area[i][j][1] == _COVERED)&&(area[i][j][0] == _BOMB))
{
area[i][j][1] = _BBOMB;
}
}
}
area[bombX][bombY][1] = _REDBOMB;
if((nbombX != -1) && (nbombY != -1))
{
area[nbombX][nbombY][1] = _XBOMB;
}
progress = _FAILED;
return;
}
public int uncoverone(int x,int y)
{
//uncovered, and it is showed as numbers
if((area[x][y][1]!=_COVERED) && (area[x][y][1]>=_BOMBAROUND_0)&&(area[x][y][1]<=_BOMBAROUND_8))
{
if(getAround(x,y,_REDFLAG)!=getAround(x,y,_BOMB))
{
return _ONGOING;
}
int nbombx = -1;
int nbomby = -1;
int bombx = -1;
int bomby = -1;
int nbombflag = _ONGOING;
int bombflag = _ONGOING;
for(int i = x-1;i<x+2;i++)
{
for(int j = y-1;j<y+2;j++)
{
if((i>=0)&&(j>=0)&&(i<height)&&(j<width))
{
//if it is a bomb, but have not marked,
if((area[i][j][1] == _REDFLAG)&&(area[i][j][0] == _UNBOMB))
{
if(nbombflag !=_FAILED)
{
nbombx = i;
nbomby = j;
nbombflag = _FAILED;
}
}
//
else if((area[i][j][1] != _REDFLAG)&&(area[i][j][0] == _BOMB ))
{
if(bombflag !=_FAILED)
{
bombx = i;
bomby = j;
bombflag = _FAILED;
}
}
else
{
}
}
if((bombflag == _FAILED)&&(nbombflag == _FAILED))
{
setFailed(bombx,bomby,nbombx,nbomby);
return _FAILED;
}
}
}
for(int i = x-1;i<x+2;i++)
{
for(int j = y-1;j<y+2;j++)
{
if((i>=0)&&(j>=0)&&(i<height)&&(j<width))
{
if(area[i][j][1]==_COVERED)
{
uncoverone(i,j);
}
}
}
}
}
//uncovered, and it is not number
else if((area[x][y][1]!=_COVERED) && (!((area[x][y][1]>=_BOMBAROUND_0)&&(area[x][y][1]<=_BOMBAROUND_8))))
{
}
else
{
if(area[x][y][0]==_BOMB)
{
//game over if you uncover a bomb
setFailed(x,y,-1,-1);
return _FAILED;
}
int around = getAround(x,y,_BOMB);
area[x][y][1] = around;
if(around==0)
{
for(int i = x-1;i<x+2;i++)
{
for(int j = y-1;j<y+2;j++)
{
if((i>=0)&&(j>=0)&&(i<height)&&(j<width))
{
if(area[i][j][1]==_COVERED)
{
uncoverone(i,j);
}
}
}
}
}
}
if(isSuccess()==_SUCCESSFUL)
{
progress = _SUCCESSFUL;
return _SUCCESSFUL;
}
return _ONGOING;
}
/*
public int uncoverAround(int x,int y)
{
if(area[x][y][1]==_COVERED)
{
return _ONGOING;
}
if(area[x][y][0]==_BOMB)
{
//game over if you uncover a bomb
setFailed();
return _FAILED;
}
int around = getAround(x,y,_BOMB);
area[x][y][1] = around;
// if
if(around==0)
{
for(int i = x-1;i<x+2;i++)
{
for(int j = y-1;j<y+2;j++)
{
if((i>=0)&&(j>=0)&&(i<height)&&(j<width))
{
if(area[i][j][1]==_COVERED)
{
uncoverone(i,j);
}
}
}
}
}
return _ONGOING;
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -