2015-10-01 304 views
0

因此,在我的大酒杯節目中,每場比賽結束時,節目會詢問您是否要再次參賽。我現在的主要問題是,當新遊戲開始時,分數計數器會不斷將新分數添加到舊分數中,而不是重置爲0.我不確定如何解決這個問題。以下是問題所在的兩個類。大酒杯節目總分

Player類:

public class Player{ 
private String name; 
private Card[] hand; // from 2 - 5 cards allowed 
private int cardCount, 
      chips;  

public Player() 
{ 
    hand = new Card[5]; 
    chips = 5; 
    cardCount = 0; 
} 
public Player(String n){ 
    hand = new Card[5]; 
    name = n; 
    chips = 5; 
    cardCount = 0; 
} 
public void acceptACard(Card c){ 
    hand[cardCount] = new Card(); 
    hand[cardCount] = c; 
    cardCount++; 
} 
public void showHand(int startCard) 
{ 
    for (int i = startCard; i < cardCount; i++){ 
     System.out.print(hand[i] + "\t"); // displays one card from hand 
    } 
} 

public int calcScore(){ 
int cardScore =0; 
int total = 0; 
boolean hasAce = false; 
for(int i=0; i < cardCount; i++){ 
    cardScore = hand[i].getValue(); 

if (cardScore >=11 && cardScore <=13) 
    cardScore = 10; 
else if (cardScore == 14){ 
    cardScore = 11; 
    hasAce = true; 

} 
total += cardScore;} 

if (total > 21 && hasAce == true) 
    total -= 10; 

return total; 
} 
public void incrementChips(){ 
    chips ++; 
} 
public void decrementChips(){ 
    chips --; 
} 
public int getChips(){ 

    return chips; 
} 
} 

的BlackJack類:

public class BlackJack { 
private Player human, 
       computer; 
private Deck deck = new Deck(); 
Scanner scan = new Scanner(System.in); 

public BlackJack(){ 
    human = new Player(""); 
    computer = new Player (""); 

} 

public void playGame() 
{ 

    int cardTotal = 0; 
    String answer, answer2; 
    deck.shuffle(); 

    do{ 
    for (int i = 0; i < 2; i++) 
    { 
     human.acceptACard(deck.dealACard()); 
     computer.acceptACard(deck.dealACard()); 
    } 
    System.out.print(" Human hand: "); 
    human.showHand(0); 
    System.out.print("\n Computer hand: "); 
    computer.showHand(1); 

    System.out.println("\nThe computers total points: " + 
      computer.calcScore()); 
    System.out.println("Players total points: " + human.calcScore()); 

    if(human.calcScore() == 21 && computer.calcScore() < 21) 
     System.out.println("You win"); 
    else if (computer.calcScore() == 21 && human.calcScore() < 21) 
     System.out.println("Computer wins!"); 
    else if (computer.calcScore() == 21 && human.calcScore() == 21)  
     System.out.println("Tie!");  
    else if (human.calcScore() < 21)  
     do{ 
     System.out.println("\nWould you like to hit or stay? Type hit or" + 
       " stay."); 
      answer = scan.nextLine(); 

    if(answer.equals("hit"))   
    { 
     dealHand(); 
     human.calcScore(); 
     computer.calcScore(); 
     cardTotal ++; 
    } 
     }while(cardTotal < 4 && answer.equals("hit")); 

    determineWinner(); 
    System.out.println("Would you like to play again? Enter yes or no: "); 
    answer = scan.nextLine(); 

    }while(answer.equals("yes")); 
    reportGameStatus(); 
} 
public void dealHand(){ 
    int i = 2; int j =2; 
    human.acceptACard(deck.dealACard()); 
    System.out.println("New card: "); 
    human.showHand(i++); 

    while(computer.calcScore() < 17){ 
    computer.acceptACard(deck.dealACard()); 
    System.out.println(); 
    System.out.println("Computer's new card: "); 
    computer.showHand(j++); 
    } 
    } 

    public void determineWinner(){ 
    System.out.println("\nThe computers total points: " + 
      computer.calcScore()); 
    System.out.println("Players total points: " + human.calcScore()); 

    if (computer.calcScore() > human.calcScore() && computer.calcScore()<22){ 
     System.out.println("Computer wins!"); 
     computer.incrementChips(); 
     human.decrementChips(); 
    } 
    else if (human.calcScore() > computer.calcScore() && human.calcScore() 
      <22){ 
     System.out.println("You win!!"); 
      human.incrementChips(); 
      computer.decrementChips(); 
    } 
    else if (human.calcScore() == computer.calcScore()) 
     System.out.println("Tie!"); 
    else if (human.calcScore() > 21){ 
     System.out.println("You bust! The Computer wins!"); 
     computer.incrementChips(); 
     human.decrementChips(); 
    } 
    else if (computer.calcScore() > 21){ 
     System.out.println("The Computer busts! You win!"); 
     computer.decrementChips(); 
     human.incrementChips(); 
    }  


} 

public void reportGameStatus(){ 
    if(computer.getChips() > human.getChips()) 
     System.out.println("Overall winner is the computer!"); 
    else if(human.getChips() > computer.getChips()) 
     System.out.println("You are the overall winner!"); 

} 
} 

任何幫助將非常感激。

+0

遊戲結束時如何將分數設置爲'0'?或者在循環中創建一個新的對象? –

回答

0

我覺得這裏是你的問題:

if(answer.equals("hit"))   
{ 
    dealHand(); 
    human.calcScore(); 
    computer.calcScore(); 
    cardTotal ++; 
} 

而不是讓你用舊的,並保持自己內心狀態的新對象。這意味着您的施工者不會再次使用,因此您的得分,手牌和籌碼都不會重置。

如何嘗試這樣的:

if(answer.equals("hit"))   
{ 
    dealHand(); 
    Player human = new Player(); 
    human.calcScore(); 
    Computer computer = new Coputer(); 
    computer.calcScore(); 
    cardTotal ++; 
} 

而且你必須做出一個新的甲板每次你開始一個新的遊戲:

Deck deck = new Deck(); 

編輯: 如果你要保持你的chipCount將它放在Object這將是您的Blackjack class的構造函數中的初始值。之後,如果您想更改chipCount,請致電功能chipcount.setChipcount(chips);。 Obvisoulsly你的chipcount-object應該有一個吸氣劑getChipcounts()以及得到實際的chipCount。 它看起來是這樣的:

public BlackJack(){ 
    human = new Player(""); 
    computer = new Player (""); 
    ChipCount chipCount = new Chipcount(0); 
} 

這裏是你的對象怎麼會:

class ChipCount{ 

int chipCount; 

    public ChipCount(int startChips){ 
    this.chipCount = startchips; 
    } 
    public void setChips(int chipsToAdd){ 
    this.chipCount = this.chipcount + chipsToAdd; 
    } 
    public int getChips(){ 
    return chipCount; 
    } 
} 

要求的youre之前。當然,您可以製作兩個對象(ChipCountPlayer & ChipCountComputer)。也有給予setChips & getChips另一種說法類似的可能性:

class ChipCount{ 

     int chipCountPlayer; 
     int chipCountComp; 

    public ChipCount(int startChips){ 
     this.chipCountPlayer = startchips; 
     this.chipCountComp = startchips; 
    } 
    public void setChips(int chipsToAdd, String player){ 
     if(player.equals("player")){ 
     this.chipCountPlayer = this.chipcountPlayer + chipsToAdd; 
     } else if (player.equals("computer")){ 
      this.chipCountComp = this.chipcountComp + chipsToAdd; 
      } 
     } 
     public int getChips(String player){ 
     if(player.equals("player")){ 
     return chipCountPlayer; 
     } else if (player.equals("computer")){ 
     return chipCountcomp; 
     } 
     } 
    } 

,這將是另一種解決方案:P

PS:我不喜歡大酒杯,做電腦甚至有芯片?無論如何,如果你想進一步擴展你的程序,你可以用另一個玩家替換它:D

+0

感謝創建新的對象確實解決了這個問題,但它重置了我不希望它做的芯片數量。 – Paige

+0

看看我的編輯:D,如果你對它感到滿意,請標記我的回答:) –

+0

非常感謝! – Paige