java蜘蛛纸牌游戏设计 第4页
java蜘蛛纸牌游戏源代码
y -= 5;
}
}
/**
**返回值:void
**方法:显示可移动的操作
*/
public void 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
**方法:游戏运行
*/
public void deal()
{
this.setNA();
//判断10列中是否空列
for (int i = 0; i < 10; i++){
if (this.getLastCardLocation(i) == null){
JOptionPane.showMessageDialog(this, "有空位不能发牌!", "提示",
JOptionPane.WARNING_MESSAGE);
return;
}
}
int x = 20;
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);
}
}
x += 101;
}
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) return null;
while (card != null){
point = card.getLocation();
card = this.getNextCard(card);
}
return point;
}
public Point getGroundLabelLocation(int column){
return new Point(groundLabel[column].getLocation());
}
/*
**返回值:void
**方法:放置groundLable组件
*/
public void setGroundLabelZOrder(){
for (int i = 0; i < 10; i++){
//将组件groundLable移动到容器中指定的顺序索引。顺序(105+i)确定了绘制组件的顺序;具有最高顺序的组件将第一个绘制,而具有最低顺序的组件将最后一个绘制。在组件重叠的地方,具有较低顺序的组件将覆盖具有较高顺序的组件。
pane.setComponentZOrder(groundLabel[i], 105 + i);
}
}
/*
**返回值:void
**方法:判断纸牌的摆放是否完成
*/
public void 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);
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);
}
}
}import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;
public class SpiderMenuBar extends JMenuBar{
//生成spider框架对象
Spider main = null;
//生成菜单组
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);
//为组件添加事件监听并实现
//“开局”
jItemOpen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
main.newGame();
}
});
//“重新发牌”
jItemPlayAgain.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if(main.getC() < 60){
main.deal();
}
}
});
//"显示可行操作"
jItemValid.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
new Show().start();
}
});
//“退出”
jItemExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
main.dispose();
System.exit(0);
}
});
//“简单级别”默认已选
jRMItemEasy.setSelected(true);
//“简单级别”
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) {}
});
//“关于”
jItemAbout.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
new AboutDialog();
}
});
}
/**
**构造线程:显示可以执行的操作
*/
class Show extends Thread{
public void run(){
main.showEnableOperator();
}
}
}