安阳工学院计算机学院 JAVA课程设计报告
蜘蛛纸牌游戏
专业班级: 09级网络工程(2)班
学生姓名: 李瑞琳
学生学号: 200902010016
小组成员: 司慧晓郑亚楠司小婷杨达允
指导教师姓名: 宋 强
目 录
1.选题目的与系统概述…………………………………………2
2.系统可行性分析与相关技术背景分析………………………3
2.1系统可行性分析…………………………………………………3
2.2相关技术背景分析………………………………………………3
3.系统设计与关键技术…………………………………………4
3.1系统设计…………………………………………………………4
3.2关键技术…………………………………………………………5
4.系统测试与操作实施方法……………………………………9
4.1进入游戏运行界面………………………………………………9
4.2 游戏的具体的操作过程…………………………………………9
4.3进入“帮助”菜单………………………………………………10
4.4游戏中的异常处理情况…………………………………………11
5.系统设计总结与回顾………………………………………12
6.附录……………………………………………………………13
6.1 AboutDialog.java……………………………………………13
6.2 PKCard.java……………………………………………………14
6.3 Spider.java………………………………………………………20
6.4 SpiderMenuBar.java……………………………………………29
1.选题目的与系统概述
随着经济全球一体化的逐步加剧,人们的生活节奏越来越快。蜘蛛纸牌游戏的一种,旨在让工作学习之后疲惫的玩家有一个轻愉快的放松方式。与普通的纸牌相比,降低了一点难度。游戏简单级别由8组A-K的清一色黑桃牌面组成,取消了红黑相间的游戏规则,更突然出强化了玩家在安排事物方面的能力。
蜘蛛纸牌游戏实现了与Windows系列操作系统中自带的蜘蛛纸牌游戏类似的功能。该游戏程序中采用Windows系列中游戏的图片,运用Java语言设计了线程、鼠标事件、纸牌的装载、图形界面的构造、组件监听接口的实现、生成蜘蛛纸牌游戏的框架、实现游戏中的方法,是一种休闲益智游戏。
2.系统可行性分析与相关技术背景分析
2.1系统可行性分析
蜘蛛纸牌游戏规则是:
(1) 将电脑分发给你的牌按照相同的花色又打至小排列起来,直到桌面上的牌全都消失。
(2) 可执行的牌必须为明牌
(3) 单牌移动规则
(4) 多拍移动规则
游戏分三个等级:(1)简答:单一花色;(2)中级:双花色;(3)四花色。
2.2相关技术背景分析
2.2.1蜘蛛纸牌游戏是基于Eclipse平台的Java程序。Eclipse的背景如下:
Eclipse最初是IBM公司的一个软件产品。20##年11月7日发布Eclipse 1.0版本,以后几乎每年都更新一次版本,到20##年6月22日,发布最新版本Eclipse 3.7(INDIGO)。尽管Eclipse是使用Java语言开发的,但它的用途并不仅限于Java语言。例如,支持诸如C/C++、COBOL和Eiffel等编程语言的插件已经可用。Eclipse平台的主要任务是为开发者提供能够使用各种工具无缝集成的机制和需要遵守的规则。Eclipse的特点是:(1)基于Java、开发源码的可扩展平台;(2)一个免费得Java IDE(Java 集成开发环境)。
2.2.2本课程设计是在最新版本Eclipse 3.7(INDIGO)下运行,进入界面如下:
3.系统设计与关键技术
3.1系统设计
3.1.1蜘蛛游戏可以分为四个模块,它们之间的关系如下:
(1)Spider.Java游戏开始的主函数,调用中及实现游戏中的方法,即调用 SpiderMenuBar.java
(2)SpiderMenuBar调用游戏框架和游戏菜单,即调用PKCard.java和AboutDialog.Java
(3) PKCard.java纸牌的调用和移动位置及纸牌属性,即调用Spider.Java
3.1.2蜘蛛游戏的流程图如图3-1所示。
图3-1
3.1.3蜘蛛游戏可以分为四个模块各项功能如下:
(1)AboutDialog.java 其作用是生成游戏的“帮助”下拉菜单
(2)PKCard.java 其作用是定义纸牌属性,包括名称、位置等相关信息。并管理所有可执行操作,如启动Flash线程、点击鼠标、释放鼠标、放置纸牌、判断可用列、用鼠标拖动纸牌、移动(x,y)个位置、令纸牌显示正面、令纸牌显示背面、将纸牌移动到点point、判断纸牌是否移动、判断card是否是正面等 。
(3)Spider.java其作用是设置游戏等级、框架属性、纸牌初始化、异常及成功处理等
(4)SpiderMenuBar.java 其作用是添加“游戏”下拉列表所有菜单选项,实现各组件的事件侦听 。主要包括三个模块:图形用户界面的构建、组件监听接口的实现、显示可执行操作的线程。
3.2关键技术
3.2.1 AboutDialog.java 中的“关于”窗口
private JTextArea jt1 = new JTextArea("将电脑多次分发给你的牌按照相同的花色由大至小排列起来。直到桌面上的牌全都消失。"); //用来显示“游戏规则”对应的文本框
private JTextArea jt2 = new JTextArea("该游戏中,纸牌的图片来自于Windows XP的纸牌游戏,图片权属于原作者所有!"); //用来显示“声明”对应的文本框
3.2.2 PKCard.java当有空位的时候,启动Flash线程,继续进行发牌。
public void flashCard(PKCard card){
new Flash(card).start(); //不停的获得下一张牌,直到完成
if(main.getNextCard(card) != null){
card.flashCard(main.getNextCard(card)); }}
class Flash extends Thread{
private PKCard card = null;
public Flash(PKCard card){
this.card = card;
}
3.2.3 PKCard.java中点击“游戏”->“显示可行操作”后,可以连续的纸牌会闪烁一下,即一张是图片本身一张是白色图片。这里有线程的run()方法来实现。
public void run(){
boolean is = false;
ImageIcon icon = new ImageIcon("images/white.gif");
for (int i = 0; i < 4; i++){
try{
Thread.sleep(200);
}
catch (InterruptedException e){
e.printStackTrace();
}
if (is){ this.card.turnFront();
is = !is; }
else{ this.card.setIcon(icon);
is = !is;
}// 根据当前外观将card的UI属性重置
card.updateUI();}}}
3.2.4 PKCard.java放置纸牌
public void setNextCardLocation(Point point){
PKCard card = 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 = new Point(point);
point.y += 20;
card.setNextCardLocation(point);
point.y -= 20;
main.table.remove(card.getLocation());
card.setLocation(point);
main.table.put(card.getLocation(), card);
card.initPoint = card.getLocation();}}}
3.2.5 PKCard.java 判断可用列
public int whichColumnAvailable(Point point){
int x = point.x;
int y = point.y;
int a = (x - 20) / 101;
int b = (x - 20) % 101;
if (a != 9){
if (b > 30 && b <= 71){
a = -1; }
else if (b > 71){
a++;}}
else if (b > 71){
a = -1; }
if (a != -1){
Point p = main.getLastCardLocation(a);
if (p == null) p = main.getGroundLabelLocation(a);
b = y - p.y;
if (b <= -96 || b >= 96){
a = -1; }}
return a; }
3.2.6 Spider.java中对鼠标事件监听程序进行初始布局
clickLabel.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent me){
if (c < 60){
Spider.this.deal();
}} });
3.2.7SpiderMenuBar.java“游戏”下拉菜单中游戏级别设置
jRMItemEasy.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
main.setGrade(Spider.EASY);
main.initCards();
main.newGame();
}}); //“简单级别”默认设置
jRMItemNormal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
main.setGrade(Spider.NATURAL);
main.initCards();
main.newGame();
} });//“中级”
jRMItemHard.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
main.setGrade(Spider.HARD);
main.initCards();
main.newGame();
}
});//“高级”
jNewGame.addMenuListener(new javax.swing.event.MenuListener() {
public void menuSelected(javax.swing.event.MenuEvent e) {
if(main.getC() < 60){
jItemPlayAgain.setEnabled(true);
}
else{
jItemPlayAgain.setEnabled(false); } }
public void menuDeselected(javax.swing.event.MenuEvent e) {}
public void menuCanceled(javax.swing.event.MenuEvent e) {} });
3.2.8SpiderMenuBar.java "中显示可行操作"功能控制
jItemValid.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
new Show().start();
}});
3.2.9为组件添加事件监听addActionListener并实现
jItemOpen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
main.newGame();
}
});//“开局”
3.2.10“重新发牌”功能控制
jItemPlayAgain.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if(main.getC() < 60){
main.deal();
}} });
4.系统测试与操作实施方法
4.1进入游戏运行界面。如图4-1所示。
图4-1“游戏”下拉菜单列表
4.2 游戏的具体的操作过程
(1)开始时,为“简单”级别,花色为黑桃,如图4-2所示。
(2)将纸牌从大到小将纸牌排列,当从K到A排好序后,游戏将所有牌归并到界面的左下方,如图4-2到图4-5所示。
(3)当第八次归并完毕后,游戏自动弹出“成功”对话框,如图4-6所示。
(4)设置为“中级”的开局后的界面,如图4-7 所示。
(5)设置为“高级”的开局后的界面,如图4-8所示。
图4-2进入“开局”初始化界面 图4-3 将纸牌从大到小排好后,归并到
左下角
图4-4 第二次归并后 图4-5第三次归并后
图4-6 第八次归并完毕后,游戏自动弹出“成功”对话框
图4-7 设置为“中级”的开局后界面 图4-8设置为“高级”的开局后界面
4.3进入“帮助”菜单
点击“帮助”下拉菜单的“关于”会弹出“蜘蛛牌”对话框,如图4-9所示。单击“游戏规则”如图4-10所示。单击“声明”图4-11所示。
图4-9“帮助”下拉菜单“关于” 图4-10“游戏规则”下的编辑框
图4-11“声明”下的编辑框
4.4游戏中的异常处理情况
游戏过程中,如果移动纸牌时,中间有空位时,单击发牌,会弹出“提示对话框”。如图4-12所示。
图4-12纸牌的异常处理
5.系统设计总结与回顾
蜘蛛纸牌游戏实现了与Windows系列操作系统中自带的蜘蛛纸牌游戏类似的功能。该游戏程序中采用Windows系列中游戏的图片,运用Java语言设计了线程、鼠标事件、纸牌的装载、图形界面的构造、组件监听接口的实现、生成蜘蛛纸牌游戏的框架、实现游戏中的方法,是一种休闲益智游戏。
通过本次课程设计对蜘蛛纸牌游戏的深入研究和分析,对于Eclipse 3.7有了深入的学习和了解,并对其应用有了一定的掌握。对Java语言在实际中的应用有了切身的体会,并将所学的知识在实践中得到应用,同时也巩固了Java中所学的理论知识,对于不了解的知识,又有了新的学习。同学们对于不懂的内容同学查阅相关资料以及老师们的帮助,还有同学间的相互讨论,增加了团队合作的能力。在分析源码的过程中,有些不熟悉的代码,参考了相关资料,以及老师和同学们的帮助,使得问题得以解决,在此向向帮助过我们的老师和同学表示感谢!
6.附 录
源代码:
6.1 AboutDialog.java
import javax.swing.*;
import java.awt.*;
/*“关于”窗口*/
publicclassAboutDialogextends JDialog
{
privatestaticfinallongserialVersionUID = 1L;//Add default serial version ID
JPanel jMainPane = new JPanel();
JTabbedPane jTabbedPane = new JTabbedPane();
private JPanel jPanel1 = new JPanel();
private JPanel jPanel2 = new JPanel();
private JTextArea jt1 = new JTextArea("将电脑多次分发给你的牌按照相同的花色由大至小排列起来。直到桌面上的牌全都消失。"); //用来显示“游戏规则”对应的文本框
private JTextArea jt2 = new JTextArea("该游戏中,纸牌的图片来自于Windows XP的纸牌游戏,图片权属于原作者所有!"); //用来显示“声明”对应的文本框
/*构造函数*/
public AboutDialog()
{
setTitle("蜘蛛牌");
setSize(300,200);
setResizable(false);//设置是否可调整大小,初始值设为false
setDefaultCloseOperation (WindowConstants.DISPOSE_ON_CLOSE); //setDefaultCloseOperation()是用来设定窗口被关闭时候(比如点击了右上角的"x")的行为的。
Container c = this.getContentPane();
jt1.setSize(300,300);//设置“游戏规则”下文本框大小
jt2.setSize(300,300);//设置“声明”下文本框的大小
jt1.setEditable(false);//设置文本框是否可以编辑,这里设置为false
jt2.setEditable(false);
jt1.setLineWrap(true);
jt2.setLineWrap(true);
jt1.setFont(new Font("楷体_GB2312", java.awt.Font.BOLD, 20));//中文字体:楷体_GB2312,字形:黑体,字号:13
jt1.setForeground(Color.blue);//设置“游戏规则”下文本的字体颜色,可以设置为black(黑)、blue(蓝)、 cyan(蓝绿)、darkGray(深灰)、gray(灰)、green(绿)、lightGray(浅灰)、orange(橘黄)、pink(粉红)、red(红)、white(白)、yellow(黄)等java.awt.Color中13中标准颜色之一
jt2.setFont(new Font("楷体_GB2312", java.awt.Font.BOLD, 20));
jt2.setForeground(Color.red);
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);
}}
6.2 PKCard.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
publicclassPKCard extends JLabel implementsMouseListener,
MouseMotionListener{
privatestaticfinallongserialVersionUID = 1L;//Add default serial version ID
//纸牌的位置
Point point = null;
Point initPoint = null;
intvalue = 0;
inttype = 0;
String name = null;
Container pane = null;
Spider main = null;
booleancanMove = false;
booleanisFront = false;
PKCard previousCard = null;
publicvoid mouseClicked(MouseEvent arg0){
}
publicvoid flashCard(PKCard card){ //启动Flash线程
new Flash(card).start(); //不停的获得下一张牌,直到完成
if(main.getNextCard(card) != null){
card.flashCard(main.getNextCard(card));
}
}
class Flash extends Thread{
private PKCard card = null;
public Flash(PKCard card){
this.card = card;
}
/*线程的run()方法--为纸牌的正面设置白色图片*/
publicvoid run(){
boolean is = false;
ImageIcon icon = new ImageIcon("images/white.gif");
for (int i = 0; i < 4; i++){
try{
Thread.sleep(200);
}
catch (InterruptedException e){
e.printStackTrace();
}
if (is){
this.card.turnFront();
is = !is;
}
else{
this.card.setIcon(icon);
is = !is;
}// 根据当前外观将card的UI属性重置
card.updateUI();
}}}
/*点击鼠标*/
public void mousePressed(MouseEvent mp){
point = mp.getPoint();
main.setNA();
this.previousCard = main.getPreviousCard(this);
}
/*释放鼠标*/
publicvoid mouseReleased(MouseEvent mr){
Point point = ((JLabel) mr.getSource()).getLocation();
//判断可行列
int n = 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);
boolean isEmpty = false;
PKCard card = 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;
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);
}
}
}
/*
**方法:放置纸牌
*/
publicvoid setNextCardLocation(Point point){
PKCard card = 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 = new Point(point);
point.y += 20;
card.setNextCardLocation(point);
point.y -= 20;
main.table.remove(card.getLocation());
card.setLocation(point);
main.table.put(card.getLocation(), card);
card.initPoint = card.getLocation();
}
}
}
/**
**返回值:int
**方法:判断可用列
*/
publicint whichColumnAvailable(Point point){
int x = point.x;
int y = point.y;
int a = (x - 20) / 101;
int b = (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){
Point p = main.getLastCardLocation(a);
if (p == null) p = main.getGroundLabelLocation(a);
b = y - p.y;
if (b <= -96 || b >= 96){
a = -1;
}
}
return a;
}
publicvoid mouseEntered(MouseEvent arg0){
}
publicvoid mouseExited(MouseEvent arg0){
}
/**
**用鼠标拖动纸牌
*/
publicvoid mouseDragged(MouseEvent arg0){
if (canMove){
int x = 0;
int y = 0;
Point p = arg0.getPoint();
x = p.x - point.x;
y = p.y - point.y;
this.moving(x, y);
}
}
/**
**返回值:void
**方法:移动(x,y)个位置
*/
publicvoid moving(int x, int y){
PKCard card = main.getNextCard(this);
Point p = 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);
}
publicvoid mouseMoved(MouseEvent arg0){
}
/**
**构造方法
*/
public PKCard(String name, Spider spider){
super();
this.type = new Integer(name.substring(0, 1)).intValue();
this.value = new Integer(name.substring(2)).intValue();
this.name = name;
this.main = spider;
this.pane = this.main.getContentPane();
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.setIcon(new ImageIcon("images/rear.gif"));//背面牌
this.setSize(71, 96);//纸牌的大小
this.setVisible(true);
}
/**
**返回值:void
**方法:令纸牌显示正面(纸牌将全部归位到游戏界面的左下角)
*/
publicvoid turnFront(){
this.setIcon(new ImageIcon("images/" + name + ".gif"));
this.isFront = true;
}
/**
**返回值:void
**方法:令纸牌显示背面
*/
publicvoid turnRear(){
this.setIcon(new ImageIcon("images/rear.gif"));
this.isFront = false;
this.canMove = false;
}
/**
**返回值:void
**方法:将纸牌移动到点point
*/
publicvoid moveto(Point point){
this.setLocation(point);
this.initPoint = point;
}
/**
**返回值:void
**方法:判断牌是否能移动
*/
publicvoid setCanMove(boolean can){
this.canMove = can;
PKCard card = 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);
}}}}
/*返回值:boolean--方法:判断card是否是正面*/
publicboolean isCardFront(){
returnthis.isFront;
}
/*返回值:boolean--方法:返回是否能够移动*/
publicboolean isCardCanMove(){
returnthis.canMove;
}
/*返回值:int--方法:获得card的内容值*/
publicint getCardValue(){
returnvalue;
}
/*返回值:int--方法:获得card的类型*/
publicint getCardType(){
returntype;
}}
6.3 Spider.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
publicclassSpiderextends JFrame{
privatestaticfinallongserialVersionUID = 1L;
//整型变量,表示难度等级为:简单
publicstaticfinalintEASY = 1;
//整型变量,表示难度等级为:中级
publicstaticfinalintNATURAL = 2;
//整型变量,表示难度等级为:高级
publicstaticfinalintHARD = 3;
//设定初始难度等级为简单
privateintgrade = Spider.EASY;
private Container pane = null;
//生成纸牌数组
private PKCard cards[] = new PKCard[104];
private JLabel clickLabel = null;
privateintc = 0;
privateintn = 0;
privateinta = 0;
privateintfinish = 0;
Hashtable<Point, PKCard> table = null;//Hashtable
private JLabel groundLabel[] = null;
publicstaticvoid main(String[] args){
Spider spider = new Spider();
spider.setVisible(true);
}
/**
**构造方法
*/
public Spider(){
setTitle("蜘蛛牌");
setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
//设置框架的大小
setSize(1024, 742);
//生成SpiderMenuBar对象,并放置在框架之上
setJMenuBar(new SpiderMenuBar(this));
pane = this.getContentPane();
//设置背景颜色
pane.setBackground(new Color(0, 112, 26));//0~255之间
//将布局管理器设置成为null
pane.setLayout(null);
clickLabel = new JLabel();
clickLabel.setBounds(883, 606, 121, 96);
pane.add(clickLabel);
clickLabel.addMouseListener(newMouseAdapter(){
publicvoid mouseReleased(MouseEvent me){
if (c < 60){
Spider.this.deal();
}
}
});//对鼠标事件监听程序进行初始布局
this.initCards();
this.randomCards();
this.setCardsLocation();
groundLabel = new JLabel[10];
int x = 20;//因为纸牌按顺序进行摆放,而每一次放的位置都需要进行累加所以在不同位置的纸牌摆放可以引入int x=20;增加一个累加的位置数值变量
for (int i = 0; i < 10; i++)
{
groundLabel[i] = new JLabel();
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(new KeyAdapter(){
class Show extends Thread{
publicvoid run(){
Spider.this.showEnableOperator();
}
}
publicvoid keyPressed(KeyEvent e){
if (finish != 8) if (e.getKeyCode() == KeyEvent.VK_D && c < 60){
Spider.this.deal();
}
elseif (e.getKeyCode() == KeyEvent.VK_M){
new Show().start();
}
}
});
}
/**
**开始新游戏
*/
publicvoid newGame(){
this.randomCards();
this.setCardsLocation();
this.setGroundLabelZOrder();
this.deal();
}
/**
**返回值:int
**返回牌的数量
*/
publicint getC(){
returnc;
}
/**
**设置等级
*/
publicvoid setGrade(int grade){
this.grade = grade;
}
/**
**纸牌初始化
*/
publicvoid initCards(){
//如果纸牌已被赋值,即将其从框架的面板中移去
if (cards[0] != null){
for (int i = 0; i < 104; i++){
pane.remove(cards[i]);
}
}
int n = 0;
//通过难度等级,为n赋值
if (this.grade == Spider.EASY){
n = 1;
}
elseif (this.grade == Spider.NATURAL){
n = 2;
}
else{
n = 4;
}
//为card赋值
for (int i = 1; i <= 8; i++){
for (int j = 1; j <= 13; j++){
cards[(i - 1) * 13 + j - 1] = new PKCard((i % n + 1) + "-" + j,
this);
}
}
//随机纸牌初始化
this.randomCards();
}
/**
**纸牌随机分配
*/
publicvoid randomCards(){
PKCard temp = null;
//随机生成牌号
for (int i = 0; i < 52; i++){
int a = (int) (Math.random() * 104);
int b = (int) (Math.random() * 104);
temp = cards[a];
cards[a] = cards[b];
cards[b] = temp;
}
}
/**
**设置还原
*/
publicvoid setNA(){
a = 0;
n = 0;
}
/**
**设置纸牌的位置
*/
publicvoid setCardsLocation(){
table = new Hashtable<Point, PKCard>();
c = 0;
finish = 0;
n = 0;
a = 0;
int x = 883;
int y = 580;
//初始化待展开的纸牌
for (int i = 0; i < 6; i++){
for (int j = 0; j < 10; j++){
int n = i * 10 + j;
pane.add(cards[n]);
//将card转向背面,因为纸牌未点击的时候,纸牌是背面显示,要对其进行初始化
cards[n].turnRear();
//将card放在固定的位置上
cards[n].moveto(new Point(x, y));
//将card的位置及相关信息存入
table.put(new Point(x, y), cards[n]);
}
x += 10;
}
x = 20;
y = 45;
//初始化表面显示的纸牌,因为当点击该纸牌之后,程序会继续分发纸牌,这时程序要对显示的纸牌进行初始化设置
for (int i = 10; i > 5; i--){
for (int j = 0; j < 10; j++){
int n = i * 10 + j;
if (n >= 104) continue;
pane.add(cards[n]);
cards[n].turnRear();
cards[n].moveto(new Point(x, y));
table.put(new Point(x, y), cards[n]);
x += 101;
}
x = 20;
y -= 5;
}
}
/**
**返回值:void
**方法:显示可移动的操作(点击可行性操作后,可以连续的纸牌会闪一下,即一下是图片本身,一张是一张白色图片。确定有可行性操作之后SpiderMenuBar对象会调用PKCard对象中的run()来实现提示)
*/
publicvoid showEnableOperator(){
int x = 0;
out: while (true){
Point point = null;
PKCard card = 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) break out;
}
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){
Point p = null;
PKCard c = null;
do{
if (p != null){
a++;
}
p = this.getLastCardLocation(a);
int z = 0;
while (p == null){
p = this.getLastCardLocation(++a);
if (a == 10) a = 0;
if (a == n) a++;
z++;
if (z == 10) break out; }
c = (PKCard) this.table.get(p);
}
while (!c.isCardCanMove());
if (c.getCardValue() == card.getCardValue() + 1){
card.flashCard(card);
try{ Thread.sleep(800); }
catch (InterruptedException e){ e.printStackTrace();}
c.flashCard(c);
a++;
if (a == 10){
n++;
}
break out;
}
}
}
n++;
if (n == 10){
n = 0;
}
x++;
if (x == 10){
break out;
}
}
}
/*
**返回值:void
**方法:游戏运行
*/
publicvoid deal()
{
this.setNA();
//判断10列中是否空列
for (int i = 0; i < 10; i++){
if (this.getLastCardLocation(i) == null){
JOptionPane.showMessageDialog(this, "有空位不能发牌!", "提示",
JOptionPane.WARNING_MESSAGE);
return;
}
}
for (int i = 0; i < 10; i++){
Point lastPoint = 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(new Point(lastPoint), cards[c + i]);
cards[c + i].turnFront();
cards[c + i].setCanMove(true);
//将组件card移动到容器中指定的顺序索引。
this.pane.setComponentZOrder(cards[c + i], 1);
Point point = new Point(lastPoint);
if (cards[c + i].getCardValue() == 1){
int n = cards[c + i].whichColumnAvailable(point);
point.y -= 240;
PKCard card = (PKCard) this.table.get(point);
if (card != null && card.isCardCanMove()){
this.haveFinish(n);
}
}
}
c += 10;
}
/*
**返回值:PKCard对象
**方法:获得card上面的那张牌
*/
public PKCard getPreviousCard(PKCard card){
Point point = new Point(card.getLocation());
point.y -= 5;
card = (PKCard) table.get(point);
if (card != null){
return card;
}
point.y -= 15;
card = (PKCard) table.get(point);
return card;
}
/**
**返回值:PKCard对象
**方法:取得card下面的一张牌
*/
public PKCard getNextCard(PKCard card){
Point point = new Point(card.getLocation());
point.y += 5;
card = (PKCard) table.get(point);
if (card != null)
return card;
point.y += 15;
card = (PKCard) table.get(point);
return card;
}
/**
**返回值:Point对象
**方法:取得第column列最后一张牌的位置(如果最后一张牌的位置是在该列摆放纸牌的最后一个位置则可以继续发牌)
*/
public Point getLastCardLocation(int column){
Point point = new Point(20 + column * 101, 25);
PKCard card = (PKCard) this.table.get(point);
if (card == null) returnnull;
while (card != null){//如果最后一张牌的位置在发牌的列的位置上则可以发牌,即card!=null
point = card.getLocation();
card = this.getNextCard(card);
}
return point;
}
public Point getGroundLabelLocation(int column){
returnnew Point(groundLabel[column].getLocation());
}
/*
**返回值:void
**方法:放置groundLable组件
*/
publicvoid setGroundLabelZOrder(){
for (int i = 0; i < 10; i++){
//将组件groundLable移动到容器中指定的顺序索引。顺序(105+i)确定了绘制组件的顺序;具有最高顺序的组件将第一个绘制,而具有最低顺序的组件将最后一个绘制。在组件重叠的地方,具有较低顺序的组件将覆盖具有较高顺序的组件。
pane.setComponentZOrder(groundLabel[i], 105 + i);
}
}
/*
**返回值:void
**方法:判断纸牌的摆放是否完成
*/
publicvoid haveFinish(int column){
Point point = this.getLastCardLocation(column);
PKCard card = (PKCard) this.table.get(point);
do{
this.table.remove(point);
card.moveto(new Point(20 + finish * 10, 580));
//将组件移动到容器中指定的顺序索引。
pane.setComponentZOrder(card, 1);
//将纸牌新的相关信息存入Hashtable
this.table.put(card.getLocation(), card);
card.setCanMove(false);
point = this.getLastCardLocation(column);
if (point == null)
card = null;
else
card = (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);
}
}}
6.4 SpiderMenuBar.java
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;
publicclass SpiderMenuBar extends JMenuBar{
privatestaticfinallongserialVersionUID = 1L
Spider main = null;//生成spider框架对象
JMenu jNewGame = new JMenu("游戏");//生成菜单组
JMenu jHelp = new JMenu("帮助");//生成菜单项 JMenuItem jItemAbout = new JMenuItem("关于");
JMenuItem jItemOpen = new JMenuItem("开局");
JMenuItem jItemPlayAgain = new JMenuItem("重新发牌");
//生成单选框
JRadioButtonMenuItem jRMItemEasy = new JRadioButtonMenuItem("简单:单一花色");
JRadioButtonMenuItem jRMItemNormal = new JRadioButtonMenuItem("中级:双花色");
JRadioButtonMenuItem jRMItemHard = new JRadioButtonMenuItem("高级:四花色");;
JMenuItem jItemExit = new JMenuItem("退出");
JMenuItem jItemValid = new JMenuItem("显示可行操作");
/**
**构造方法,生成JMenuBar的图形界面
*/
public SpiderMenuBar(Spider spider){
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);
ButtonGroup group = new ButtonGroup();
group.add(jRMItemEasy);
group.add(jRMItemNormal);
group.add(jRMItemHard);
jHelp.add(jItemAbout);
this.add(jNewGame);
this.add(jHelp);
//为组件添加事件监听addActionListener并实现
//“开局”
jItemOpen.addActionListener(new java.awt.event.ActionListener() {
publicvoid actionPerformed(java.awt.event.ActionEvent e) {
main.newGame();
}
});
//“重新发牌”
jItemPlayAgain.addActionListener(new java.awt.event.ActionListener() {
publicvoid actionPerformed(java.awt.event.ActionEvent e) {
if(main.getC() < 60){
main.deal();
}
}
});
//"显示可行操作"
jItemValid.addActionListener(new java.awt.event.ActionListener() {
publicvoid actionPerformed(java.awt.event.ActionEvent e) {
new Show().start();
}
});
//“退出”
jItemExit.addActionListener(new java.awt.event.ActionListener() {
publicvoid actionPerformed(java.awt.event.ActionEvent e) {
main.dispose();
System.exit(0);
}
});
//“简单级别”默认已选
jRMItemEasy.setSelected(true);
//“简单级别”
jRMItemEasy.addActionListener(new java.awt.event.ActionListener() {
publicvoid actionPerformed(java.awt.event.ActionEvent e) {
main.setGrade(Spider.EASY);
main.initCards();
main.newGame();
}
});
//“中级”
jRMItemNormal.addActionListener(new java.awt.event.ActionListener() {
publicvoid actionPerformed(java.awt.event.ActionEvent e) {
main.setGrade(Spider.NATURAL);
main.initCards();
main.newGame();
}
});
//“高级”
jRMItemHard.addActionListener(new java.awt.event.ActionListener() {
publicvoid actionPerformed(java.awt.event.ActionEvent e) {
main.setGrade(Spider.HARD);
main.initCards();
main.newGame();
}
});
jNewGame.addMenuListener(new javax.swing.event.MenuListener() {
publicvoid menuSelected(javax.swing.event.MenuEvent e) {
if(main.getC() < 60){
jItemPlayAgain.setEnabled(true);
}
else{
jItemPlayAgain.setEnabled(false);
}
}
publicvoid menuDeselected(javax.swing.event.MenuEvent e) { }
publicvoid menuCanceled(javax.swing.event.MenuEvent e) {}
});
//“关于”下拉列表
jItemAbout.addActionListener(new java.awt.event.ActionListener() {
publicvoid actionPerformed(java.awt.event.ActionEvent e) {
new AboutDialog();
}
});
}
/**
**构造线程:显示可以执行的操作
*/
class Show extends Thread{
publicvoid run(){
main.showEnableOperator();
}
}
}