今天借鉴网上写了一个扫雷游戏
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Random; public class SaoLei implements MouseListener,ActionListener { JPanel p= new JPanel(); JFrame frame= new JFrame( "扫雷" ); @SuppressWarnings ( "rawtypes" ) JComboBox combobox= new JComboBox(); JButton reset= new JButton( "重新开始" ); Container container= new Container(); SaoLeiConstant constant= new SaoLeiConstant(); JButton[][] buttons= new JButton[constant.row][constant.col]; //按钮 int [][] counts= new int [constant.row][constant.col]; public SaoLei(){ //构造 frame.setSize( 600 , 700 ); frame.setResizable( false ); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout( new BorderLayout()); addtopButton(); //选择 addButtons(); addLei(); //加雷 calcNeiboLei(); //雷数 frame.setVisible( true ); } void addtopButton() { p.removeAll(); p.add(reset); reset.setBackground(Color.green); reset.setOpaque( true ); reset.addActionListener( this ); //combobox.addItem("选择难度"); combobox.addItem( "新手难度" ); combobox.addItem( "初级难度" ); combobox.addItem( "中级难度" ); combobox.addItem( "高级难度" ); combobox.addItem( "大师难度" ); combobox.setBackground(Color.GREEN); combobox.setOpaque( true ); combobox.addItemListener( new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { String item = e.getItem().toString(); if (item == "新手难度" ) { constant.leiCount = 20 ; ResetGame(); } else if (item == "初级难度" ) { constant.leiCount = 43 ; ResetGame(); } else if (item == "中级难度" ) { constant.leiCount = 63 ; ResetGame(); } else if (item == "高级难度" ) { constant.leiCount = 99 ; ResetGame(); } else if (item == "大师难度" ) { constant.leiCount = 119 ; ResetGame(); } } }); p.add(combobox); frame.add(p, BorderLayout.NORTH); } void addLei() { Random rand = new Random(); int randRow,randCol; for ( int i= 0 ; i<constant.leiCount; i++) { randRow = rand.nextInt(constant.row); randCol = rand.nextInt(constant.col); if (counts[randRow][randCol] == constant.LEICODE) { i--; } else { counts[randRow][randCol] = constant.LEICODE; //buttons[randRow][randCol].setText("X"); } } } void addButtons() { frame.add(container,BorderLayout.CENTER); container.setLayout( new GridLayout(constant.row,constant.col)); for ( int i= 0 ;i<constant.row;i++) { for ( int j= 0 ;j<constant.col;j++) { JButton button = new JButton(); button.setBackground(Color.white); button.setOpaque( true ); button.addActionListener( this ); button.addMouseListener((MouseListener) this ); buttons[i][j] = button; container.add(button); } } } void calcNeiboLei() { int count; for ( int i= 0 ;i<constant.row;i++) { for ( int j= 0 ;j<constant.col;j++) { count = 0 ; if (counts[i][j] == constant.LEICODE) continue ; if (i> 0 && j> 0 && counts[i- 1 ][j- 1 ] == constant.LEICODE) count++; if (i> 0 && counts[i- 1 ][j] == constant.LEICODE) count++; if (i> 0 && j< 19 &&counts[i- 1 ][j+ 1 ] == constant.LEICODE) count++; if (j> 0 && counts[i][j- 1 ] == constant.LEICODE) count++; if (j< 19 && counts[i][j+ 1 ] == constant.LEICODE) count++; if (i< 19 && j> 0 && counts[i+ 1 ][j- 1 ] == constant.LEICODE) count++; if (i< 19 && counts[i+ 1 ][j] == constant.LEICODE) count++; if (i< 19 && j< 19 && counts[i+ 1 ][j+ 1 ] == constant.LEICODE) count++; counts[i][j] = count; buttons[i][j].setMargin( new Insets( 0 , 0 , 0 , 0 )); //让按钮随按钮上的图案变化 //buttons[i][j].setText(counts[i][j] + ""); } } } @Override public void actionPerformed(ActionEvent e) { JButton button = (JButton)e.getSource(); if (button.equals(reset)) { ResetGame(); //重新开始游戏 } else { int count = 0 ; for ( int i= 0 ;i<constant.row;i++) { for ( int j= 0 ;j<constant.col;j++) { if (button.equals(buttons[i][j])) { count = counts[i][j]; if (count == constant.LEICODE) { loseGame(); } else { openCell(i,j); checkWin(); } return ; } } } } } public void mouseClicked(MouseEvent e){ JButton button=(JButton)e.getSource(); if (e.getButton()==MouseEvent.BUTTON3){ for ( int i= 0 ;i<constant.row;i++){ for ( int j= 0 ;i<constant.col;j++){ if (button.equals(buttons[i][j])){ if (buttons[i][j].isEnabled()== true ){ buttons[i][j].setMargin( new Insets( 0 , 0 , 0 , 0 )); buttons[j][j].setText( "?" ); return ; } } } } } } void ResetGame() { for ( int i= 0 ;i<constant.row;i++) { for ( int j= 0 ;j<constant.col;j++) { buttons[i][j].setText( "" ); buttons[i][j].setEnabled( true ); buttons[i][j].setBackground(Color.white); counts[i][j] = 0 ; } } addLei(); calcNeiboLei(); } void checkWin() { for ( int i= 0 ;i<constant.row;i++) { for ( int j= 0 ;j<constant.col;j++) { if (buttons[i][j].isEnabled() == true && counts[i][j] != constant.LEICODE ) return ; } } JOptionPane.showMessageDialog(frame, "Yeah,你赢了!" ); } //使用递归方法打开格子 void openCell( int i, int j) { if (buttons[i][j].isEnabled() == false ) return ; buttons[i][j].setBackground(Color.yellow); buttons[i][j].setOpaque( true ); buttons[i][j].setEnabled( false ); if (counts[i][j] == 0 ) { if (i> 0 && j> 0 && counts[i- 1 ][j- 1 ] != constant.LEICODE) openCell(i- 1 ,j- 1 ); if (i> 0 && j< 19 && counts[i- 1 ][j] != constant.LEICODE) openCell(i- 1 ,j); if (i> 0 && j< 19 &&counts[i- 1 ][j+ 1 ] != constant.LEICODE) openCell(i- 1 ,j+ 1 ); if (j> 0 && counts[i][j- 1 ] != constant.LEICODE) openCell(i,j- 1 ); if (j< 19 && counts[i][j+ 1 ] != constant.LEICODE) openCell(i,j+ 1 ); if (i< 19 && j> 0 && counts[i+ 1 ][j- 1 ] != constant.LEICODE) openCell(i+ 1 ,j- 1 ); if (i< 19 && counts[i+ 1 ][j] != constant.LEICODE) openCell(i+ 1 ,j); if (i< 19 && j< 19 && counts[i+ 1 ][j+ 1 ] != constant.LEICODE) openCell(i+ 1 ,j+ 1 ); } else { buttons[i][j].setMargin( new Insets( 0 , 0 , 0 , 0 )); buttons[i][j].setText(counts[i][j] + "" ); } } void loseGame() { for ( int i= 0 ;i<constant.row;i++) { for ( int j= 0 ;j<constant.col;j++) { int count = counts[i][j]; if (count == constant.LEICODE) { buttons[i][j].setMargin( new Insets( 0 , 0 , 0 , 0 )); buttons[i][j].setText( "雷" ); buttons[i][j].setBackground(Color.red); buttons[i][j].setEnabled( false ); } else { buttons[i][j].setMargin( new Insets( 0 , 0 , 0 , 0 )); buttons[i][j].setText(count + "" ); buttons[i][j].setEnabled( false ); } } } JOptionPane.showMessageDialog(frame, "error,你输了!" ); } public static void main(String[] args) { new SaoLei(); } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } } |
初始数据定义
1 2 3 4 5 6 7 | public class SaoLeiConstant { final int row= 20 ; //行数 final int col= 20 ; //列数 final int LEICODE= 10 ; //定义雷下放的数字 protected int temp= 20 ; protected int leiCount=temp; //雷数量 } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库