好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java实现连连看游戏课程设计

本文为大家分享了JAVA语言课程设计:连连看小游戏,供大家参考,具体内容如下

1.设计内容

界面中有5*10的界面,图中共有6种不同的图片,每两个相同的图片连接在一起,如果连线中转折的次数<=3次,两张图片可同时削掉,否则不能削去。

2.设计要求

色彩鲜艳,鼠标点击键好用,以固定时间将所有图片消掉为胜利,若时间到了,图片还有,则闯关失败。

3.设计思想

1)    搭建界面,首先搭建简单界面,可以先用按钮代替图片,并且行列可以先少做一些,如下图所示:
2)    每次用户选择两个图形,如果图形满足一定条件(两个图形一样,且这两个图形之间存在转弯少于3的路径),则两个图形都能消掉。给定任意具有相同图形的两个格子,我们需要寻找这两个格子之间在转弯最少的情况下,经过格子数目最少的路径。如果这个最优路径的转弯数目少于3 ,则这两个格子可以消去。
3)    定义消掉的方法,此方法可以实现,当执行消掉功能时,可以消除两个按钮。
4)    在检验两个方块能否消掉的时候,我们要让两个方块同时满足两个条件才行,就是两者配对并且连线成功。

分3种情况: (从下面的这三种情况,我们可以知道,需要三个检测,这三个检测分别检测一条直路经。这样就会有三条路经。若这三条路经上都是空按钮,那么就刚好是三种直线(两个转弯点)把两个按钮连接起来了)

* 1.相邻  
* 2. 若不相邻的先在第一个按钮的同行找一个空按钮。1).找到后看第二个按钮横向到这个空按钮所在的列是否有按钮。2).没有的话再看第一个按钮到与它同行的那个空按钮之间是否有按钮。3).没有的话,再从与第一个按钮同行的那个空按钮竖向到与第二个按钮的同行看是否有按钮。没有的话路经就通了,可以消了. 
* 3.若2失败后,再在第一个按钮的同列找一个空按钮。1).找到后看第二个按钮竖向到这个空按钮所在的行是否有按钮 2).没有的话,再看第一个按钮到与它同列的那个空按钮之间是否有按钮。3).没有的话,再从与第一个按钮同列的那个空按钮横向到与第二个按钮同列看是否有按钮。没有的话路经就通了,可以消了。

* 若以上三步都失败,说明这两个按钮不可以消去。

5)    在死锁的情况下,我们也可以暂时不终止游戏,而是随机打乱局面(即点击[重列]按钮),打破[死锁]局面。
6)    设计时间限制,即指定时间内没有消除全部按钮,则游戏结束
7)    设计过关模式,可以根据所有按钮都消除了,或是得分到达某一个阈值作为过关的依据。
8)    每一关的指定时间应该逐渐减少
9)    尝试将按钮换成图片,可以利用数组定义图片名称,然后把随机选择按钮变成随机选择数组下标,继而实现选择图片的功能

代码如下:

?

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

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

package test1;

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.GridLayout;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Timer;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class LianLianKan implements ActionListener {

     JFrame mainFrame; // 主面板

     Container thisContainer;

     JPanel centerPanel, southPanel, northPanel; // 子面板

     JButton diamondsButton[][] = new JButton[ 6 ][ 5 ]; // 游戏按钮数组

     JButton exitButton, resetButton, newlyButton; // 退出,重列,重新开始按钮

     JLabel fractionLable = new JLabel( "0" ); // 分数标签

     JButton firstButton, secondButton; // 分别记录两次被选中的按钮

     int grid[][] = new int [ 8 ][ 7 ]; // 储存游戏按钮位置

     int score= 0 ;

     static boolean pressInformation = false ; // 判断是否有按钮被选中

     int x0 = 0 , y0 = 0 , x = 0 , y = 0 , firstMsg = 0 , secondMsg = 0 ; // 游戏按钮的位置坐标

     int i, j, k, n; // 消除方法控制

     public void init() {

         mainFrame = new JFrame( "JKJ连连看" );

         thisContainer = mainFrame.getContentPane();

         thisContainer.setLayout( new BorderLayout());

         centerPanel = new JPanel();

         southPanel = new JPanel();

         northPanel = new JPanel();

         thisContainer.add(centerPanel, "Center" );

         thisContainer.add(southPanel, "South" );

         thisContainer.add(northPanel, "North" );

         centerPanel.setLayout( new GridLayout( 6 , 5 ));

         for ( int cols = 0 ; cols < 6 ; cols++) {

             for ( int rows = 0 ; rows < 5 ; rows++) {

                 diamondsButton[cols][rows] = new JButton(String.valueOf(grid[cols + 1 ][rows + 1 ]));

                 diamondsButton[cols][rows].addActionListener( this );

                 centerPanel.add(diamondsButton[cols][rows]);

             }

         }

         exitButton = new JButton( "退出" );

         exitButton.addActionListener( this );

         resetButton = new JButton( "重列" );

         resetButton.addActionListener( this );

         newlyButton = new JButton( "再来一局" );

         newlyButton.addActionListener( this );

         southPanel.add(exitButton);

         southPanel.add(resetButton);

         southPanel.add(newlyButton);

         fractionLable.setText( "分数:" +score);

         northPanel.add(fractionLable);

         int width=Toolkit.getDefaultToolkit().getScreenSize().width;

         int height=Toolkit.getDefaultToolkit().getScreenSize().height;

         mainFrame.setBounds((width- 500 )/ 2 ,(height- 500 )/ 2 , 500 , 500 );

         mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         mainFrame.setResizable( false );

         mainFrame.setVisible( true );

     }

     public void randomBuild() {

         int randoms, cols, rows;

         for ( int twins = 1 ; twins <= 15 ; twins++) {   //生成15个随机数

             randoms = ( int ) (Math.random() * 15 + 1 );

             for ( int alike = 1 ; alike <= 2 ; alike++) {   //每次将这个数放在两个位置

                 cols = ( int ) (Math.random() * 6 + 1 );

                 rows = ( int ) (Math.random() * 5 + 1 );   //随机生成数组坐标[cols,rows]

                 while (grid[cols][rows] != 0 ) {         //如果这个位置已经赋值了,重新生成。

                     cols = ( int ) (Math.random() * 6 + 1 );

                     rows = ( int ) (Math.random() * 5 + 1 );

                 }

                 this .grid[cols][rows] = randoms;     //将这个随机数赋到数组中。

             }

         }

     }

     //显示分数

     public void fraction() {

         score+= 100 ;

         fractionLable.setText(score+ "" );

     }

     //重列方法的实现

     public void reload() {

         int save[] = new int [ 50 ];

         int n = 0 , cols, rows;

         int grid[][] = new int [ 8 ][ 7 ];

         for ( int i = 0 ; i <= 6 ; i++) {

             for ( int j = 0 ; j <= 5 ; j++) {

                 if ( this .grid[i][j] != 0 ) {

                     save[n] = this .grid[i][j];

                     n++;

                 }

             }

         }

         n--;

         this .grid = grid;

         while (n >= 0 ) {

             cols = ( int ) (Math.random() * 6 + 1 );

             rows = ( int ) (Math.random() * 5 + 1 );

             while (grid[cols][rows] != 0 ) {   //如果已经赋值了,重新生成

                 cols = ( int ) (Math.random() * 6 + 1 );

                 rows = ( int ) (Math.random() * 5 + 1 );

             }

             this .grid[cols][rows] = save[n];

             n--;

         }

         mainFrame.setVisible( false );

         pressInformation = false ; // 这里一定要将按钮点击信息归为初始

         init();

         for ( int i = 0 ; i < 6 ; i++) {

             for ( int j = 0 ; j < 5 ; j++) {

                 if (grid[i + 1 ][j + 1 ] == 0 )

                     diamondsButton[i][j].setVisible( false );

             }

         }

     }

     public void estimateEven( int placeX, int placeY, JButton bz) {

         if (pressInformation == false ) { //如果以前没点击过

             x = placeX;

             y = placeY;       //记录这个按钮坐标[x,y]

             secondMsg = grid[x][y];

             secondButton = bz; //记录这个按钮的信息

             pressInformation = true ;

         } else {           //如果以前点击过

             x0 = x;    

             y0 = y;

             firstMsg = secondMsg;

             firstButton = secondButton;     //将上一次的button按钮信息赋给first

             x = placeX;

             y = placeY;

             secondMsg = grid[x][y];         //将这次点击按钮的信息记录下来

             secondButton = bz;

             if (firstMsg == secondMsg && secondButton != firstButton) {

                 xiao();

             }

         }

     }

     public void xiao() { // 相同的情况下能不能消去。仔细分析,不一条条注释

         if ((x0 == x && (y0 == y + 1 || y0 == y - 1 ))|| ((x0 == x + 1 || x0 == x - 1 ) && (y0 == y))) { // 判断是否相邻

             remove();

         } else {

             for (j = 0 ; j < 7 ; j++) {

                 if (grid[x0][j] == 0 ) { // 判断第一个按钮同行哪个按钮为空

                     if (y > j) { // 如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边

                         for (i = y - 1 ; i >= j; i--) { // 判断第二按钮左侧直到第一按钮中间有没有按钮

                             if (grid[x][i] != 0 ) {

                                 k = 0 ;    

                                 break ;

                             } else {

                                 k = 1 ;

                             } // K=1说明通过了第一次验证

                         }

                         if (k == 1 ) {

                             linePassOne();

                         }

                     }

                     if (y < j) { // 如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边

                         for (i = y + 1 ; i <= j; i++) { // 判断第二按钮左侧直到第一按钮中间有没有按钮

                             if (grid[x][i] != 0 ) {

                                 k = 0 ;

                                 break ;

                             } else {

                                 k = 1 ;

                             }

                         }

                         if (k == 1 ) {

                             linePassOne();

                         }

                     }

                     if (y == j) {

                         linePassOne();

                     }

                 }

                 if (k == 2 ) {

                     if (x0 == x) {

                         remove();

                     }

                     if (x0 < x) {

                         for (n = x0; n <= x - 1 ; n++) {

                             if (grid[n][j] != 0 ) {

                                 k = 0 ;

                             }

                             if (grid[n][j] == 0 && n == x - 1 ) {

                                 remove();

                             }

                         }

                     }

                     if (x0 > x) {

                         for (n = x0; n >= x + 1 ; n--) {

                             if (grid[n][j] != 0 ) {

                                 k = 0 ;

                                 break ;

                             }

                             if (grid[n][j] == 0 && n == x + 1 ) {

                                 remove();

                             }

                         }

                     }

                 }

             }

             for (i = 0 ; i < 8 ; i++) { // 列

                 if (grid[i][y0] == 0 ) {

                     if (x > i) {

                         for (j = x - 1 ; j >= i; j--) {

                             if (grid[j][y] != 0 ) {

                                 k = 0 ;

                                 break ;

                             } else {

                                 k = 1 ;

                             }

                         }

                         if (k == 1 ) {

                             rowPassOne();

                         }

                     }

                     if (x < i) {

                         for (j = x + 1 ; j <= i; j++) {

                             if (grid[j][y] != 0 ) {

                                 k = 0 ;

                                 break ;

                             } else {

                                 k = 1 ;

                             }

                         }

                         if (k == 1 ) {

                             rowPassOne();

                         }

                     }

                     if (x == i) {

                         rowPassOne();

                     }

                 }

                 if (k == 2 ) {

                     if (y0 == y) {

                         remove();

                     }

                     if (y0 < y) {

                         for (n = y0; n <= y - 1 ; n++) {

                             if (grid[i][n] != 0 ) {

                                 k = 0 ;

                                 break ;

                             }

                             if (grid[i][n] == 0 && n == y - 1 ) {

                                 remove();

                             }

                         }

                     }

                     if (y0 > y) {

                         for (n = y0; n >= y + 1 ; n--) {

                             if (grid[i][n] != 0 ) {

                                 k = 0 ;

                                 break ;

                             }

                             if (grid[i][n] == 0 && n == y + 1 ) {

                                 remove();

                             }

                         }

                     }

                 }

             }

         }

     }

     public void linePassOne() {

         if (y0 > j) { // 第一按钮同行空按钮在左边

             for (i = y0 - 1 ; i >= j; i--) { // 判断第一按钮同左侧空按钮之间有没按钮    

                 if (grid[x0][i] != 0 ) {

                     k = 0 ;

                     break ;

                 } else {

                     k= 2 ;

                 } // K=2说明通过了第二次验证

             }

         }

         if (y0 < j) { // 第一按钮同行空按钮在与第二按钮之间

             for (i = y0 + 1 ; i <= j; i++) {

                 if (grid[x0][i] != 0 ) {

                     k = 0 ;

                     break ;

                 } else {

                     k = 2 ;

                 }

             }

         }

     }

     public void rowPassOne() {

         if (x0 > i) {

             for (j = x0 - 1 ; j >= i; j--) {

                 if (grid[j][y0] != 0 ) {

                     k = 0 ;

                     break ;

                 } else {

                     k = 2 ;

                 }

             }

         }

         if (x0 < i) {

             for (j = x0 + 1 ; j <= i; j++) {

                 if (grid[j][y0] != 0 ) {

                     k = 0 ;

                     break ;

                 } else {

                     k = 2 ;

                 }

             }

         }

     }

     //消去按钮算法

     public void remove() {

         firstButton.setVisible( false );

         secondButton.setVisible( false );   //两个按钮不可见

         fraction();         //改变分数

         pressInformation = false ;         //点击按钮键清掉

         k = 0 ;                             //可消除标志清空

         grid[x0][y0] = 0 ;

         grid[x][y] = 0 ;                   //按钮键值清零

     }

     //事件响应

     public void actionPerformed(ActionEvent e) {

         if (e.getSource() == newlyButton) {

             int grid[][] = new int [ 8 ][ 7 ];

             this .grid = grid;

             randomBuild();

             score= 0 ;

             mainFrame.setVisible( false );

             pressInformation = false ;

             init();

         }

         if (e.getSource() == exitButton)

             System.exit( 0 );

         if (e.getSource() == resetButton)

             reload();

         for ( int cols = 0 ; cols < 6 ; cols++) {

             for ( int rows = 0 ; rows < 5 ; rows++) {

                 if (e.getSource() == diamondsButton[cols][rows])

                     estimateEven(cols + 1 , rows + 1 , diamondsButton[cols][rows]);

             }

         }

     }

     public static void main(String[] args) {

         LianLianKan llk = new LianLianKan();

         llk.randomBuild(); //随机分配键值

         llk.init(); //初始化

     }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/weixin_61149547/article/details/122789361

查看更多关于java实现连连看游戏课程设计的详细内容...

  阅读:21次