扫雷java代码思路 java实现扫雷游戏

java扫雷递归算法根据规则扫雷java代码思路,应该有个 “打开没有空白方格时递归打开周围空白或数字方格” 扫雷java代码思路的方法扫雷java代码思路;
还要有个 “点击数字方格时扫雷java代码思路 , 判断周围8个方格标记地雷数量与数字是否相同,相同打开未标记地雷扫雷java代码思路的方块” 的方法 。
方格类的思路应该是
class fangGe{
//标记是数字空白还是地雷
int type;
void daKai (){
//判断是否已经打开
//判断类型
//如果是空白 循环周围8个方格 调用其打开方法
//如果是数字 , 打开自己
//如果是地雷GameOver
}
}
只写了问题相关的,希望对你有帮助 。
用java怎么写扫雷程序首先要写一个UI,也就是操作界面,使用java.swing.*内的东西就可以搞定;
其次写一个hander,也就是具体的按钮响应,UI的初始化(哪里有雷),怎么触发雷和其他的;
一般来说简单的扫雷模型就好了 , 如果需要更有意思点,可以写一些数据库的操作内容的tool类具体的就是处理历史操作记录,场均数据或多人竞技的特点 。
如果你是说你没有设计思路 , 我可以给你个提示:递归算法是触发扫雷的方法 , 初始化用随机数来做 。
怎么用Java做一个扫雷程序 , 要原创 。。。做好了给加100第一个JAVA文件
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* 显示所有按钮的面板
* @author Administrator
*
*/
public class AllButtonPanel extends JPanel implements ActionListener{
private int row;//行数
private int col;//列数
private int mineCount;//地雷数
private MineButton[][] allButtons;//所有按钮
public AllButtonPanel(int row,int col,int mineCount){
this.row=row;
this.col=col;
this.mineCount=mineCount;
allButtons=new MineButton[row][col];
createButtons();
createMine();
init();
}
private void init(){
this.setLayout(new GridLayout(row,col));
for(int i=0;iallButtons.length;i++){
for(int j=0;jallButtons[i].length;j++){
this.add(allButtons[i][j]);
}
}
}
/**
* 随机布雷的方法
*
*/
private void createMine(){
int n=0;
while(nmineCount){//随机生成mineCount个地雷
int i=(int)(Math.random()*row);
int j=(int)(Math.random()*col);
if(allButtons[i][j].getCountOfSurroundMines()!=-1){
allButtons[i][j].setCountOfSurroundMines(-1);
n++;
}
}
for(int i=0;iallButtons.length;i++){//计算每个位置的周围地雷数
for(int j=0;jallButtons[i].length;j++){
if(allButtons[i][j].getCountOfSurroundMines()!=-1){
allButtons[i][j].setCountOfSurroundMines(getSurroundMineCount(i,j));
}
}
}
}
/**
* 统计(i , j)坐标周围8个位置的地雷数
* @param data
* @param i
* @param j
* @return
*/
private int getSurroundMineCount(int i,int j){
int num=0;//统计周围的雷数
if(i-1=0j-1=0){
num+=(allButtons[i-1][j-1].getCountOfSurroundMines()==-1?1:0);
}
if(i-1=0){
num+=(allButtons[i-1][j].getCountOfSurroundMines()==-1?1:0);
}
if(i-1=0j+1allButtons[0].length){
num+=(allButtons[i-1][j+1].getCountOfSurroundMines()==-1?1:0);
}
if(j-1=0){
num+=(allButtons[i][j-1].getCountOfSurroundMines()==-1?1:0);
}
if(j+1allButtons[0].length){
num+=(allButtons[i][j+1].getCountOfSurroundMines()==-1?1:0);
}
if(i+1allButtons.lengthj-1=0){
num+=(allButtons[i+1][j-1].getCountOfSurroundMines()==-1?1:0);

推荐阅读