资源描述:
《Java课程设计报告蜘蛛纸牌资料》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
Java课程设计——蜘蛛纸牌游戏(陈浩数学与应用数学专业091班)摘要本游戏通过java语言编写,实现了游戏的难度选择、开局、重新发牌以及可视化的相关操作界面。并通过设计鼠标事件,引入线程,构建游戏程序框架完成了对该游戏的难度选择、开局、发牌、显示可执行操作的各个基本功能。关键词蜘蛛纸牌java语言操作界面1.问题重述该游戏通过鼠标操作,将电脑多次分发的纸牌,按照相同的花色由大到小排列起来,直到桌面上的纸牌全都消失,即为胜利。包括如下功能:a.难度选择:包括简单(单色)、中级(双色)、高级(四色)。b.开局:任何时候可以重新开始一盘新的游戏。c.发牌。d.显示可行操作:提示当前可行操作。e.简单的帮助文档:对软件的简单介绍和编制说明。2.理论分析参照windows游戏蜘蛛纸牌,自己编写设计实现其功能,它具有如下一些功能:a.设计一个游戏界面,包括玩牌区、发牌区和回收区。b.纸牌以及其背景的设定。C.移动纸牌。使用鼠标将较小的纸牌拖放到较大的纸牌下方,可以拖动一组纸牌,当要求这组纸牌是同一个花色,并且这一组纸牌从上到下是从大到小排列的,且不间断。d.回收纸牌。当某列出现同一花色,从上到下依次是从K到A的不间组合时,这组纸牌将被回收到回收区中。e.发牌。用鼠标单击发牌区,若发牌区还有剩余的纸牌,则将发出一叠纸牌共10张,依次添加到玩牌区的10列纸牌最下方,但要求这10列纸牌没有空白列。f.若玩家不了解游戏规则可以点击帮助获得游戏方法。g.级别设定。点击“游戏”菜单中选取游戏难以级别,有3种级别,分别对应初级、中级、高级。h.退出游戏。34 3.模型该游戏可分为以下各项功能:a.Spider.java用于实现纸牌初始化、设置游戏等级以及异常处理。b.PKCard.java用于定义纸牌的显示的各种属性以及鼠标事件的相应属性。c.SpiderMenu..java用于添加游戏下拉菜单的各个选项,包含图形用户界面的构建,组件监听的实现,以及显示可执行操作的线程。d.SpiderMenu.class生成Spider.class,AboutDialog.class和PKCard.class4.程序流程图5.程序设计技术文档编写程序以实现游戏蜘蛛纸牌的功能。1、设计游戏界面。2、纸牌以及其背景的设定。3、移动纸牌。4、回收纸牌。34 5、发牌。6、若玩家不了解游戏规则可以点击帮助获得游戏方法。7、级别设定。8、退出游戏。该游戏的开发及相关功能的实现需要在Eclipse下建立javaproject,同时编写Spider.java、PKCard.java、SpiderMenu..java、AboutDialog.class,另外将纸牌的图片文件保存在images文件夹中。6.程序使用说明6.1主要界面:以下是主菜单界面的主要代码:JMenujNewGame=newJMenu("游戏");JMenujHelp=newJMenu("帮助");JMenuItemjItemAbout=newJMenuItem("关于");JMenuItemjItemOpen=newJMenuItem("开始新游戏");JMenuItemjItemDeal=newJMenuItem("发牌");JMenuItemjItemExit=newJMenuItem("退出");JRadioButtonMenuItemjRMItemEasy=newJRadioButtonMenuItem("初级:单花色");//菜单中的可选项按钮JRadioButtonMenuItemjRMItemNormal=newJRadioButtonMenuItem("中级:双花色");JRadioButtonMenuItemjRMItemHard=newJRadioButtonMenuItem("高级:四花色");publicSpiderMenuBar(Spiderspider){this.main=spider;jNewGame.add(jItemOpen);jNewGame.add(jItemDeal);jNewGame.addSeparator();//添加划分线jNewGame.add(jRMItemEasy);jNewGame.add(jRMItemNormal);jNewGame.add(jRMItemHard);jNewGame.addSeparator();jNewGame.add(jItemExit);ButtonGroupgroup=newButtonGroup();//(用来获取JRadioButtonMenuItem的值)group.add(jRMItemEasy);group.add(jRMItemNormal);group.add(jRMItemHard);jHelp.add(jItemAbout);34 this.add(jNewGame);this.add(jHelp);//添加到菜单栏以下是游戏窗口界面初始化的主要代码:pane=this.getContentPane();//获得容器pane.setBackground(newColor(0,112,26));//设置背景颜色clickLabel=newJLabel();//发牌区的鼠标点击响应区域clickLabel.setBounds(890,580,110,96);//设置响应区域大小与坐标clickLabel.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));pane.add(clickLabel);//将响应区域添加到容器中this.initCards();//纸牌初始化this.randomCards();//随机分配纸牌this.setCardsLocation();//为纸牌设置坐标位置this.deal();//从右下角发出一叠纸牌。游戏刚开始时,需要先发出一叠纸牌。groundLabel=newJLabel[10];//创建背景框数组,一共有10个背景框。intx=20;//背景框的x坐标,10个背景框是并排放置的,y坐标相等。第一个背景框的x坐标为20。for(inti=0;i<10;i++){//循环创建背景框,并设置其坐标大小等属性groundLabel[i]=newJLabel();//创建背景框groundLabel[i].setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));//为Label设置边框//,//形成背景框groundLabel[i].setBounds(x,25,71,96);//设置背景框的大小与坐标x+=101;//相邻的背景框的x坐标差值为101。this.pane.add(groundLabel[i]);//将背景框添加到容器中}this.setVisible(true);//显示窗口6.2初始化纸牌及发牌操作:初始化纸牌的主要代码(包括所有纸牌生成及存入数组,右下角发牌区纸牌初始化以及表面显示纸牌初始化):所有纸牌的生成及存入数组代码:publicvoidinitCards(){//如果纸牌已被赋值,即将其从框架的面板中移去if(cards[0]!=null){34 for(inti=0;i<104;i++){pane.remove(cards[i]);}}intn=0;//通过难度等级,为n赋值if(this.grade==Spider.EASY){n=1;}elseif(this.grade==Spider.NATURAL){n=2;}else{n=4;}//为card赋值for(inti=1;i<=8;i++){for(intj=1;j<=13;j++){cards[(i-1)*13+j-1]=newPKCard((i%n+1)+"-"+j,this);//当n为1时,i%8的值永远是0,所以i%8+1的值永远是1,因此n为1时,创建的card的名称是1//-1到1-13,全是黑桃。其他等级类似。}}}//纸牌随机分配publicvoidrandomCards(){//将纸牌的顺序打乱,否则牌号的将是从A到K的出现规律PKCardtemp=null;//随机生成牌号for(inti=0;i<52;i++){inta=(int)(Math.random()*104);intb=(int)(Math.random()*104);temp=cards[a];cards[a]=cards[b];cards[b]=temp;}}右下角发牌区纸牌的初始化代码:intx=883;//将坐标定位到右下角发牌区inty=580;for(inti=0;i<6;i++){//初始化右下角发牌区的纸牌,共6叠34 for(intj=0;j<10;j++){intn=i*10+j;pane.add(cards[n]);//将card转向背面cards[n].turnRear();//将card放在固定的位置上cards[n].moveto(newPoint(x,y));//将card的位置及相关信息存入map.put(newPoint(x,y),cards[n]);}x+=10;//共6叠纸牌,最上面一叠纸牌的x坐标为883,再往下,x依次增加10}表面显示纸牌初始化:x=20;//将坐标定位到上方玩牌区的左边起点,也就是第一个背景框的坐标位置y=45;/**初始化表面显示的纸牌*/for(inti=10;i>5;i--){//初始化玩牌区的纸牌,共5行for(intj=0;j<10;j++){//共10列intn=i*10+j;//生成索引值if(n>=104){//如果索引值超出数组下标continue;//退出本次循环,执行外层循环}pane.add(cards[n]);//将索引处的纸牌添加到容器中cards[n].turnRear();//将纸牌转向背面cards[n].moveto(newPoint(x,y));//将纸牌放在固定的位置上map.put(newPoint(x,y),cards[n]);//将纸牌及其坐标值存入map中x+=101;//每两列之间的x坐标相距101个像素}x=20;//x坐标回到起点位置y-=5;//主坐标向上移5个像素}发牌操作功能代码:publicvoiddeal(){//发牌,指从右下角的发牌区发牌this.setNA();//判断10列中是否有空列for(inti=0;i<10;i++){if(this.getLastCardLocation(i)==null){JOptionPane.showMessageDialog(this,"有空位不能发牌!","提示",JOptionPane.WARNING_MESSAGE);34 return;}}intx=20;for(inti=0;i<10;i++){PointlastPoint=this.getLastCardLocation(i);//这张牌应“背面向上”if(c==0){//当还没有发过牌时,玩牌区的纸牌列中,每两张纸牌之间的y坐标值相差5,//指的是背面牌与背面牌之间以及背面牌与第一张正面牌之间的y距离lastPoint.y+=5;}//这张牌应“正面向上”else{//两张正面牌之间的y距离lastPoint.y+=20;}map.remove(cards[c+i].getLocation());cards[c+i].moveto(lastPoint);map.put(newPoint(lastPoint),cards[c+i]);cards[c+i].turnFront();cards[c+i].setCanMove(true);//将组件card移动到容器中指定的顺序索引。this.pane.setComponentZOrder(cards[c+i],1);Pointpoint=newPoint(lastPoint);if(cards[c+i].getValue()==1){intn=cards[c+i].whichColumnAvailable(point);point.y-=240;PKCardcard=(PKCard)this.map.get(point);if(card!=null&&card.isCanMove()){this.haveFinish(n);}}x+=101;}c+=10;}6.3纸牌移动放置操作://用鼠标拖动纸牌publicvoidmouseDragged(MouseEventarg0){34 if(canMove){intx=0;inty=0;Pointp=arg0.getPoint();x=p.x-point.x;y=p.y-point.y;this.moving(x,y);}}/**方法:放置纸牌*/publicvoidsetNextCardLocation(Pointpoint){PKCardcard=main.getNextCard(this);if(card!=null){if(point==null){card.setNextCardLocation(null);main.map.remove(card.getLocation());//先从HashMap中删除cardcard.setLocation(card.initPoint);//为card设置新的坐标main.map.put(card.initPoint,card);//再将card添加到HashMap中}else{point=newPoint(point);point.y+=20;card.setNextCardLocation(point);point.y-=20;main.map.remove(card.getLocation());card.setLocation(point);main.map.put(card.getLocation(),card);card.initPoint=card.getLocation();}}}6.4回收纸牌操作:回收纸牌及过关判定功能主要代码:publicvoidhaveFinish(intcolumn){Pointpoint=this.getLastCardLocation(column);PKCardcard=(PKCard)this.map.get(point);do{this.map.remove(point);34 card.moveto(newPoint(20+finish*10,580));//将组件移动到容器中指定的顺序索引。pane.setComponentZOrder(card,1);//将纸牌新的相关信息存入HashMapthis.map.put(card.getLocation(),card);card.setCanMove(false);point=this.getLastCardLocation(column);if(point==null)card=null;elsecard=this.map.get(point);}while(card!=null&&card.isCanMove());finish++;//如果8付牌全部组合成功,则显示成功的对话框if(finish==8){JOptionPane.showMessageDialog(this,"恭喜你,顺利通过!","成功",JOptionPane.PLAIN_MESSAGE);}if(card!=null){card.turnFront();card.setCanMove(true);}}}6.5帮助菜单功能的实现:主要代码如下:publicclassAboutDialogextendsJDialog{JPaneljMainPanel=newJPanel();JTabbedPanejTabbedPane=newJTabbedPane();privateJPaneljPanel1=newJPanel();privateJTextAreajt1=newJTextArea("将电脑多次分发给你的牌按照相同的花色由大至小排列起来。直到桌面上的牌全都消失。");publicAboutDialog(){setTitle("蜘蛛牌");setSize(300,200);setResizable(false);setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);Containerc=this.getContentPane();34 jt1.setSize(260,200);jt1.setEditable(false);jt1.setLineWrap(true);jt1.setFont(newFont("楷体_GB2312",java.awt.Font.BOLD,13));jt1.setForeground(Color.BLUE);jPanel1.add(jt1);jTabbedPane.setSize(300,200);jTabbedPane.addTab("游戏规则",null,jPanel1,null);jMainPanel.add(jTabbedPane);c.add(jMainPanel);pack();this.setVisible(true);}}6.6退出功能的实现:主要代码如下://“退出”jItemExit.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){main.dispose();System.exit(0);}});7.调试验证运行情况通过对游戏的运行,包括移牌、发牌、开始游戏、选择等级等等。未发现明显异常现象。在这里:我们将测试的结果图片贴在了下面:进入游戏主界面:34 游戏菜单:打开帮助菜单:拖动纸牌:34 发牌以及收牌:完成游戏:34 选择难度(高级):有空位不能发牌:34 8.设计总结通过这次JAVA课程设计实验,让我对java程序设计语言有了更进一步的认识,同时让我对这门程序设计语言有了浓厚的兴趣。在这次java的编程的实践过程中让我进一步体会到了面向对象编程思想在解决实际问题中的应用,让我受益匪浅。同时在这次课程设计实验中,遇到了不少技术难题,例如引入线程以及对鼠标操作事件的控制。但最终通过询问别人和查阅各种资料把这些问题都一一解决了。希望自己今后能在进一步学习计算机编程语言,为以后就业打下坚实的基础。34 参考文献《Java程序设计》,朱庆生,古平等著,清华大学出版社,2011,1《Java编程》,王伟平等著,清华大学出版社,2010,5《Java课程设计案例精编》黄晓东编著,中国水利水电出版社出版《Java程序设计实用教程》张永常主编,电子工业出版社出版34 附录(程序部分代码):importjavax.swing.*;importjava.awt.*;/***“关于”窗口*/publicclassAboutDialogextendsJDialog{JPaneljMainPane=newJPanel();JTabbedPanejTabbedPane=newJTabbedPane();privateJPaneljPanel1=newJPanel();privateJPaneljPanel2=newJPanel();privateJTextAreajt1=newJTextArea("将电脑多次分发给你的牌按照相同的花色由大至小排列起来。直到桌面上的牌全都消失。");privateJTextAreajt2=newJTextArea("该游戏中,纸牌的图片来自于WindowsXP的纸牌游戏,图片权属于原作者所有!");/***构造函数*/publicAboutDialog(){setTitle("蜘蛛牌");setSize(300,200);setResizable(false);setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);Containerc=this.getContentPane();jt1.setSize(260,200);jt2.setSize(260,200);jt1.setEditable(false);jt2.setEditable(false);jt1.setLineWrap(true);jt2.setLineWrap(true);jt1.setFont(newFont("楷体_GB2312",java.awt.Font.BOLD,13));jt1.setForeground(Color.blue);jt2.setFont(newFont("楷体_GB2312",java.awt.Font.BOLD,13));jt2.setForeground(Color.black);jPanel1.add(jt1);jPanel2.add(jt2);jTabbedPane.setSize(300,200);jTabbedPane.addTab("游戏规则",null,jPanel1,null);jTabbedPane.addTab("声明",null,jPanel2,null);jMainPane.add(jTabbedPane);c.add(jMainPane);pack();this.setVisible(true);}34 }importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassPKCardextendsJLabelimplementsMouseListener,MouseMotionListener{//纸牌的位置Pointpoint=null;PointinitPoint=null;intvalue=0;inttype=0;Stringname=null;Containerpane=null;Spidermain=null;booleancanMove=false;booleanisFront=false;PKCardpreviousCard=null;publicvoidmouseClicked(MouseEventarg0){}publicvoidflashCard(PKCardcard){//启动Flash线程newFlash(card).start();//不停的获得下一张牌,直到完成if(main.getNextCard(card)!=null){card.flashCard(main.getNextCard(card));}}classFlashextendsThread{privatePKCardcard=null;publicFlash(PKCardcard){this.card=card;}/***线程的run()方法**为纸牌的正面设置白色图片*/publicvoidrun(){booleanis=false;ImageIconicon=newImageIcon("images/white.gif");for(inti=0;i<4;i++){try{Thread.sleep(200);}catch(InterruptedExceptione){e.printStackTrace();}if(is){34 this.card.turnFront();is=!is;}else{this.card.setIcon(icon);is=!is;}//根据当前外观将card的UI属性重置card.updateUI();}}}/****点击鼠标*/publicvoidmousePressed(MouseEventmp){point=mp.getPoint();main.setNA();this.previousCard=main.getPreviousCard(this);}/****释放鼠标*/publicvoidmouseReleased(MouseEventmr){Pointpoint=((JLabel)mr.getSource()).getLocation();//判断可行列intn=this.whichColumnAvailable(point);if(n==-1||n==this.whichColumnAvailable(this.initPoint)){this.setNextCardLocation(null);main.table.remove(this.getLocation());this.setLocation(this.initPoint);main.table.put(this.initPoint,this);return;}point=main.getLastCardLocation(n);booleanisEmpty=false;PKCardcard=null;if(point==null){point=main.getGroundLabelLocation(n);isEmpty=true;}else{card=(PKCard)main.table.get(point);}if(isEmpty||(this.value+1==card.getCardValue())){point.y+=40;if(isEmpty)point.y-=20;34 this.setNextCardLocation(point);main.table.remove(this.getLocation());point.y-=20;this.setLocation(point);main.table.put(point,this);this.initPoint=point;if(this.previousCard!=null){this.previousCard.turnFront();this.previousCard.setCanMove(true);}this.setCanMove(true);}else{this.setNextCardLocation(null);main.table.remove(this.getLocation());this.setLocation(this.initPoint);main.table.put(this.initPoint,this);return;}point=main.getLastCardLocation(n);card=(PKCard)main.table.get(point);if(card.getCardValue()==1){point.y-=240;card=(PKCard)main.table.get(point);if(card!=null&&card.isCardCanMove()){main.haveFinish(n);}}}/***方法:放置纸牌*/publicvoidsetNextCardLocation(Pointpoint){PKCardcard=main.getNextCard(this);if(card!=null){if(point==null){card.setNextCardLocation(null);main.table.remove(card.getLocation());card.setLocation(card.initPoint);main.table.put(card.initPoint,card);}else{point=newPoint(point);point.y+=20;card.setNextCardLocation(point);point.y-=20;34 main.table.remove(card.getLocation());card.setLocation(point);main.table.put(card.getLocation(),card);card.initPoint=card.getLocation();}}}/****返回值:int**方法:判断可用列*/publicintwhichColumnAvailable(Pointpoint){intx=point.x;inty=point.y;inta=(x-20)/101;intb=(x-20)%101;if(a!=9){if(b>30&&b<=71){a=-1;}elseif(b>71){a++;}}elseif(b>71){a=-1;}if(a!=-1){Pointp=main.getLastCardLocation(a);if(p==null)p=main.getGroundLabelLocation(a);b=y-p.y;if(b<=-96||b>=96){a=-1;}}returna;}publicvoidmouseEntered(MouseEventarg0){}publicvoidmouseExited(MouseEventarg0){}/****用鼠标拖动纸牌*/publicvoidmouseDragged(MouseEventarg0){if(canMove){intx=0;inty=0;Pointp=arg0.getPoint();34 x=p.x-point.x;y=p.y-point.y;this.moving(x,y);}publicvoidmoving(intx,inty){PKCardcard=main.getNextCard(this);Pointp=this.getLocation();//将组件移动到容器中指定的顺序索引。pane.setComponentZOrder(this,1);//在Hashtable中保存新的节点信息main.table.remove(p);p.x+=x;p.y+=y;this.setLocation(p);main.table.put(p,this);if(card!=null)card.moving(x,y);}publicvoidmouseMoved(MouseEventarg0){}publicPKCard(Stringname,Spiderspider){super();this.type=newInteger(name.substring(0,1)).intValue();this.value=newInteger(name.substring(2)).intValue();this.name=name;this.main=spider;this.pane=this.main.getContentPane();this.addMouseListener(this);this.addMouseMotionListener(this);this.setIcon(newImageIcon("images/rear.gif"));this.setSize(71,96);this.setVisible(true);}publicvoidturnFront(){this.setIcon(newImageIcon("images/"+name+".gif"));this.isFront=true;}publicvoidturnRear(){this.setIcon(newImageIcon("images/rear.gif"));this.isFront=false;this.canMove=false;34 }publicvoidmoveto(Pointpoint){this.setLocation(point);this.initPoint=point;}publicvoidsetCanMove(booleancan){this.canMove=can;PKCardcard=main.getPreviousCard(this);if(card!=null&&card.isCardFront()){if(!can){if(!card.isCardCanMove()){return;}else{card.setCanMove(can);}}else{if(this.value+1==card.getCardValue()&&this.type==card.getCardType()){card.setCanMove(can);}else{card.setCanMove(false);}}}}publicbooleanisCardFront(){returnthis.isFront;}publicbooleanisCardCanMove(){returnthis.canMove;}publicintgetCardValue(){returnvalue;}publicintgetCardType(){returntype;}}importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;34 importjava.util.*;publicclassSpiderextendsJFrame{//整型变量,表示难度等级为:简单publicstaticfinalintEASY=1;//整型变量,表示难度等级为:普通publicstaticfinalintNATURAL=2;//整型变量,表示难度等级为:难publicstaticfinalintHARD=3;//设定初始难度等级为简单privateintgrade=Spider.EASY;privateContainerpane=null;//生成纸牌数组privatePKCardcards[]=newPKCard[104];privateJLabelclickLabel=null;privateintc=0;privateintn=0;privateinta=0;privateintfinish=0;Hashtabletable=null;privateJLabelgroundLabel[]=null;publicstaticvoidmain(String[]args){Spiderspider=newSpider();spider.setVisible(true);}publicSpider(){setTitle("蜘蛛牌");setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);//设置框架的大小setSize(1024,742);//生成SpiderMenuBar对象,并放置在框架之上setJMenuBar(newSpiderMenuBar(this));pane=this.getContentPane();//设置背景颜色pane.setBackground(newColor(0,112,26));//将布局管理器设置成为nullpane.setLayout(null);clickLabel=newJLabel();clickLabel.setBounds(883,606,121,96);pane.add(clickLabel);34 clickLabel.addMouseListener(newMouseAdapter(){publicvoidmouseReleased(MouseEventme){if(c<60){Spider.this.deal();}}});this.initCards();this.randomCards();this.setCardsLocation();groundLabel=newJLabel[10];intx=20;for(inti=0;i<10;i++){groundLabel[i]=newJLabel();groundLabel[i].setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));groundLabel[i].setBounds(x,25,71,96);x+=101;this.pane.add(groundLabel[i]);this.setVisible(true);this.deal();this.addKeyListener(newKeyAdapter(){classShowextendsThread{publicvoidrun(){Spider.this.showEnableOperator();}}publicvoidkeyPressed(KeyEvente){if(finish!=8)if(e.getKeyCode()==KeyEvent.VK_D&&c<60){Spider.this.deal();}elseif(e.getKeyCode()==KeyEvent.VK_M){newShow().start();}}});}publicvoidnewGame(){34 this.randomCards();this.setCardsLocation();this.setGroundLabelZOrder();this.deal();}publicintgetC(){returnc;}publicvoidsetGrade(intgrade){this.grade=grade;}publicvoidinitCards(){//如果纸牌已被赋值,即将其从框架的面板中移去if(cards[0]!=null){for(inti=0;i<104;i++){pane.remove(cards[i]);}}intn=0;//通过难度等级,为n赋值if(this.grade==Spider.EASY){n=1;}elseif(this.grade==Spider.NATURAL){n=2;}else{n=4;}//为card赋值for(inti=1;i<=8;i++){for(intj=1;j<=13;j++){cards[(i-1)*13+j-1]=newPKCard((i%n+1)+"-"+j,this);}}//随机纸牌初始化this.randomCards();}publicvoidrandomCards(){PKCardtemp=null;//随机生成牌号34 for(inti=0;i<52;i++){inta=(int)(Math.random()*104);intb=(int)(Math.random()*104);temp=cards[a];cards[a]=cards[b];cards[b]=temp;}}publicvoidsetNA(){a=0;n=0;}publicvoidsetCardsLocation(){table=newHashtable();c=0;finish=0;n=0;a=0;intx=883;inty=580;//初始化待展开的纸牌for(inti=0;i<6;i++){for(intj=0;j<10;j++){intn=i*10+j;pane.add(cards[n]);//将card转向背面cards[n].turnRear();//将card放在固定的位置上cards[n].moveto(newPoint(x,y));//将card的位置及相关信息存入table.put(newPoint(x,y),cards[n]);}x+=10;}x=20;y=45;//初始化表面显示的纸牌for(inti=10;i>5;i--){for(intj=0;j<10;j++){intn=i*10+j;if(n>=104)continue;pane.add(cards[n]);cards[n].turnRear();34 cards[n].moveto(newPoint(x,y));table.put(newPoint(x,y),cards[n]);x+=101;}x=20;y-=5;}}publicvoidshowEnableOperator(){intx=0;out:while(true){Pointpoint=null;PKCardcard=null;do{if(point!=null){n++;}point=this.getLastCardLocation(n);while(point==null){point=this.getLastCardLocation(++n);if(n==10)n=0;x++;if(x==10)breakout;}card=(PKCard)this.table.get(point);}while(!card.isCardCanMove());while(this.getPreviousCard(card)!=null&&this.getPreviousCard(card).isCardCanMove()){card=this.getPreviousCard(card);}if(a==10){a=0;}for(;a<10;a++){if(a!=n){Pointp=null;PKCardc=null;do{if(p!=null){a++;}p=this.getLastCardLocation(a);intz=0;34 while(p==null){p=this.getLastCardLocation(++a);if(a==10)a=0;if(a==n)a++;z++;if(z==10)breakout;}c=(PKCard)this.table.get(p);}while(!c.isCardCanMove());if(c.getCardValue()==card.getCardValue()+1){card.flashCard(card);try{Thread.sleep(800);}catch(InterruptedExceptione){e.printStackTrace();}c.flashCard(c);a++;if(a==10){n++;}breakout;}}}n++;if(n==10){n=0;}x++;if(x==10){breakout;}}}/***返回值:void**方法:游戏运行*/publicvoiddeal(){34 this.setNA();//判断10列中是否空列for(inti=0;i<10;i++){if(this.getLastCardLocation(i)==null){JOptionPane.showMessageDialog(this,"有空位不能发牌!","提示",JOptionPane.WARNING_MESSAGE);return;}}intx=20;for(inti=0;i<10;i++){PointlastPoint=this.getLastCardLocation(i);//这张牌应“背面向上”if(c==0){lastPoint.y+=5;}//这张牌应“正面向上”else{lastPoint.y+=20;}table.remove(cards[c+i].getLocation());cards[c+i].moveto(lastPoint);table.put(newPoint(lastPoint),cards[c+i]);cards[c+i].turnFront();cards[c+i].setCanMove(true);//将组件card移动到容器中指定的顺序索引。this.pane.setComponentZOrder(cards[c+i],1);Pointpoint=newPoint(lastPoint);if(cards[c+i].getCardValue()==1){intn=cards[c+i].whichColumnAvailable(point);point.y-=240;PKCardcard=(PKCard)this.table.get(point);if(card!=null&&card.isCardCanMove()){this.haveFinish(n);}}x+=101;}c+=10;}34 publicPKCardgetPreviousCard(PKCardcard){Pointpoint=newPoint(card.getLocation());point.y-=5;card=(PKCard)table.get(point);if(card!=null){returncard;}point.y-=15;card=(PKCard)table.get(point);returncard;}publicPKCardgetNextCard(PKCardcard){Pointpoint=newPoint(card.getLocation());point.y+=5;card=(PKCard)table.get(point);if(card!=null)returncard;point.y+=15;card=(PKCard)table.get(point);returncard;}publicPointgetLastCardLocation(intcolumn){Pointpoint=newPoint(20+column*101,25);PKCardcard=(PKCard)this.table.get(point);if(card==null)returnnull;while(card!=null){point=card.getLocation();card=this.getNextCard(card);}returnpoint;}publicPointgetGroundLabelLocation(intcolumn){returnnewPoint(groundLabel[column].getLocation());}publicvoidsetGroundLabelZOrder(){for(inti=0;i<10;i++){pane.setComponentZOrder(groundLabel[i],105+i);}}publicvoidhaveFinish(intcolumn){Pointpoint=this.getLastCardLocation(column);PKCardcard=(PKCard)this.table.get(point);do{34 this.table.remove(point);card.moveto(newPoint(20+finish*10,580));//将组件移动到容器中指定的顺序索引。pane.setComponentZOrder(card,1);//将纸牌新的相关信息存入Hashtablethis.table.put(card.getLocation(),card);card.setCanMove(false);point=this.getLastCardLocation(column);if(point==null)card=null;elsecard=(PKCard)this.table.get(point);}while(card!=null&&card.isCardCanMove());finish++;//如果8付牌全部组合成功,则显示成功的对话框if(finish==8){JOptionPane.showMessageDialog(this,"恭喜你,顺利通过!","成功",JOptionPane.PLAIN_MESSAGE);}if(card!=null){card.turnFront();card.setCanMove(true);}}}importjavax.swing.JMenuBar;importjavax.swing.JMenu;importjavax.swing.JMenuItem;importjavax.swing.JRadioButtonMenuItem;importjavax.swing.ButtonGroup;publicclassSpiderMenuBarextendsJMenuBar{//生成spider框架对象Spidermain=null;//生成菜单组JMenujNewGame=newJMenu("游戏");JMenujHelp=newJMenu("帮助");//生成菜单项JMenuItemjItemAbout=newJMenuItem("关于");JMenuItemjItemOpen=newJMenuItem("开局");JMenuItemjItemPlayAgain=newJMenuItem("重新发牌");//生成单选框JRadioButtonMenuItemjRMItemEasy=newJRadioButtonMenuItem("简单:单一花色");JRadioButtonMenuItemjRMItemNormal=newJRadioButtonMenuItem("中级:双花色");34 JRadioButtonMenuItemjRMItemHard=newJRadioButtonMenuItem("高级:四花色");;JMenuItemjItemExit=newJMenuItem("退出");JMenuItemjItemValid=newJMenuItem("显示可行操作");publicSpiderMenuBar(Spiderspider){this.main=spider;jNewGame.add(jItemOpen);jNewGame.add(jItemPlayAgain);jNewGame.add(jItemValid);jNewGame.addSeparator();jNewGame.add(jRMItemEasy);jNewGame.add(jRMItemNormal);jNewGame.add(jRMItemHard);jNewGame.addSeparator();jNewGame.add(jItemExit);ButtonGroupgroup=newButtonGroup();group.add(jRMItemEasy);group.add(jRMItemNormal);group.add(jRMItemHard);jHelp.add(jItemAbout);this.add(jNewGame);this.add(jHelp);//为组件添加事件监听并实现//“开局”jItemOpen.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEvente){main.newGame();}});//“重新发牌”jItemPlayAgain.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEvente){if(main.getC()<60){main.deal();}}});//"显示可行操作"jItemValid.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEvente){newShow().start();}});//“退出”jItemExit.addActionListener(newjava.awt.event.ActionListener(){34 publicvoidactionPerformed(java.awt.event.ActionEvente){main.dispose();System.exit(0);}});//“简单级别”默认已选jRMItemEasy.setSelected(true);//“简单级别”jRMItemEasy.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEvente){main.setGrade(Spider.EASY);main.initCards();main.newGame();}});//“中级”jRMItemNormal.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEvente){main.setGrade(Spider.NATURAL);main.initCards();main.newGame();}});//“高级”jRMItemHard.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEvente){main.setGrade(Spider.HARD);main.initCards();main.newGame();}});jNewGame.addMenuListener(newjavax.swing.event.MenuListener(){publicvoidmenuSelected(javax.swing.event.MenuEvente){if(main.getC()<60){jItemPlayAgain.setEnabled(true);}else{jItemPlayAgain.setEnabled(false);}}publicvoidmenuDeselected(javax.swing.event.MenuEvente){}publicvoidmenuCanceled(javax.swing.event.MenuEvente){}});//“关于”34 jItemAbout.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEvente){newAboutDialog();}});}classShowextendsThread{publicvoidrun(){main.showEnableOperator();}}}34