二十一点计划总分

时间:2015-10-01 07:31:43

标签: java

所以在我的二十一点程序中,当每个游戏结束时,程序会询问你是否要再玩一次。我现在的主要问题是,当新游戏开始时,分数计数器只是将新分数添加到旧分数而不是重置为0.我不确定如何解决它。以下是问题所在的两个类。

玩家类:

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

}
}

非常感谢任何帮助。

1 个答案:

答案 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();

修改 如果您希望chipCountObject放在Blackjack class中,则chipcount.setChipcount(chips);将在chipCount的构造函数中初始化。如果您想更改chipcount-object,请调用函数getChipcounts()。显然,您的chipCount应该有一个getter 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; } } 。 它看起来像这样:

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

以下是您的对象:

A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +coord_flip() 
B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip() 

library(cowplot)
plot_grid(A, B, ncol=1, align="v")

在你问之前。当然你可以制作两个对象(ChipCountPlayer和ChipCountComputer)。还有可能给setChips&amp; getChips另一个参数如:

{{1}}

这将是另一种解决方案:P

PS:我不喜欢二十一点,电脑是否还有筹码?无论如何,如果你想进一步扩展你的程序,你可以用另一个玩家替换comp:D