二十一点游戏代码有BUG,求指导。。。。
玩家牌数大于等于2的时候,在显示游戏结果的时会消失一张牌,求指是什么回事,
求。。。。。。
因为字数限制,分两段发,代码如下:
//AboutFrame.java
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
public class AboutFrame extends JFrame{
JLabel jLabel1=new JLabel();
JTextArea ta1=new JTextArea();
JLabel jLabel2=new JLabel();
JTextArea ta2=new JTextArea();
public AboutFrame(){
getContentPane().setLayout(null);
jLabel1.setText("游戏规则");
jLabel1.setBounds(new Rectangle(27,0,97,36));
jLabel2.setText("声明");
jLabel2.setBounds(new Rectangle(26,176,80,27));
ta2.setEditable(false);
ta2.setLineWrap(true);
ta2.setBounds(new Rectangle(27,207,398,63));
ta1.setLineWrap(true);
this.setTitle("关于");
this.getContentPane().add(jLabel1);
this.getContentPane().add(ta1);
this.getContentPane().add(ta2);
this.getContentPane().add(jLabel2);
ta1.setEditable(false);
ta1.setText("电脑先抓牌,玩家后抓牌。计算自己的面值总数,比较面值数,如果面值总数都大于或等于21,则电脑获胜;如果玩家和电脑的面值总数有一个大于21点," +
"另一个不大于21点,则不大于21点的,为赢家;如果玩家和电脑的面值总数都不大于21,则面值总数大的为赢家。");
ta1.setBounds(new Rectangle(25,36,392,130));
ta2.setText("该游戏中,纸牌的图片来自于Windows XP的纸牌游戏,图片权属于原作者所有!");
this.setSize(450,300);
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
//获得当前窗体的宽和高
Dimension frameSize=this.getSize();
//设置窗体居中
if(frameSize.height>screenSize.height)
{
frameSize.height=screenSize.height;
}
if(frameSize.width>screenSize.width){
frameSize.width=screenSize.width;
}
this.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
this.setVisible(true);
}
}
//Card.java
public class Card {
//代表纸牌面值
private int value=0;
//代表纸牌花色
private int type=0;
//构造方法,给面值和花色赋值
public Card(int type,int value)
{
this.value=value;
this.type=type;
}
public int getType(){
return type;
}
public int getValue(){
return value;
}
public void setType(int type){
this.type=type;
}
public void setValue(){
this.value=value;
}
}
//CardManager.java
import java.awt.Container;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class CardManager {
public Card[]cards=new Card[52];
//初始化牌
public void initCards(){
for(int i=1;i<=4;i++){
for(int j=1;j<=13;j++){
cards[(i-1)*13+j-1]=new Card(i,j);
}
}
}
//随机生成牌号
public void randomCards(){
Card temp=null;
//随机生成牌号
for(int i=0;i<52;i++){
int a=(int)(Math.random()*52);
int b=(int)(Math.random()*52);
temp=cards[a];
cards[a]=cards[b];
cards[b]=temp;
}
}
//发牌
public void gameStart(JLabel game[],Container c){
//在容器中删除标签组件
if(game[0]!=null){
for(int i=0;i<52;i++){
c.remove(game[i]);
}
c.repaint();
}
//在容器中防止52个标签组件用于盛放图片
for(int i=0;i<52;i++){
game[i]=new JLabel();
game[i].setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
game[i].setBounds(new Rectangle(100+i*10,59,71,96));
c.add(game[i]);
}
//设置标签组件的图片为rear.gif,即牌的背面
for(int i=0;i<52;i++){
game[i].setIcon(new ImageIcon("images/cover.jpg"));
}
}
}