21点Java代码测试

21点Java代码测试,java,computer-science,blackjack,Java,Computer Science,Blackjack,我在测试blackjack java代码时遇到问题,以下是代码: package view; /* ////In this applet, the user plays a game of Blackjack. The ////computer acts as the dealer. The user plays by clicking ////"Hit!" and "Stand!" buttons. ////The programming of this applet assumes

我在测试blackjack java代码时遇到问题,以下是代码:

package view;

/*
////In this applet, the user plays a game of Blackjack.  The
////computer acts as the dealer.  The user plays by clicking
////"Hit!" and "Stand!" buttons.

////The programming of this applet assumes that the applet is
////set up to be about 466 pixels wide and about 346 pixels high.
////That width is just big enough to show 2 rows of 5 cards.
////The height is probably a little bigger than necessary,
////to allow for variations in the size of buttons from one platform
////to another.

*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import model.Card;
import model.Deck;
import model.Hand;

public class GUI21 extends JApplet {

public void init() {

  // The init() method creates components and lays out the applet.
  // A BlackjackCanvas occupies the CENTER position of the layout.
  // On the bottom is a panel that holds three buttons.  The
  // BlackjackCanvas object listens for events from the buttons
  // and does all the real work of the program.

setBackground( new Color(130,50,40) );

BlackjackCanvas board = new BlackjackCanvas();
getContentPane().add(board, BorderLayout.CENTER);

JPanel buttonPanel = new JPanel();
buttonPanel.setBackground( new Color(220,200,180) );
getContentPane().add(buttonPanel, BorderLayout.SOUTH);

JButton hit = new JButton( "Hit!" );
hit.addActionListener(board);
buttonPanel.add(hit);

JButton stand = new JButton( "Stand!" );
stand.addActionListener(board);
buttonPanel.add(stand);

JButton newGame = new JButton( "New Game" );
newGame.addActionListener(board);
buttonPanel.add(newGame);

}  // end init()

public Insets getInsets() {
  // Specify how much space to leave between the edges of
  // the applet and the components it contains.  The background
  // color shows through in this border.
return new Insets(3,3,3,3);
}


// --- The remainder of this class consists of a nested class ---


class BlackjackCanvas extends JPanel implements ActionListener {

  // A nested class that displays the card game and does all the work
  // of keeping track of the state and responding to user events.

Deck deck;         // A deck of cards to be used in the game.

Hand dealerHand;   // Hand containing the dealer's cards.
Hand playerHand;   // Hand containing the user's cards.

String message;  // A message drawn on the canvas, which changes
                //    to reflect the state of the game.

boolean gameInProgress;  // Set to true when a game begins and to false
                        //   when the game ends.

Font bigFont;      // Font that will be used to display the message.
Font smallFont;    // Font that will be used to draw the cards.


BlackjackCanvas() {
     // Constructor.  Creates fonts and starts the first game.
  setBackground( new Color(0,120,0) );
  smallFont = new Font("SansSerif", Font.PLAIN, 12);
  bigFont = new Font("Serif", Font.BOLD, 14);
  doNewGame();
}


public void actionPerformed(ActionEvent evt) {
      // Respond when the user clicks on a button by calling
      // the appropriate procedure.  Note that the canvas is
      // registered as a listener in the GUI21 class.
  String command = evt.getActionCommand();
  if (command.equals("Hit!"))
     doHit();
  else if (command.equals("Stand!"))
     doStand();
  else if (command.equals("New Game"))
     doNewGame();
}


void doHit() {
      // This method is called when the user clicks the "Hit!" button.
      // First check that a game is actually in progress.  If not, give
      // an error message and exit.  Otherwise, give the user a card.
      // The game can end at this point if the user goes over 21 or
      // if the user has taken 5 cards without going over 21.
  if (gameInProgress == false) {
     message = "Click \"New Game\" to start a new game.";
     repaint();
     return;
  }
  playerHand.addCard( deck.takeCardfromDeck() );
  if ( playerHand.getScore() > 21 ) {
     message = "You've busted!  Sorry, you lose.";
     gameInProgress = false;
  }
  else if (playerHand.getScore() == 5) {
     message = "You win by taking 5 cards without going over 21.";
     gameInProgress = false;
  }
  else {
     message = "You have " + playerHand.getScore() + ".  Hit or Stand?";
  }
  repaint();
}


void doStand() {
       // This method is called when the user clicks the "Stand!" button.
       // Check whether a game is actually in progress.  If it is,
       // the game ends.  The dealer takes cards until either the
       // dealer has 5 cards or more than 16 points.  Then the 
       // winner of the game is determined.
  if (gameInProgress == false) {
     message = "Click \"New Game\" to start a new game.";
     repaint();
     return;
  }
  gameInProgress = false;
  while (dealerHand.getScore() <= 16 && dealerHand.getCardsinHand() < 5)
     dealerHand.addCard( deck.takeCardfromDeck() );
  if (dealerHand.getScore() > 21)
      message = "You win!  Dealer has busted with " + dealerHand.getCardsinHand() +".";
  else if (dealerHand.getScore() == 5)
      message = "Sorry, you lose.  Dealer took 5 cards without going over 21.";
  else if (dealerHand.getScore() > playerHand.getScore())
      message = "Sorry, you lose, " + dealerHand.getScore()
                                        + " to " + playerHand.getScore() + ".";
  else if (dealerHand.getScore() == playerHand.getScore())
      message = "Sorry, you lose.  Dealer wins on a tie.";
  else
      message = "You win, " + playerHand.getScore()
                                        + " to " + dealerHand.getScore() + "!";
  repaint();
  }


  void doNewGame() {
      // Called by the constructor, and called by actionPerformed() if
      // the use clicks the "New Game" button.  Start a new game.
      // Deal two cards to each player.  The game might end right then
      // if one of the players had blackjack.  Otherwise, gameInProgress
      // is set to true and the game begins.
  if (gameInProgress) {
          // If the current game is not over, it is an error to try
          // to start a new game.
     message = "You still have to finish this game!";
     repaint();
     return;
  }
  deck = new Deck();   // Create the deck and hands to use for this game.
  dealerHand = new Hand();
  playerHand = new Hand();
  deck.shuffle();
  dealerHand.addCard( deck.takeCardfromDeck() );  // Deal two cards to each player.
  dealerHand.addCard( deck.takeCardfromDeck() );
  playerHand.addCard( deck.takeCardfromDeck() );
  playerHand.addCard( deck.takeCardfromDeck() );
  if (dealerHand.getScore() == 21) {
      message = "Sorry, you lose.  Dealer has Blackjack.";
      gameInProgress = false;
  }
  else if (playerHand.getScore() == 21) {
      message = "You win!  You have Blackjack.";
      gameInProgress = false;
  }
  else {
      message = "You have " + playerHand.getScore() + ".  Hit or stand?";
      gameInProgress = true;
  }
  repaint();
  }  // end newGame();


  public void paintComponent(Graphics g) {
     // The paint method shows the message at the bottom of the
     // canvas, and it draws all of the dealt cards spread out
     // across the canvas.

  super.paintComponent(g); // fill with background color.

  g.setFont(bigFont);
  g.setColor(Color.green);
  g.drawString(message, 10, getSize().height - 10);

  // Draw labels for the two sets of cards.

  g.drawString("Dealer's Cards:", 10, 23);
  g.drawString("Your Cards:", 10, 153);

  // Draw dealer's cards.  Draw first card face down if
  // the game is still in progress,  It will be revealed
  // when the game ends.

  g.setFont(smallFont);
  if (gameInProgress)
     drawCard(g, null, 10, 30);
  else
     drawCard(g, dealerHand.getCard(0), 10, 30);
  for (int i = 1; i < dealerHand.getCardsinHand(); i++)
     drawCard(g, dealerHand.getCard(i), 10 + i * 90, 30);

  // Draw the user's cards.

  for (int i = 0; i < playerHand.getCardsinHand(); i++)
     drawCard(g, playerHand.getCard(i), 10 + i * 90, 160);

  }  // end paint();


  void drawCard(Graphics g, Card card, int x, int y) {
       // Draws a card as a 80 by 100 rectangle with
       // upper left corner at (x,y).  The card is drawn
       // in the graphics context g.  If card is null, then
       // a face-down card is drawn.  (The cards are 
       // rather primitive.)
  if (card == null) {  
         // Draw a face-down card
     g.setColor(Color.blue);
     g.fillRect(x,y,80,100);
     g.setColor(Color.white);
     g.drawRect(x+3,y+3,73,93);
     g.drawRect(x+4,y+4,71,91);
  }
  else {
     g.setColor(Color.white);
     g.fillRect(x,y,80,100);
     g.setColor(Color.gray);
     g.drawRect(x,y,79,99);
     g.drawRect(x+1,y+1,77,97);
     if (card.getSuit() == card.getSuit() || card.getSuit() == card.getSuit())
        g.setColor(Color.red);
     else
        g.setColor(Color.black);
     g.drawString(card.toSymbol(), x + 10, y + 30);
     g.drawString("of", x+ 10, y + 50);
     g.drawString(card.toSymbol(), x + 10, y + 70);

  }  // end drawCard()


  } // end nested class BlackjackCanvas

  }
  } // end class HighLowGUI
包视图;
/*
////在这个小程序中,用户玩21点游戏。这个
////计算机充当经销商。用户通过单击来播放
////“点击”和“站立”按钮。
////此小程序的编程假定小程序是
////设置为大约466像素宽和大约346像素高。
////宽度刚好足够显示2行5张牌。
////高度可能比需要的大一点,
////允许一个平台上的按钮大小发生变化
////另一个。
*/
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入模型卡;
进口模型.甲板;
进口模型。手工;
公共类GUI21扩展了JApplet{
公共void init(){
//init()方法创建组件并布局小程序。
//BlackjackCanvas占据布局的中心位置。
//底部是一个面板,上面有三个按钮
//BlackjackCanvas对象侦听来自按钮的事件
//并完成了该项目的所有实际工作。
挫折背景(新颜色(130,50,40));
BlackjackCanvas board=新的BlackjackCanvas();
getContentPane().add(board,BorderLayout.CENTER);
JPanel buttonPanel=新的JPanel();
按钮面板立根(新颜色(220200180));
getContentPane().add(buttonPanel,BorderLayout.SOUTH);
JButton hit=新JButton(“hit!”);
hit.addActionListener(板);
按钮面板。添加(点击);
JButton stand=新JButton(“stand!”);
stand.addActionListener(板);
按钮面板。添加(支架);
JButton newGame=新JButton(“新游戏”);
newGame.addActionListener(板);
按钮面板。添加(新游戏);
}//end init()
公共插图getInsets(){
//指定边缘之间要保留的空间大小
//小程序及其包含的组件。背景
//颜色在此边框中显示。
返回新的插图(3,3,3);
}
//---该类的其余部分由嵌套类组成---
类BlackjackCanvas扩展JPanel实现ActionListener{
//显示纸牌游戏并执行所有工作的嵌套类
//跟踪状态和响应用户事件。
一副牌;//游戏中使用的一副牌。
手牌发牌人;//包含发牌人牌的手牌。
手牌玩家手;//包含用户卡的手牌。
String message;//在画布上绘制的消息,该消息会更改
//以反映游戏的状态。
布尔gameInProgress;//当游戏开始时设置为true,设置为false
//比赛结束时。
Font bigFont;//将用于显示消息的字体。
Font smallFont;//用于绘制卡片的字体。
BlackjackCanvas(){
//创建字体并开始第一个游戏。
挫折背景(新颜色(0120,0));
smallFont=新字体(“SansSerif”,Font.PLAIN,12);
bigFont=新字体(“衬线”,Font.BOLD,14);
doNewGame();
}
已执行的公共无效操作(操作事件evt){
//当用户单击按钮时,通过调用
//请注意,画布是
//在GUI21类中注册为侦听器。
String command=evt.getActionCommand();
if(command.equals(“Hit!”)
doHit();
else if(command.equals(“Stand!”)
doStand();
else if(command.equals(“新游戏”))
doNewGame();
}
void doHit(){
//当用户单击“点击!”按钮时,将调用此方法。
//首先检查游戏是否正在进行中。如果没有,请给出答案
//显示错误消息并退出。否则,请给用户一张卡。
//如果用户超过21岁或以上,游戏可以到此结束
//如果用户在未超过21张卡的情况下使用了5张卡。
if(gameInProgress==false){
message=“单击“新游戏”开始新游戏。”;
重新油漆();
返回;
}
playerHand.addCard(deck.takeCardfromDeck());
如果(playerHand.getScore()>21){
message=“您失败了!对不起,您输了。”;
gameInProgress=false;
}
else if(playerHand.getScore()==5){
message=“您可以在不超过21张的情况下获得5张牌,从而获胜。”;
gameInProgress=false;
}
否则{
message=“您有“+playerHand.getScore()+”。击中还是站立?”;
}
重新油漆();
}
void doStand(){
//当用户单击“站立!”按钮时,将调用此方法。
//检查游戏是否正在进行。如果正在进行,
//游戏结束。发牌人接受牌,直到
//经销商有5张卡或超过16分。然后
//比赛的胜利者已经确定。
if(gameInProgress==false){
message=“单击“新游戏”开始新游戏。”;
重新油漆();
返回;
}
gameInProgress=false;
while(dealerHand.getScore()21)
message=“您赢了!经销商已使用“+dealerHand.getCardsinHand()+”终止交易”;
else if(dealerHand.getScore()==5)
message=“对不起,您输了。经销商未超过21张就拿走了5张卡。”;
else if(dealerHand.getScore()>playerHand.getScore())
message=“对不起,您输了,”+dealerHand.getScore()
+“到“+playerHand.getScore()+”;
else if(dealerHand.getScore()==playerHand.getScore())
message=“对不起,您输了。平局时庄家赢。”;
其他的
message=“你赢了,”+playerHand.getScore()
+“到”+DealHand.getScore()+“!”;
重新油漆();
}
void doNewGame(){
//由构造函数调用,如果
//用户点击“新游戏”按钮,开始新游戏。
//给每位玩家发两张牌。游戏可能就在那时结束
//如果其中一个玩家有21点,否则,gameInProgress
//设置为true,游戏开始。
如果(游戏进程){
//如果当前游戏尚未结束,则尝试是错误的
//开始一场新的比赛。
message=“您仍然需要
Exception in thread "AWT-EventQueue-1" java.util.UnknownFormatConversionException: Conversion = 'i'
    at java.util.Formatter$FormatSpecifier.conversion(Unknown Source)
    at java.util.Formatter$FormatSpecifier.<init>(Unknown Source)
    at java.util.Formatter.parse(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.lang.String.format(Unknown Source)
    at model.Card.toSymbol(Card.java:38)
    at view.GUI21$BlackjackCanvas.drawCard(GUI21.java:266)
    at view.GUI21$BlackjackCanvas.paintComponent(GUI21.java:232)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
    at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-1" java.util.UnknownFormatConversionException: Conversion = 'i'
    at java.util.Formatter$FormatSpecifier.conversion(Unknown Source)
    at java.util.Formatter$FormatSpecifier.<init>(Unknown Source)
    at java.util.Formatter.parse(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.lang.String.format(Unknown Source)
    at model.Card.toSymbol(Card.java:38)