重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这些东西早忘光了说下我的理解希望可以帮助你把。
10多年的南岗网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站的优势是能够根据用户设备显示端的尺寸不同,自动调整南岗建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“南岗网站设计”,“南岗网站推广”以来,每个客户项目都认真落实执行。
你先想好用什么来做,java swing里面做这个的,我记得这个东西可以直接用button来做的。
这些button形成一个矩形,用一个数组来记录每个位置,比如point (x,y)这种。
用一个map来存放每个位置上button的状态,比如用0表示是雷,1不是。2是已经显示空白的区域,最后可能就是map((x,y),1);这种。
然后基本就是一些逻辑问题了,比如随机地雷位置(设置3里面随机数设置多少个是01).怎么右键点击显示周围雷个数,这些都是不少工作。
我能想到的就这些,毕竟过了很久了,你现在要是上学的话就抓紧写这个东西,我感觉你有了思路查资料的话一个礼拜差不多也就能看得出能不能做出来,不能做出来在找源码学吧,这些东西自己先做一遍和看源码在学习效果差挺多。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main
{
public static void main(String[] argus)
{
Landmine Zhang = new Landmine();
}
}
//
// Landmine类 主界面
class Landmine extends JFrame
{
static Resources resources = new Resources();
Playing listener = new Playing(this); //主要监听者,监听地雷面板的动作
Help helpListener = new Help(this); //辅助监听者,监听“帮助”、“关于”
JPanel landminePanel = new JPanel(); //创建地雷面板
JPanel topPanel = new JPanel(); //创建顶部面板
JPanel lowerPanel = new JPanel(); //创建底部面板
public static MyButton [][] lei; //主区按钮组
public static int numberOfUnflaged ; //剩余的雷数,显示在topPanel上,用于提示用户
public static int numberOfClicked; //已经翻开的格子数,当数字数字到"总格子数—雷数"时,即胜利
public static int usedTime; //已用时间
public static JLabel numberOfUnflagedLabel = new JLabel(); //创建剩雷数标签
public static JLabel timeLabel = new JLabel();//创建时间标签
public static Timer timer; //创建计时
Keylistener keyListener = new Keylistener(this);
public Landmine()
{
super("扫雷__1.2版__小老头"); //标题
setBounds(300,90,800,800); //设置窗口位置和大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//最大化、最小化、关闭按钮
BorderLayout ff = new BorderLayout(); //创建布局管理器
setLayout(ff); //关联布局管理器
setResizable(false); //禁止改变窗口大小
/*初始化一些数据*/
numberOfClicked = 0;
numberOfUnflaged = 40;
usedTime = 0;
/*设置顶部面板*/
numberOfUnflagedLabel.setText("剩余雷数:"+numberOfUnflaged);//显示剩余雷数
numberOfUnflagedLabel.setFont(resources.fontOne);//设置剩雷数标签字体
numberOfUnflagedLabel.setIcon(resources.bombIconForLabel);//剩雷数标签图标(地雷形)
topPanel.add(numberOfUnflagedLabel); //剩雷数标签加入topPanel
timeLabel.setText("用时:" + usedTime); //显示剩余时间
timeLabel.setFont(resources.fontOne); //设置时间标签字体
timeLabel.setIcon(resources.clockIcon); //设置时间标签图标
topPanel.add(timeLabel); //时间标签加入topPanel
add(topPanel,BorderLayout.NORTH); //加入主面板上部
timer = new Timer(1000,new TimerListener());//计算器注册监听者
/*设置底部面板*/
JButton aboutJB = new JButton("关于"); //创建“关于”按钮
JButton helpJB = new JButton("求救"); //创建“求救”按钮
helpJB.addActionListener(helpListener); //"求救"按钮加入监听者
aboutJB.addActionListener(helpListener);//"关于"按钮加入监听者
helpJB.addKeyListener(keyListener);
aboutJB.addKeyListener(keyListener); //注册按键监听
lowerPanel.add(aboutJB); //“关于”按钮加入lowerPanel
lowerPanel.add(helpJB); //“帮助”按钮加入lowerPanel
add(lowerPanel,BorderLayout.SOUTH);
/*设置地雷面板*/
GridLayout dd = new GridLayout(16,16);
landminePanel.setLayout(dd); //布局管理
lei = new MyButton[18][18];
for(int i=0; i18; ++i)
{//创建下标0—17的按钮,18*18矩阵
for(int j=0; j18; ++j)
{
lei[i][j] = new MyButton(i,j);
}
}
for(int i=1; i17; ++i)
{//将下标1-16的按钮,加入面板、设置图标、翻开标记为假、加入监听者
for(int j=1; j17; ++j)
{
landminePanel.add(lei[i][j]); //按钮加入地雷面板
lei[i][j].setIcon(resources.smallIcon); //设置按钮图标
lei[i][j].isClicked = false; //翻开标记设置为 假lei[i][j].setIcon(dead);
lei[i][j].addActionListener(listener); //加入监听者
lei[i][j].addMouseListener(listener); //加入鼠标事件监听者
lei[i][j].addKeyListener(keyListener); //按钮注册按键监听,当焦点在按钮上是能监听按键
}
}
add(landminePanel,BorderLayout.CENTER); //landminePanel加入主框架中央
addLandmine(); //布雷
timer.start(); //启动计时器
setVisible(true);//显示之
}
/*布雷*/
public static void addLandmine()
{//随机将40的按钮的是否为雷的标记isBomb设为真
for(int count = 0; count40; /*blank*/)
{
int i = (int)(Math.random()*100 % 16 +1 ) ;
int j = (int)(Math.random()*100 % 16 +1 ) ;
if(lei[i][j].isBomb == false)
{
lei[i][j].isBomb = true;
count++;
}
}
}
class TimerListener implements ActionListener
{//内部类,时间监听
public void actionPerformed(ActionEvent e)
{
usedTime++;
timeLabel.setText("用时:" + usedTime);
}
}
}
//
// Playing类 执行主要游戏操作
class Playing implements ActionListener,MouseListener
{
static Resources resources = new Resources();
public static Landmine gui;
public Playing(Landmine in )
{
gui = in;
}
public void actionPerformed(ActionEvent event)
{
MyButton receive = (MyButton)event.getSource();
if(receive.isBomb)
{//如果翻到了雷。。
for(int i=1; i17; ++i)
{//将所有的雷图标设为 “地雷”
for(int j=1; j17; ++j)
{
if(gui.lei[i][j].isBomb)
gui.lei[i][j].setIcon(resources.bombIcon);
}
}
receive.setIcon(resources.deadIcon);//将踩到的地雷图标设为 “衰”
gui.timer.stop(); //停止计时器
JOptionPane.showMessageDialog(null,"小朋友,你挂了…","失败!",
JOptionPane.INFORMATION_MESSAGE,
resources.deadIcon);//提示失败
int yourChose = JOptionPane.showConfirmDialog(null,"你可能是一不小心点错了,再来一局?" );
if(yourChose == JOptionPane.OK_OPTION)
{//点击“是”时
replay();
}
else
{//点击 “否” 或 “取消” 时退出程序
System.exit(0);
}
}
else if(receive.isClicked ==false)
{//未翻到雷
showBombNumber(receive);
}
}
public static void showBombNumber(MyButton in)
{//翻开点击的按钮
int numberOfLandmine = 0;//记录雷的个数
in.isClicked = true; //翻开标记设为真
/*检测周围8个方块是否为雷*/
if(gui.lei[in.num_x-1][in.num_y-1].isBomb == true) numberOfLandmine++;//左上
if(gui.lei[in.num_x][in.num_y-1].isBomb == true) numberOfLandmine++; //上
if(gui.lei[in.num_x+1][in.num_y-1].isBomb == true) numberOfLandmine++;//右上
if(gui.lei[in.num_x+1][in.num_y].isBomb == true) numberOfLandmine++; //右
if(gui.lei[in.num_x+1][in.num_y+1].isBomb == true) numberOfLandmine++;//右下
if(gui.lei[in.num_x][in.num_y+1].isBomb == true) numberOfLandmine++; //下
if(gui.lei[in.num_x-1][in.num_y+1].isBomb == true) numberOfLandmine++;//左下
if(gui.lei[in.num_x-1][in.num_y].isBomb == true) numberOfLandmine++; //左
in.setIcon(new ImageIcon("images/"+numberOfLandmine+".png"));//根据周围的雷数显示数字图标
gui.numberOfClicked++;//翻开格子数+1
if(gui.numberOfClicked==216)
{//翻开216个格子时游戏成功,用户选择是否再来一局
int yourChoice = JOptionPane.showConfirmDialog(null,"恭喜你成功了!再来一盘吗?");
if(yourChoice == JOptionPane.OK_OPTION)
replay();
else
System.exit(0);
}
if(numberOfLandmine==0)
{//如果周围无雷,则将周围未翻开格子的全部翻开
if(gui.lei[in.num_x-1][in.num_y-1].isClicked == false)
showBombNumber(gui.lei[in.num_x-1][in.num_y-1]);
if(gui.lei[in.num_x][in.num_y-1].isClicked == false)
showBombNumber(gui.lei[in.num_x][in.num_y-1]);
if(gui.lei[in.num_x+1][in.num_y-1].isClicked == false)
showBombNumber(gui.lei[in.num_x+1][in.num_y-1]);
if(gui.lei[in.num_x+1][in.num_y].isClicked == false)
showBombNumber(gui.lei[in.num_x+1][in.num_y]);
if(gui.lei[in.num_x+1][in.num_y+1].isClicked == false)
showBombNumber(gui.lei[in.num_x+1][in.num_y+1]);
if(gui.lei[in.num_x][in.num_y+1].isClicked == false)
showBombNumber(gui.lei[in.num_x][in.num_y+1]);
if(gui.lei[in.num_x-1][in.num_y+1].isClicked == false)
showBombNumber(gui.lei[in.num_x-1][in.num_y+1]);
if(gui.lei[in.num_x-1][in.num_y].isClicked == false)
showBombNumber(gui.lei[in.num_x-1][in.num_y]);
}
}
public static void replay()
{//重新开始
gui.dispose(); //释放框架资源
gui.timer.stop(); //终止计时器
Landmine ff = new Landmine();//重新创建一个主类的实例
//这几条语句实现了重新开始————关闭上一个窗口,重新开启一个
//但是这种方法会造成内存的浪费,一个改进的方法是不关闭当年窗口,而是将当前窗口重新初始化
}
public void mousePressed(MouseEvent e)
{//当鼠标右键点击时自动调用此函数
int mods = e.getModifiers();
MyButton receive = (MyButton)e.getSource();
if((mods InputEvent.BUTTON3_MASK) != 0)
{//鼠标右键
if(receive.isClicked == false)
{
receive.isRight = receive.isRight ? false : true;//改变receive.isRight的值
if(receive.isRight)
{//如果添加标记,则剩余雷数-1,设置标签为“旗帜”
gui.numberOfUnflaged--;
receive.setIcon(resources.flagIcon);
}
else
{//如果清除标记,则剩余雷数+1,设置标签为“未翻开”
gui.numberOfUnflaged++;
receive.setIcon(resources.smallIcon);
}
gui.numberOfUnflagedLabel.setText("剩余雷数:"+gui.numberOfUnflaged);
//更新剩余雷数标签
}
}
}
public void mouseReleased(MouseEvent e){}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
}
//
// Help类,响应“关于”、“求救”
class Help implements ActionListener
{
static Resources resources = new Resources();
public static Landmine gui;
public Help(Landmine in)
{
gui = in ;
}
public void actionPerformed(ActionEvent event)
{
if(event.getActionCommand()=="关于")
JOptionPane.showMessageDialog(null,"扫雷1.2版。。小老头出品");
if(event.getActionCommand()=="求救")
help();
}
public static void help()
{//求救
int stopNumber = (int)(Math.random() * gui.numberOfUnflaged + 1 );
int count = 0;
for(int i=1; i17;++i )
{
for(int j=1; j17; ++j)
{
if( gui.lei[i][j].isBomb !gui.lei[i][j].isClicked !gui.lei[i][j].isRight )
{
count++;
}
if(count == stopNumber)
{
gui.lei[i][j].setIcon(resources.badIcon);
return;
}
}
}
}
}
//
// Keylistener类,响应键盘事件
class Keylistener implements KeyListener
{
static Resources resources = new Resources();
Landmine gui;
public Keylistener(Landmine in)
{
gui = in;
}
public void keyPressed(KeyEvent e)
{//有键按下时自动执行该方法
if(e.getKeyCode() == KeyEvent.VK_UP)
{//按键为 向上 时,将所有未标记的地雷显示出
for(int i=1; i17; ++i)
{
for(int j=1; j17; ++j)
{
if(gui.lei[i][j].isBomb !gui.lei[i][j].isRight)
gui.lei[i][j].setIcon(resources.badIcon);
}
}
}
if(e.getKeyCode() == KeyEvent.VK_DOWN)
{//按键为 向下 时,将所有未标记的地雷恢复为未点击的图标
for(int i=1; i17; ++i)
{
for(int j=1; j17; ++j)
{
if(gui.lei[i][j].isBomb !gui.lei[i][j].isRight)
gui.lei[i][j].setIcon(resources.smallIcon);
}
}
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
}
//
// 按钮类 MyBtton
class MyButton extends JButton
{
public int num_x,num_y; //第几号方块
public boolean isBomb; //是否为雷
public boolean isClicked; //是否被点击
public int BombFlag; //探雷标记
public boolean isRight; //是否点击右键
public MyButton(int x, int y)
{
BombFlag = 0;
num_x = x;
num_y = y;
isBomb = false;
isClicked = true;
isRight = false;
}
}
//
// 资源类 其他类中用到的图标,字体等
class Resources
{
public static ImageIcon deadIcon;
public static ImageIcon smallIcon;
public static ImageIcon clockIcon;
public static ImageIcon bombIcon;
public static ImageIcon flagIcon;
public static ImageIcon badIcon;
public static ImageIcon bombIconForLabel;
public static Font fontOne;
public Resources()
{
deadIcon = new ImageIcon("images/dead.gif");
smallIcon = new ImageIcon("images/smallIcon.png");
clockIcon = new ImageIcon("images/clock2.png");
bombIcon = new ImageIcon("images/bomb.png");
flagIcon = new ImageIcon("images/flag_2.png");
badIcon = new ImageIcon("images/bad.gif");
bombIconForLabel = new ImageIcon("images/bombForLabel.gif");
fontOne = new Font("null",Font.BOLD,20);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Frame
extends JFrame {
JTextField text;
JLabel nowBomb, setBomb;
int BombNum, BlockNum; // 当前雷数,当前方块数
int rightBomb, restBomb, restBlock; // 找到的地雷数,剩余雷数,剩余方块数
JButton start = new JButton(" 开始 ");
JPanel MenuPamel = new JPanel();
JPanel bombPanel = new JPanel();
Bomb[][] bombButton;
JPanel c;
BorderLayout borderLayout1 = new BorderLayout();
GridLayout gridLayout1 = new GridLayout();
public Frame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
c = (JPanel) getContentPane();
setTitle("扫雷");
c.setBackground(Color.WHITE);
MenuPamel.setBackground(Color.GRAY);
c.setLayout(borderLayout1);
setSize(new Dimension(600, 600));
setResizable(false);
BlockNum = 144;
BombNum = 10;
text = new JTextField("10 ", 3);
nowBomb = new JLabel("当前雷数" + ":" + BombNum);
setBomb = new JLabel("设置地雷数");
start.addActionListener(new Frame1_start_actionAdapter(this));
MenuPamel.add(setBomb);
MenuPamel.add(text);
MenuPamel.add(start);
MenuPamel.add(nowBomb);
c.add(MenuPamel, java.awt.BorderLayout.SOUTH);
bombPanel.setLayout(gridLayout1);
gridLayout1.setColumns( (int) Math.sqrt(BlockNum));
gridLayout1.setRows( (int) Math.sqrt(BlockNum));
bombButton = new Bomb[ (int) Math.sqrt(BlockNum)][ (int) Math.sqrt(BlockNum)];
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
bombButton[i][j] = new Bomb(i, j);
//bombButton[i][j].setSize(10, 10);
bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//设置字体大小
bombButton[i][j].setForeground(Color.white);
bombButton[i][j].addMouseListener(new Bomb_mouseAdapter(this));
bombButton[i][j].addActionListener(new Bomb_actionAdapter(this));
bombPanel.add(bombButton[i][j]);
}
}
c.add(bombPanel, java.awt.BorderLayout.CENTER);
startBomb();
}
/* 开始按钮 */
public void start_actionPerformed(ActionEvent e) {
int num=Integer.parseInt(text.getText().trim());
if (num = 5 num 50) {
BombNum = num;
startBomb();
}
else if (num 5) {
JOptionPane.showMessageDialog(null, "您设置的地雷数太少了,请重设!", "错误",
JOptionPane.ERROR_MESSAGE);
num=10;
BombNum = num;
}
else {
JOptionPane.showMessageDialog(null, "您设置的地雷数太多了,请重设!", "错误",
JOptionPane.ERROR_MESSAGE);
num=10;
BombNum = num;
}
}
/* 开始,布雷 */
public void startBomb() {
nowBomb.setText("当前雷数" + ":" + BombNum);
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
bombButton[i][j].isBomb = false;
bombButton[i][j].isClicked = false;
bombButton[i][j].isRight = false;
bombButton[i][j].BombFlag = 0;
bombButton[i][j].BombRoundCount = 9;
bombButton[i][j].setEnabled(true);
bombButton[i][j].setText("");
bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//设置字体大小
bombButton[i][j].setForeground(Color.BLUE);
rightBomb = 0;
restBomb = BombNum;
restBlock = BlockNum - BombNum;
}
}
for (int i = 0; i BombNum; ) {
int x = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1));
int y = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1));
if (bombButton[x][y].isBomb != true) {
bombButton[x][y].isBomb = true;
i++;
}
}
CountRoundBomb();
}
/* 计算方块周围雷数 */
public void CountRoundBomb() {
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
int count = 0;
// 当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数
if (bombButton[i][j].isBomb != true) {
for (int x = i - 1; x i + 2; x++) {
for (int y = j - 1; y j + 2; y++) {
if ( (x = 0) (y = 0)
(x ( (int) Math.sqrt(BlockNum)))
(y ( (int) Math.sqrt(BlockNum)))) {
if (bombButton[x][y].isBomb == true) {
count++;
}
}
}
}
bombButton[i][j].BombRoundCount = count;
}
}
}
}
/* 是否挖完了所有的雷 */
public void isWin() {
restBlock = BlockNum - BombNum;
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
if (bombButton[i][j].isClicked == true) {
restBlock--;
}
}
}
if (rightBomb == BombNum || restBlock == 0) {
JOptionPane.showMessageDialog(this, "您挖完了所有的雷,您胜利了!", "胜利",
JOptionPane.INFORMATION_MESSAGE);
startBomb();
}
}
/** 当选中的位置为空,则翻开周围的地图* */
public void isNull(Bomb ClickedButton) {
int i, j;
i = ClickedButton.num_x;
j = ClickedButton.num_y;
for (int x = i - 1; x i + 2; x++) {
for (int y = j - 1; y j + 2; y++) {
if ( ( (x != i) || (y != j)) (x = 0) (y = 0)
(x ( (int) Math.sqrt(BlockNum)))
(y ( (int) Math.sqrt(BlockNum)))) {
if (bombButton[x][y].isBomb == false
bombButton[x][y].isClicked == false
bombButton[x][y].isRight == false) {
turn(bombButton[x][y]);
}
}
}
}
}
/* 翻开 */
public void turn(Bomb ClickedButton) {
ClickedButton.setEnabled(false);
ClickedButton.isClicked = true;
if (ClickedButton.BombRoundCount 0) {
ClickedButton.setText(ClickedButton.BombRoundCount + "");
}
else {
isNull(ClickedButton);
}
}
/* 左键点击 */
public void actionPerformed(ActionEvent e) {
if ( ( (Bomb) e.getSource()).isClicked == false
( (Bomb) e.getSource()).isRight == false) {
if ( ( (Bomb) e.getSource()).isBomb == false) {
turn( ( (Bomb) e.getSource()));
isWin();
}
else {
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
if (bombButton[i][j].isBomb == true) {
bombButton[i][j].setText("b");
}
}
}
( (Bomb) e.getSource()).setForeground(Color.RED);
( (Bomb) e.getSource()).setFont(new Font("", Font.BOLD, 20));
( (Bomb) e.getSource()).setText("X");
JOptionPane.showMessageDialog(this, "你踩到地雷了,按确定重来", "踩到地雷", 2);
startBomb();
}
}
}
/* 右键点击 */
public void mouseClicked(MouseEvent e) {
Bomb bombSource = (Bomb) e.getSource();
boolean right = SwingUtilities.isRightMouseButton(e);
if ( (right == true) (bombSource.isClicked == false)) {
bombSource.BombFlag = (bombSource.BombFlag + 1) % 3;
if (bombSource.BombFlag == 1) {
if (restBomb 0) {
bombSource.setForeground(Color.RED);
bombSource.setText("F");
bombSource.isRight = true;
restBomb--;
}
else {
bombSource.BombFlag = 0;
}
}
else if (bombSource.BombFlag == 2) {
restBomb++;
bombSource.setText("Q");
bombSource.isRight = false;
}
else {
bombSource.setText("");
}
if (bombSource.isBomb == true) {
if (bombSource.BombFlag == 1) {
rightBomb++;
}
else if (bombSource.BombFlag == 2) {
rightBomb--;
}
}
nowBomb.setText("当前雷数" + ":" + restBomb);
isWin();
}
}
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
}
}
class Frame1_start_actionAdapter
implements ActionListener {
private Frame adaptee;
Frame1_start_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.start_actionPerformed(e);
}
}
////////////////////////////
class Bomb
extends JButton {
int num_x, num_y; // 第几号方块
int BombRoundCount; // 周围雷数
boolean isBomb; // 是否为雷
boolean isClicked; // 是否被点击
int BombFlag; // 探雷标记
boolean isRight; // 是否点击右键
public Bomb(int x, int y) {
num_x = x;
num_y = y;
BombFlag = 0;
BombRoundCount = 9;
isBomb = false;
isClicked = false;
isRight = false;
}
}
class Bomb_actionAdapter
implements ActionListener {
private Frame adaptee;
Bomb_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.actionPerformed(e);
}
}
class Bomb_mouseAdapter
extends MouseAdapter {
private Frame adaptee;
Bomb_mouseAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.mouseClicked(e);
}
}
扫雷游戏的算法概述:
你可以把地雷所在的区域抽象成一个二维数组。数组里的元素是该地周围的雷数。然后根据玩家所设定的地雷个数用一个随机数生成器来撒雷。有雷的地方可以用-1来表示,没有雷的地方在撒雷的时候数它旁边的雷数,然后将值填入对应的数组元素里。这样,你的雷阵已经部署完毕。
接下来是玩家的操作了。如果你有仔细观察的话,当你点击一个位置的是后发生的情况有3种:
1. 如果该处有雷的话,很简单,游戏结束
2. 如果该处没有雷,但是这个地方附近有至少一个雷的话,只将当前的位置周围雷数显示出来。
3. 如果该处没有雷,并且附近也没有雷的话,就用一个循环或是递归的方法将它四周的没有雷的地方也显示出来。如果他四周的位置的四周也没有雷的话,继续这个过程,知道四周至少有一个雷时候停止。
玩家事件的处理:
玩家的事件有三种:单击,左右键同时点击,和右击。
单击可以想象成走到该处,如果有雷的话就游戏结束,没有的话就根据我以上讲的三点来判断该做什么。
左右键同时点击的话就将该处四周可能有雷的地方显示出来,如果玩家错将没有雷的地方放了小红旗的话,游戏结束。
右击是放小红旗。
当所有雷都被清楚的时候,游戏结束,玩家胜利。
刚写过这个代码
t java.util.Random;
import java.util.Scanner;
public class Mine {
public static void main(String[] args) {
int[][] m = new int[10][10];
Random r = new Random();
for(int i = 0;i10;++i)
for(int j = 0;j10;++j)
{
m[i][j] = r.nextInt(2);
}
Scanner sc = new Scanner(System.in);
System.out.println("输入坐标x和坐标y");
int x = sc.nextInt();
int y = sc.nextInt();
System.out.println("地雷的个数是:"+findMine(m,x,y));
for(int i1 = 0;i110;i1++)
{for(int j1 = 0;j110;j1++)
System.out.print(m[i1][j1]+" ");
System.out.println();
}
System.out.println();
}
public static int findMine(int[][] m,int x,int y){
int sum = 0;
if(m[x][y] == 1)
return -1;
if((x-1)0y-10)
if(m[x-1][y-1]==1)
sum++;
if((x-1)0)
if(m[x-1][y]==1)
sum++;
if(x-10y+1m[0].length)
if(m[x-1][y+1]==1)
sum++;
if(y-10)
if(m[x][y-1]==1)
sum++;
if(y+1m[0].length)
if(m[x][y+1]==1)
sum++;
if(x+1m.lengthy-10)
if(m[x+1][y-1]==1)
sum++;
if(x+1m.length)
if(m[x+1][y]==1)
sum++;
if(x+1m.lengthy+1m[0].length)
if(m[x+1][y+1]==1)
sum++;
return sum;
}
}