- 论坛徽章:
- 0
|
还是太长——只好分2部分了
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.regex.*;
import java.util.Random;
class MineTable extends JPanel implements MouseListener
{
static final int BEGINNER=10,MIDDLE=40,ADVANCED=100;
static final int INITIAL=0 , EXPANDED=-1 , FLAG=1 ;
boolean explored=false;
int grid , grade , total , marked;
//logic form
boolean bomb[][];
int status[][];
//human form
JButton humantable[];
Color defaultcolor;
void map()//generate random map
{
int row,col;
//initial logic table set all grid non mine
for(int i=0; igrid; i++)
{
for(int j=0; jgrid; j++)
{
this.bomb[j] = false;
this.status[j] = INITIAL;
}
}
//set mines
for(int k=0; kgrade; k++)
{
row = (int) (Math.random()*grid);
col = (int) (Math.random()*grid);
if(bomb[row][col] == true)//this grid has been marked as a bomb,so it can not be counted
k--;
else
bomb[row][col] = true;
}
}
//if it is bound
boolean isBound(int row , int col)
{
if(row0 || row>(grid-1))
return true;
else if(col0 || col>(grid-1))
return true;
return false;
}
boolean isBomb(int row , int col)
{
if(bomb[row][col] == true)
return true;
else
return false;
}
//computer how many bomb in it's circle
int computeBomb(int row , int col)//计算它周围的炸弹数量
{
int number=0;
//up_left
if(!isBound((row-1),(col-1)))//没有越界
if(this.bomb[row-1][col-1] == true)
number++;
//up_middle
if(!isBound((row-1),(col)))
if(this.bomb[row-1][col] == true)
number++;
//up_right
if(!isBound((row-1),(col+1)))
if(this.bomb[row-1][col+1] == true)
number++;
//left
if(!isBound((row),(col-1)))
if(this.bomb[row][col-1] == true)
number++;
//right
if(!isBound((row),(col+1)))
if(this.bomb[row][col+1] == true)
number++;
//down_left
if(!isBound((row+1),(col-1)))
if(this.bomb[row+1][col-1] == true)
number++;
//down_middle
if(!isBound((row+1),(col)))
if(this.bomb[row+1][col] == true)
number++;
//down_right
if(!isBound((row+1),(col+1)))
if(this.bomb[row+1][col+1] == true)
number++;
return(number);
}
//set number
void displayBomb(int row , int col)
{
String number="";
number = String.valueOf(computeBomb(row,col));
humantable[row*grid+col].setText(number);
}
//this function is only for debug
void showMap(int row , int col)
{
String bmb="";
String number="";
number = String.valueOf(computeBomb(row,col));
if(isBomb(row,col))
bmb = "Y";
else
bmb = "N";
humantable[row*grid+col].setText(bmb + number);
}
int computeFlag(int row , int col)
{
int number=0;
//up_left
if(!isBound((row-1),(col-1)))//it's not bound
if(this.status[row-1][col-1] == FLAG)
number++;
//up_middle
if(!isBound((row-1),(col)))
if(this.status[row-1][col] == FLAG)
number++;
//up_right
if(!isBound((row-1),(col+1)))
if(this.status[row-1][col+1] == FLAG)
number++;
//left
if(!isBound((row),(col-1)))
if(this.status[row][col-1] == FLAG)
number++;
//right
if(!isBound((row),(col+1)))
if(this.status[row][col+1] == FLAG)
number++;
//down_left
if(!isBound((row+1),(col-1)))
if(this.status[row+1][col-1] == FLAG)
number++;
//down_middle
if(!isBound((row+1),(col)))
if(this.status[row+1][col] == FLAG)
number++;
//down_right
if(!isBound((row+1),(col+1)))
if(this.status[row+1][col+1] == FLAG)
number++;
return(number);
}
//mark the mine
void flag(int row , int col)
{
status[row][col] = FLAG;
humantable[row*grid+col].setBackground(Color.RED);
}
//unmark the mine
void unflag(int row , int col)
{
status[row][col] = INITIAL;
humantable[row*grid+col].setBackground(defaultcolor);//这里因该是按钮的默认颜色,但是我不知道
}
//expand the mine
/*
void expand(int row,int col)
{
status[row][col] = EXPANDED;
humantable[row*grid+col].setBackground(new Color(217, 217, 217));
displayBomb(row,col);
}
*/
void expand(int row , int col)
{
//judge if it is a bomb itself
if(bomb[row][col] == true)
{
explored = true;
}
if ((computeBomb(row,col) - computeFlag(row,col)) == 0) //
{
this.status[row][col] = EXPANDED;
this.humantable[row*grid+col].setBackground(new Color(217, 217, 217));
this.displayBomb(row,col);
}
else
{
this.expand(row-1,col-1); //up_left
this.expand(row-1,col); //up_middle
this.expand(row-1,col+1); //up_right
this.expand(row,col-1); //left
this.expand(row,col+1); //right
this.expand(row+1,col-1); //down_left
this.expand(row+1,col); //down_middle
this.expand(row+1,col+1); //down_right
}
}
MineTable(int level)
{
switch(level)
{
case BEGINNER:
grade = level;
grid = 9;
total = grid*grid;
marked = 0;
break;
case MIDDLE:
grade = level;
grid = 16;
total = grid*grid;
marked = 0;
break;
case ADVANCED:
grade = level;
grid = 30;
total = grid*grid;
marked = 0;
break;
default:
grade = BEGINNER;
grid = 9;
total = grid*grid;
marked = 0;
}
bomb = new boolean[grid][grid];
status = new int[grid][grid];
humantable = new JButton[total];
defaultcolor = new Color(255);
this.setLayout(new GridLayout(grid,grid));
map();
for (int index = 0; indextotal; index++)
{
humantable[index] = new JButton();
humantable[index].addMouseListener(this);
this.add(humantable[index]);
this.showMap(index/grid , index%grid);
}
validate();
defaultcolor = humantable[0].getBackground(); //get buttom default color
}
/*Mouse Event */
public void mousePressed(MouseEvent e)
{
int row , col;
for(int i=0; itotal; i++)
{
if(e.getSource().equals(humantable))
{
row = i/grid;
col = i%grid;
if((e.getModifiers() == InputEvent.BUTTON1_MASK) && (status[row][col] == INITIAL))
{
this.expand(row,col);
}
/*这里有问题*/
else if((e.getModifiers() == InputEvent.BUTTON3_MASK) && (status[row][col] == INITIAL) )
{
this.flag(row,col);
}
else if((e.getModifiers() == InputEvent.BUTTON3_MASK) && (status[row][col] == FLAG) )
{
this.unflag(row,col);
}
}
}
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/15262/showart_321866.html |
|