简体   繁体   English

大酒杯节目总成绩

[英]Blackjack program score total

So in my blackjack program when each game ends the program asks you if you would to play again. 因此,在我的二十一点程序中,每局比赛结束时,程序都会询问您是否要再次玩。 My main problem right now is that when the new game is started the score counter just keeps adding the new score to the old score instead of resetting to 0. Im not really sure how to fix it. 我现在的主要问题是,在开始新游戏时,得分计数器只会将新得分添加到旧得分中,而不是重置为0。我不确定如何解决它。 Here are the two classes where the problem is. 这是问题所在的两个类别。

Player class: 玩家等级:

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 class: 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!");

}
}

Any help would be much appreciated. 任何帮助将非常感激。

I think here is your problem: 我认为这是您的问题:

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

Instead of making new Objects you use the old ones and keep their inner state. 不用创建新对象,而是使用旧对象并保持其内部状态。 That means your construktor is not used a second time and therefore your score, hand, chips are not resetted. 这意味着您的建筑商将不再使用,因此您的得分,手牌和筹码也不会重置。

How about trying this: 尝试一下:

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

Also you have to make a new Deck everytime you start a new game: 另外,每次启动新游戏时,您都必须创建一个新的Deck:

Deck deck = new Deck();

Edit: If you want to keep your chipCount place it in an Object wich will be initialiset in the constructor of your Blackjack class . 编辑:如果您想将chipCount放置在Object则将在Blackjack class的构造函数中进行初始化。 After that call a function chipcount.setChipcount(chips); 之后调用一个函数chipcount.setChipcount(chips); if you want to change the chipCount . 如果要更改chipCount Obvisoulsly your chipcount-object should have a getter getChipcounts() as well to get the actual chipCount . 显然,您的chipcount-object应该具有getter getChipcounts()来获取实际的chipCount It could look like this: 它可能看起来像这样:

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

here is how your object would be: 这是您的对象的样子:

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;
  }
}

Before youre asking. 在你问。 Of course you could make two objects (ChipCountPlayer & ChipCountComputer). 当然,您可以创建两个对象(ChipCountPlayer和ChipCountComputer)。 Also there is the possibility of giving setChips & getChips another argument like: 也可以给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;
        }
      }
  }

that would be the other solution :P 那将是另一种解决方案:P

PS: I am not fond of blackjack, does the computer even have chips? PS:我不喜欢二十一点,计算机甚至有芯片吗? Anyway you could replace the comp with another player if you want to further extent your programm :D 无论如何,如果您想进一步扩展程序,则可以用其他播放器替换伴奏:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM