JAVA实验报告3-“连连看”游戏程序设计

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

目录

一、实验目的与要求 (2)

二、实验方案 (2)

三、实验结果和数据处理 (3)

四、结论 (20)

五、问题与讨论 (20)

一、实验目的与要求

(1)实验目的:通过在指定的窗口界面完成“连连看”小游戏程序的编写和调试,加深对面向对象程序设计的理解。

(2)实验要求:按照Java程序设计教程的要求完成第三章中“连连看”游戏程序的编写和调试。

二、实验方案

定义了一个lianliankan类,实现了接口ActionListener:

①main()方法:主函数;

②actionPerformed()方法,用来实现重来一局按钮的响应事件;

③go()方法:初始化界面,排列图形;

④ex()方法:设置“退出游戏”窗体界面;

⑤suiji()方法:产生随机数,来填充游戏界面对应的数组的各个位置;

⑥chonglie()方法:当无符合条件的图形可消去时,需要重新排列图形;

⑦ling()方法:将数组中为零的成员所对应的按钮消去(设为不可见);

⑧wei()方法:判断并记录每次单击按钮的一些信息;

⑨xiao()方法:判断两个按钮在对应数组元素的值相同时能不能消去。

三、实验结果和数据处理

整个游戏最重要的方法,xiao()方法,用来判断两个按钮在对应数组元素的值相同时能不能消去:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

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];//储存游戏按钮位置

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

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

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(String.valueOf(Integer.parseInt(fractionLable.getText()))); northPanel.add(fractionLable);

mainFrame.setBounds(280,100,500,450);

mainFrame.setVisible(true);

}

public void randomBuild() {

int randoms,cols,rows;

for(int twins=1;twins<=15;twins++) {

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

for(int alike=1;alike<=2;alike++) {

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]=randoms;

}

}

}

public void fraction(){

相关文档
最新文档