所以我正在为高中的Comp Sci课程开始我的最终项目,我让Jframe弹出所有按钮,但是交易,点击和站立按钮都不起作用。它应该能够下注,然后用出现的牌交出新牌。我可以打赌,但是当我用新手牌时,牌不会出现,我无法击中或站立。 请记住,我还是一个初学者,所以请不要把我烤得太糟糕。 这是董事会成员:
package blackjack;
public class BlackjackBoard {
/**
* Instance variables needed for a one-player blackjack game.
*/
private Deck deck;
private BlackjackHand myHand;
private BlackjackHand dealerHand;
private int myTotal;
private int dealerTotal;
private int myMoney;
private int myBet;
private int winner;
// The message to the user. It will change as the game is played and buttons
// are clicked.
private String message;
// Write the constants needed to initialize the deck.
// Be sure to use lower case when writing the words.
/**
* The ranks of the cards for this game to be sent to the deck.
*/
private static final String[] RANKS = { "2", "3", "4", "5", "6", "7", "8",
"9", "10", "jack", "queen", "king", "ace" };
/**
* The suits of the cards for this game to be sent to the deck.
*/
private static final String[] SUITS = { "spades", "diamonds", "clubs",
"hearts" };
/**
* The values of the cards for this game to be sent to the deck.
*/
private static final int[] POINT_VALUES = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 10,
10, 10, 1};
/**
* Creates a new BlackjackBoard instance.
*/
public BlackjackBoard() {
deck = new Deck(RANKS, SUITS, POINT_VALUES);
myHand = new BlackjackHand();
dealerHand = new BlackjackHand();
myTotal = 0;
dealerTotal = 0;
myMoney = 100;
myBet = 0;
message = "Place your bet.";
}
public void bet(boolean gameInProgress) {
if (!gameInProgress) {
if (myMoney <= 0) {
message = "You cannot bet any more.";
} else {
// Write the code to change the appropriate
// instance variables with a bet of $1.
myBet++;
myMoney--;
message = "Continue to bet or press \"Deal New\".";
}
} else {
message = "You cannot change your bet.";
}
}
public boolean dealNewGame(boolean gameInProgress) {
if (!gameInProgress && myBet > 0) {
message = "";
gameInProgress = true;
// Write the code for the initial deal (player, dealer, player,
// dealer).
Deck deck = new Deck(RANKS, SUITS, POINT_VALUES);
myHand.addCard(deck.deal());
dealerHand.addCard(deck.deal());
deck.shuffle();
;
dealerHand.addCard(deck.deal());
myHand.addCard(deck.deal());
dealerHand.addCard(deck.deal());
// Now test for a dealer blackjack.
if (dealerHand.hasBlackjack()) {
// Code for what happens if the dealer has blackjack.
dealerBlackjack();
dealerWins();
} else {
if (myHand.hasBlackjack()) {
myBlackjack();
iWin();
} else {
// There were no blackjacks. Write the code to update
// the total instance variables.
myTotal = myHand.getBlackjackValue();
dealerTotal = dealerHand.getBlackjackValue();
}
}
} else if (!gameInProgress && myBet == 0) {
message = "Please place a bet first.";
} else {
message = "You must finish this game first.";
}
return gameInProgress;
}
public void clear(boolean gameInProgress) {
if (gameInProgress) {
message = "You must finish this game first.";
} else {
myHand.clear();
dealerHand.clear();
myBet = 0;
myTotal = 0;
dealerTotal = 0;
message = "Place your bet";
}
}
public boolean hit(boolean gameInProgress) {
if (gameInProgress) {
// Deal an additional card to the player, find the new total,
// and clear any messages.
myHand.addCard(deck.deal());
myTotal = myHand.getBlackjackValue();
if (myTotal > 21) {
message = "Bust. ";
// What method call should be here?
return false;
}
} else {
message = "You must bet and deal first.";
return false;
}
return true;
}
public boolean stand(boolean gameInProgress) {
if (gameInProgress) {
// Get the dealer total.
// Use the while loop to draw cards for the dealer.
// The dealer hits on 16 and below, stands on 17 and above.
dealerTotal = dealerHand.getBlackjackValue();
while (dealerTotal < 17) {
{
// Deal a card to the dealer and find the total.
dealerHand.addCard(deck.deal());
if (dealerTotal > 21)// What code goes here?:)
{
winner = 2;
message = "Dealer busts. ";
// Who wins? Which method should be called.
dealerWins();
return false;
}
}
// Check for a winner. Careful with the return value.
message = "";
if (winner == -1) {
// Who won? Call the appropriate method.
dealerWins();
} else if (winner == 2) {
// Who won? Call the appropriate method.
iWin();
} else {
// Who won? Call the appropriate method.
push();
}
}
} else {
message = "You must bet and deal first.";
}
return false;
}
private void payout() {
// Write code to add the winnings (or losings) to myMoney.
if(winner == -1){
myMoney -= myBet;
}
if(winner == 2){
myMoney += myBet;
}
winner = 0;
myBet = 0;
}
private void dealerBlackjack() {
message = "Dealer has Blackjack! ";
this.dealerWins();
}
private void myBlackjack() {
message = "You have Blackjack! ";
this.iWin();
}
private void dealerWins() {
message = message + "Dealer wins.";
winner = 0;
this.payout();
}
private void iWin() {
message = message + "You win.";
winner = 2;
this.payout();
}
private void push() {
message = "Push.";
winner = 1;
this.payout();
}
// Write each of the getter methods below.
public String getMessage() {
return message;
}
public int getMyMoney() {
return myMoney;
}
public String getMyBet() {
return Integer.toString(myBet);
}
public BlackjackHand getMyHand() {
return myHand;
}
public BlackjackHand getDealerHand() {
return dealerHand;
}
public Card getCard(BlackjackHand theHand, int location) {
return theHand.getCard(location);
}
}
这是它给我的错误:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Card image not found: "cards/4diamonds.GIF"
at blackjack.BlackjackGUI.repaint(BlackjackGUI.java:157)
at blackjack.BlackjackGUI.actionPerformed(BlackjackGUI.java:251)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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)