java实现简单窗口小游戏“扫雷”

JButton reset=new JButton(“重来”);
Container container=new Container();

//游戏数据结构
final int row=20;
final int col=20;
final int leiCount=30;
JButton [][] buttons=new JButton[row][col];
int [][] counts=new int[row][col];
final int LEICODE=10;

// 构造函数
public saolei(){
//1、设置窗口
frame.setSize(1000, 800);
frame.setResizable(true);
//是否可改变窗口大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
//2、添加重来按钮
addResetButton();
//添加按钮
addButtons();
//埋雷
addLei();
//添加雷的计算
calcNeiboLei();
frame.setVisible(true);
}
public void addResetButton(){
reset.setBackground(Color.cyan);
reset.setOpaque(true);
reset.addActionListener(this);
frame.add(reset,BorderLayout.NORTH);
}
public void addLei(){
Random rand=new Random();
int randRow,randCol;
for(int i=0;i<leiCount;i++){
randRow=rand.nextInt(row);
randCol=rand.nextInt(col);
if(counts[randRow][randCol]== LEICODE){
i–;
}else{
counts[randRow][randCol]=LEICODE;
//buttons[randRow][randCol].setText(“*”);

}
}
}
public void addButtons(){
frame.add(container,BorderLayout.CENTER);
container.setLayout(new GridLayout(row,col));
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
JButton button=new JButton();
button.setBackground(Color.orange);
button.setOpaque(true);
button.addActionListener(this);
buttons[i][j]=button;
container.add(button);
}
}
}
public void calcNeiboLei(){
int count;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
count=0;
if(counts[i][j]==LEICODE) continue;

if(i>0 && j>0 && counts[i-1][j-1]==LEICODE) count++;
if(i>0&&counts[i-1][j]==LEICODE) count++;
if(i>0 && j<19 && counts[i-1][j+1]==LEICODE) count++;
if(j>0 && counts[i][j-1]==LEICODE) count++;
if(j<19 && counts[i][j+1]==LEICODE) count++;
if(i<19&&j>0&&counts[i+1][j-1]==LEICODE) count++;
if(i<19&&counts[i+1][j]==LEICODE) count++;
if(i<19&&j<19&&counts[i+1][j+1]==LEICODE) count++;

counts[i][j]=count;
//buttons[i][j].setText(counts[i][j]+“”);
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton button=(JButton)e.getSource();
if(button.equals(reset)){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
buttons[i][j].setText(“”);
buttons[i][j].setEnabled(true);
buttons[i][j].setBackground(Color.orange);
counts[i][j]=0;
}
}
addLei();
calcNeiboLei();
}else{
int count=0;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(button.equals(buttons[i][j])){
count=counts[i][j];
if(count==LEICODE){
LoseGame();
}else{
openCell(i,j);
checkWin();
} return;
}
}
}

}
}
void checkWin(){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(buttons[i][j].isEnabled()==true && counts[i][j]!=LEICODE) return;
}
}
JOptionPane.showMessageDialog(frame, “握草牛啤,你竟然赢了!”);
}
void openCell(int i,int j){
if(buttons[i][j].isEnabled()==false) return;

buttons[i][j].setEnabled(false);
if(counts[i][j]==0){
if(i>0 && j>0 && counts[i-1][j-1]!=LEICODE) openCell(i-1, j-1);
if(i>0&&counts[i-1][j]!=LEICODE) openCell(i-1, j);
if(i>0 && j<19 && counts[i-1][j+1]!=LEICODE) openCell(i-1, j+1);
if(j>0 && counts[i][j-1]!=LEICODE) openCell(i, j-1);
if(j<19 && counts[i][j+1]!=LEICODE) openCell(i, j+1);
if(i<19&&j>0&&counts[i+1][j-1]!=LEICODE) openCell(i+1, j-1);
if(i<19&&counts[i+1][j]!=LEICODE) openCell(i+1, j);
if(i<19&&j<19&&counts[i+1][j+1]!=LEICODE) openCell(i+1, j+1);

buttons[i][j].setText(counts[i][j]+“”);
}else{
buttons[i][j].setText(counts[i][j]+“”);
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

言尽于此,完结

无论是一个初级的 coder,高级的程序员,还是顶级的系统架构师,应该都有深刻的领会到设计模式的重要性。

  • 第一,设计模式能让专业人之间交流方便,如下:

程序员A:这里我用了XXX设计模式

程序员B:那我大致了解你程序的设计思路了

  • 第二,易维护

项目经理:今天客户有这样一个需求…

程序员:明白了,这里我使用了XXX设计模式,所以改起来很快

  • 第三,设计模式是编程经验的总结

程序员A:B,你怎么想到要这样去构建你的代码

程序员B:在我学习了XXX设计模式之后,好像自然而然就感觉这样写能避免一些问题

  • 第四,学习设计模式并不是必须的

程序员A:B,你这段代码使用的是XXX设计模式对吗?

程序员B:不好意思,我没有学习过设计模式,但是我的经验告诉我是这样写的

image

从设计思想解读开源框架,一步一步到Spring、Spring5、SpringMVC、MyBatis等源码解读,我都已收集整理全套,篇幅有限,这块只是详细的解说了23种设计模式,整理的文件如下图一览无余!

image

搜集费时费力,能看到此处的都是真爱!
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
理的文件如下图一览无余!

[外链图片转存中…(img-RP3okaTm-1713294472656)]

搜集费时费力,能看到此处的都是真爱!
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 17
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java实现的简易扫雷小游戏的步骤: 1. 创建一个窗口,包含一个菜单栏和一个游戏区域。 2. 菜单栏包含难度选择和重新开始游戏的选项。 3. 游戏区域包含一个二维*********```java import javax.swing.*; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class Minesweeper extends JFrame { private static final int ROWS = 10; private static final int COLS = 10; private static final int MINES = 10; private static final int CELL_SIZE = 30; private static final int FRAME_WIDTH = COLS * CELL_SIZE; private static final int FRAME_HEIGHT = ROWS * CELL_SIZE + 22; private Cell[][] cells = new Cell[ROWS][COLS]; private int minesLeft = MINES; private JLabel statusbar; public Minesweeper() { initUI(); } private void initUI() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(ROWS, COLS)); for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { cells[row][col] = new Cell(row, col); panel.add(cells[row][col]); } } statusbar = new JLabel("Mines left: " + minesLeft); add(panel, BorderLayout.CENTER); add(statusbar, BorderLayout.SOUTH); setResizable(false); pack(); setTitle("Minesweeper"); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void newGame() { for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { cells[row][col].reset(); } } minesLeft = MINES; statusbar.setText("Mines left: " + minesLeft); } private void gameOver() { for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { cells[row][col].reveal(); } } JOptionPane.showMessageDialog(this, "Game over", "Minesweeper", JOptionPane.INFORMATION_MESSAGE); newGame(); } private void checkWin() { boolean win = true; for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { if (!cells[row][col].isMine() && !cells[row][col].isRevealed()) { win = false; } } } if (win) { JOptionPane.showMessageDialog(this, "You win!", "Minesweeper", JOptionPane.INFORMATION_MESSAGE); newGame(); } } private class Cell extends JPanel { private int row; private int col; private boolean mine; private boolean revealed; private boolean marked; public Cell(int row, int col) { this.row = row; this.col = col; this.mine = Math.random() < (double) MINES / (ROWS * COLS); this.revealed = false; this.marked = false; setPreferredSize(new Dimension(CELL_SIZE, CELL_SIZE)); setBackground(Color.LIGHT_GRAY); setBorder(BorderFactory.createLineBorder(Color.GRAY)); addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { if (SwingUtilities.isLeftMouseButton(e)) { if (mine) { setBackground(Color.RED); gameOver(); } else { reveal(); checkWin(); } } else if (SwingUtilities.isRightMouseButton(e)) { mark(); } } }); } public void reset() { this.mine = Math.random() < (double) MINES / (ROWS * COLS); this.revealed = false; this.marked = false; setBackground(Color.LIGHT_GRAY); } public boolean isMine() { return mine; } public boolean isRevealed() { return revealed; } public void reveal() { if (revealed || marked) { return; } revealed = true; if (mine) { setBackground(Color.RED); return; } int count = 0; for (int r = Math.max(0, row - 1); r <= Math.min(ROWS - 1, row + 1); r++) { for (int c = Math.max(0, col - 1); c <= Math.min(COLS - 1, col + 1); c++) { if (cells[r][c].isMine()) { count++; } } } if (count > 0) { setText(Integer.toString(count)); } else { setBackground(Color.WHITE); for (int r = Math.max(0, row - 1); r <= Math.min(ROWS - 1, row + 1); r++) { for (int c = Math.max(0, col - 1); c <= Math.min(COLS - 1, col + 1); c++) { cells[r][c].reveal(); } } } } public void mark() { if (revealed) { return; } marked = !marked; if (marked) { setBackground(Color.YELLOW); minesLeft--; } else { setBackground(Color.LIGHT_GRAY); minesLeft++; } statusbar.setText("Mines left: " + minesLeft); } } public static void main(String[] args) { EventQueue.invokeLater(() -> { Minesweeper game = new Minesweeper(); game.setVisible(true); }); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值