1、 JAVA课程设计报告 实验题目: “连连看”游戏程序设计 小组成员: 王飞铭、毛建平、陈银银、黄庭威 成员分工: 组长:王飞铭 王飞铭:actionPerformed():重来一局按钮的响应事件及remove()判断移去方法 毛建平:xiao()消去方法、estimateEven()方法 陈银银:init()方法:游戏主界面和main()方法实现 黄庭威:randomBuild() 产生随机数方法、fraction()刷新方法 (一):内容: 1:功能需求和分析: (1) 该游戏界面为:数字方格类型,由6竖7横的直线平行垂直交叉而组成,分别是6行5列凹方块拼接,
2、共有30格小方块。方块上随机分布一些数字,数字的要求是至少两两相同,位置随机打乱。 (2) 游戏规则是:将相同数字的方块连接,但要满足只能至少单边无阻碍呈直线趋势连接,否则视为无效,如果一对数字连接成功,那么就会消失在界面,并且分数标签会加分,继续游戏,直到游戏结束。 (3)帮助项目:在游戏过程中,如果出现没有可以连接的数字对时候,可按界面下方按钮刷新重新排列,便可以可继续游戏。 (4)退出游戏: 可以鼠标点击 “退出”按钮,结束游戏。 (5) 再来一局:本局结束或者中途不想继续玩本局,可以点击界面下方的再来一局,系统会自动再次重新开始。 (二):主要方法: 首先我们定义了
3、一个lianliankan类,实现了接口ActionListener: 1) init()方法:实现游戏主界面; 2) randomBuild()方法:用来产生游戏按钮上的随机数; 3) fraction()方法:游戏界面最上面用来加分; 4) reload()方法:用来刷新,重载,窗体界面; 5) estimateEven()方法:判断按钮数字是否满足消去的条件 linePassOne()方法:判断第一按钮同左右侧空按钮之间⑦rowPassOne()方法:判断第一按钮同列空按钮与第二按钮; 6) actionPerformed(Ac
4、tionEvent e)方法:用来实现重来一 局按钮的响应事件; 7) main(String[] args)方法:主函数; 8) xiao()方法:消去方法 (三):界面要求:用图形界面实现,参考下 系统流程图如下 开始 初始化 设置开始界面 画表格 初始化数值 游戏 游戏是否结束 显示游戏结束画面 游戏结束 NO Yes (四):代码设计 package 数字版; /**本游戏是连连看,上面出现的都是数字, 点击两个相邻的或者之间没有第三个的,便可以消去。。。 如果不能消除完,便可以按重列, 这样会把剩余的重新排列。便
5、可以继续玩了, 或者您可以按重置,重新再来一局!!!!*/ import javax.swing.*; //调用图形界面类布局 import java.awt.*; import java.awt.event.*; public class lianliankan implements ActionListener { //继承一个监听器 JFrame mainFrame; //游戏主面板 用布局管理器 Container thisContainer; //一个容器 JPanel centerPanel,southPanel,northPanel; //子面板
6、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 pressI
7、nformation=false; //判断是否有按钮被选中 int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标 int i,j,k,n;//消除方法控制 public void init(){ mainFrame=new JFrame("数字版连连看"); thisContainer = mainFrame.getContentPane(); thisContainer.setLayout(new BorderLayout()); //setLayout()设置用户界面上的屏幕组件
8、的格式布局,是java图形界面编程的常用方法。此处是方位布局 centerPanel=new JPanel(); //中间部分(用来放游戏显示内容) southPanel=new JPanel(); //下面部分(用来放退出,重列,重新开始按钮) northPanel=new JPanel(); //上面部分(用来记录得分情况) centerPanel.setBackground(new Color(145,145,44));//灰色 northPanel.setBackground(new Color(245,252,252));//颜色red thisContainer.
9、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[c
10、ols+1][rows+1])); //换下一行 diamondsButton[cols][rows].setBackground(new Color(199,190,33));//颜色 diamondsButton[cols][rows].setFont(new java.awt.Font("黑体", 1, 22)); diamondsButton[cols][rows].setBorder(BorderFactory.createLoweredBevelBorder()); diamondsButton[cols][rows].addActionListener(this);
11、 centerPanel.add(diamondsButton[cols][rows]); } } exitButton=new JButton("退出"); exitButton.setFont(new java.awt.Font("黑体", 1, 16)); exitButton.setPreferredSize(new Dimension(55,30)); exitButton.setBorder(BorderFactory.createRaisedBevelBorder()); exitButton.setBackground(new Color(25,155,
12、100));//颜色绿色 exitButton.addActionListener(this); resetButton=new JButton("刷新"); resetButton.setFont(new java.awt.Font("黑体", 1, 16)); resetButton.setPreferredSize(new Dimension(55,30)); resetButton.setBorder(BorderFactory.createRaisedBevelBorder()); resetButton.setBackground(new Color(77, 175
13、100));//蓝色 resetButton.addActionListener(this); newlyButton=new JButton("再来一局"); newlyButton.setFont(new java.awt.Font("黑体", 1, 16)); newlyButton.setPreferredSize(new Dimension(100,33)); newlyButton.setBorder(BorderFactory.createRaisedBevelBorder()); newlyButton.setBackground(new Color(255,
14、192,203));//粉色 newlyButton.addActionListener(this); //添加了监听器 !!!!!!当点击时候,用那个来通知 southPanel.add(exitButton); southPanel.add(resetButton); southPanel.add(newlyButton); fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText()))); //用来计算得分数,不断变化 northPanel.add(fractionLa
15、ble); mainFrame.setBounds(300,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
16、)(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(){ fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100)); } public void
17、reload() { int save[] = new int[30]; 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=n-1; this.grid=grid; while(n>=0) { cols=(int)(Math.random(
18、)*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++){
19、 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; secondMsg=grid[x][y]; secondButton=bz; pressInformation=true; }
20、else { x0=x; y0=y; fristMsg=secondMsg; firstButton=secondButton; x=placeX; y=placeY; secondMsg=grid[x][y]; secondButton=bz; if(fristMsg==secondMsg && secondButton!=firstButton){ xiao(); //调用消掉的方法 } } } public void xiao() { //相同的情况下能不能消去。仔细分析,不一条条注释 if((x0==x &&(y0==y+1||
21、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; } //
22、K=1说明通过了第一次验证
}
if (k==1) {
linePassOne();
}
}
if (y 23、k==2) {
if (x0==x) {
remove();
}
if (x0 24、emove();
}
}
}
}
}
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 25、
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (x==i) {
rowPassOne();
}
}
if (k==2){
if (y0==y) {
remove();
}
if (y0 26、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= 27、2; } //K=2说明通过了第二次验证
}
}
if (y0 28、f (x0 29、 actionPerformed(ActionEvent e) {
if(e.getSource()==newlyButton){
int grid[][] = new int[8][7];
this.grid = grid;
randomBuild();
mainFrame.setVisible(false);
pressInformation=false;
init();
}
if(e.getSource()==exitButton)
System.exit(0);
if(e.getSource()==resetButton)
reload( 30、);
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();
}
}
关节点:
a. Java的接口技术
b. Java的事件处理机制
c. Java的布局管理
(四):总结
通过java这次实训学习,我从中学到了许多新的知识,而且通过这次实训设计,培养了我从多门课程中获取知识、迅速规划并开发出目标系统的能力,也锻炼了我们团队合作协调的能力,以及编程能力也有了很大的提高。另外也有许多心得体会,在系统开发过程真的很艰难,也非常辛苦,特别是其中不得不做一些功能性甚至系统结构性方面的变动,将面对许多重复枯燥性的工作。
22






