JAVA课程设计连连看最终版.docx
《JAVA课程设计连连看最终版.docx》由会员分享,可在线阅读,更多相关《JAVA课程设计连连看最终版.docx(18页珍藏版)》请在冰豆网上搜索。
JAVA课程设计连连看最终版
JAVA课程设计报告
实验题目:
“连连看”游戏程序设计
小组成员:
王飞铭、毛建平、陈银银、黄庭威
成员分工:
组长:
王飞铭
王飞铭:
actionPerformed():
重来一局按钮的响应事件及remove()判断移去方法
毛建平:
xiao()消去方法、estimateEven()方法
陈银银:
init()方法:
游戏主界面和main()方法实现
黄庭威:
randomBuild()产生随机数方法、fraction()刷新方法
(一):
内容:
1:
功能需求和分析:
(1)该游戏界面为:
数字方格类型,由6竖7横的直线平行垂直交叉而组成,分别是6行5列凹方块拼接,共有30格小方块。
方块上随机分布一些数字,数字的要求是至少两两相同,位置随机打乱。
(2)游戏规则是:
将相同数字的方块连接,但要满足只能至少单边无阻碍呈直线趋势连接,否则视为无效,如果一对数字连接成功,那么就会消失在界面,并且分数标签会加分,继续游戏,直到游戏结束。
(3)帮助项目:
在游戏过程中,如果出现没有可以连接的数字对时候,可按界面下方按钮刷新重新排列,便可以可继续游戏。
(4)退出游戏:
可以鼠标点击“退出”按钮,结束游戏。
(5)再来一局:
本局结束或者中途不想继续玩本局,可以点击界面下方的再来一局,系统会自动再次重新开始。
(二):
主要方法:
首先我们定义了一个lianliankan类,实现了接口ActionListener:
1)init()方法:
实现游戏主界面;
2)randomBuild()方法:
用来产生游戏按钮上的随机数;
3)fraction()方法:
游戏界面最上面用来加分;
4)reload()方法:
用来刷新,重载,窗体界面;
5)estimateEven()方法:
判断按钮数字是否满足消去的条件linePassOne()方法:
判断第一按钮同左右侧空按钮之间
rowPassOne()方法:
判断第一按钮同列空按钮与第二按钮;
6)actionPerformed(ActionEvente)方法:
用来实现重来一
局按钮的响应事件;
7)main(String[]args)方法:
主函数;
8)xiao()方法:
消去方法
(三):
界面要求:
用图形界面实现,参考下
系统流程图如下
(四):
代码设计
package数字版;
/**本游戏是连连看,上面出现的都是数字,
点击两个相邻的或者之间没有第三个的,便可以消去。
。
。
如果不能消除完,便可以按重列,
这样会把剩余的重新排列。
便可以继续玩了,
或者您可以按重置,重新再来一局!
!
!
!
*/
importjavax.swing.*;//调用图形界面类布局
importjava.awt.*;
publicclasslianliankanimplementsActionListener
{//继承一个监听器
JFramemainFrame;//游戏主面板用布局管理器
ContainerthisContainer;//一个容器
JPanelcenterPanel,southPanel,northPanel;//子面板
JButtondiamondsButton[][]=newJButton[6][5];//游戏按钮数组
JButtonexitButton,resetButton,newlyButton;//退出,重列,重新开始按钮
JLabelfractionLable=newJLabel("0");//分数标签显示您玩游戏得了多少分!
!
!
!
!
!
!
!
)
JButtonfirstButton,secondButton;//分别记录两次被选中的按钮
intgrid[][]=newint[8][7];//储存游戏按钮位置
staticbooleanpressInformation=false;//判断是否有按钮被选中
intx0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV;//游戏按钮的位置坐标
inti,j,k,n;//消除方法控制
publicvoidinit(){
mainFrame=newJFrame("数字版连连看");
thisContainer=mainFrame.getContentPane();
thisContainer.setLayout(newBorderLayout());
//setLayout()设置用户界面上的屏幕组件的格式布局,是java图形界面编程的常用方法。
此处是方位布局
centerPanel=newJPanel();//中间部分(用来放游戏显示内容)
southPanel=newJPanel();//下面部分(用来放退出,重列,重新开始按钮)
northPanel=newJPanel();//上面部分(用来记录得分情况)
centerPanel.setBackground(newColor(145,145,44));//灰色
northPanel.setBackground(newColor(245,252,252));//颜色red
thisContainer.add(centerPanel,"Center");
thisContainer.add(southPanel,"South");
thisContainer.add(northPanel,"North");
centerPanel.setLayout(newGridLayout(6,5));//设置游戏图标
for(intcols=0;cols<6;cols++){
for(introws=0;rows<5;rows++){
diamondsButton[cols][rows]=newJButton(String.valueOf(grid[cols+1][rows+1]));//换下一行
diamondsButton[cols][rows].setBackground(newColor(199,190,33));//颜色
diamondsButton[cols][rows].setBorder(BorderFactory.createLoweredBevelBorder());
diamondsButton[cols][rows].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton=newJButton("退出");
exitButton.setPreferredSize(newDimension(55,30));
exitButton.setBorder(BorderFactory.createRaisedBevelBorder());
exitButton.setBackground(newColor(25,155,100));//颜色绿色
exitButton.addActionListener(this);
resetButton=newJButton("刷新");
resetButton.setPreferredSize(newDimension(55,30));
resetButton.setBorder(BorderFactory.createRaisedBevelBorder());
resetButton.setBackground(newColor(77,175,100));//蓝色
resetButton.addActionListener(this);
newlyButton=newJButton("再来一局");
newlyButton.setPreferredSize(newDimension(100,33));
newlyButton.setBorder(BorderFactory.createRaisedBevelBorder());
newlyButton.setBackground(newColor(255,192,203));//粉色
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(300,100,500,450);
mainFrame.setVisible(true);
}
publicvoidrandomBuild(){//产生随机数
intrandoms,cols,rows;
for(inttwins=1;twins<=15;twins++){
randoms=(int)(Math.random()*25+1);
for(intalike=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;
}
}
}
publicvoidfraction(){
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));
}
publicvoidreload(){
intsave[]=newint[30];
intn=0,cols,rows;
intgrid[][]=newint[8][7];//双重循环,输出二维数组!
!
!
for(inti=0;i<=6;i++){
for(intj=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()*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(inti=0;i<6;i++){
for(intj=0;j<5;j++){
if(grid[i+1][j+1]==0)
diamondsButton[i][j].setVisible(false);
}
}
}
publicvoidestimateEven(intplaceX,intplaceY,JButtonbz){
if(pressInformation==false){
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
pressInformation=true;
}
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();//调用消掉的方法
}
}
}
publicvoidxiao(){//相同的情况下能不能消去。
仔细分析,不一条条注释
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(yfor(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(x0for(n=x0;n<=x-1;n++){
if(grid[n][j]!
=0){
k=0;
break;
}
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
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(y0for(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();
}
}
}
}
}
}
}
publicvoidlinePassOne(){
if(y0>j){//第一按钮同行空按钮在左边
for(i=y0-1;i>=j;i--){//判断第一按钮同左侧空按钮之间有没按钮
if(grid[x0][i]!
=0){
k=0;
break;
}
else{k=2;}//K=2说明通过了第二次验证
}
}
if(y0for(i=y0+1;i<=j;i++){
if(grid[x0][i]!
=0){
k=0;
break;
}
else{k=2;}
}
}
}
publicvoidrowPassOne(){
if(x0>i){
for(j=x0-1;j>=i;j--){
if(grid[j][y0]!
=0){
k=0;
break;
}
else{k=2;}
}
}
if(x0
for(j=x0+1;j<=i;j++){
if(grid[j][y0]!
=0){
k=0;
break;
}
else{k=2;}
}
}
}
publicvoidremove(){
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation=false;
k=0;
grid[x0][y0]=0;
grid[x][y]=0;
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==newlyButton){
intgrid[][]=newint[8][7];
this.grid=grid;
randomBuild();
mainFrame.setVisible(false);
pressInformation=false;
init();
}
if(e.getSource()==exitButton)
System.exit(0);
if(e.getSource()==resetButton)
reload();
for(intcols=0;cols<6;cols++){
for(introws=0;rows<5;rows++){
if(e.getSource()==diamondsButton[cols][rows])
estimateEven(cols+1,rows+1,diamondsButton[cols][rows]);
}
}
}
publicstaticvoidmain(String[]args){//主方法
lianliankanllk=newlianliankan();//实例化
llk.randomBuild();//调用
llk.init();
}
}
关节点:
a.Java的接口技术
b.Java的事件处理机制
c.Java的布局管理
(四):
总结
通过java这次实训学习,我从中学到了许多新的知识,而且通过这次实训设计,培养了我从多门课程中获取知识、迅速规划并开发出目标系统的能力,也锻炼了我们团队合作协调的能力,以及编程能力也有了很大的提高。
另外也有许多心得体会,在系统开发过程真的很艰难,也非常辛苦,特别是其中不得不做一些功能性甚至系统结构性方面的变动,将面对许多重复枯燥性的工作。