骰子游戏的问题

时间:2017-09-21 09:21:00

标签: java netbeans

每当代码通过编译器时,它会无限次打印出相同的数字,我需要多次编译随机数。我尝试使用while循环打印出更多次。然而,它只打印出相同的数字。游戏的目的是让两个玩家(以及AI和一个人)竞争首先达到100分,每当玩家获得两个1时,这些点将重置。

class piggame {

    public static void main(String[] args) {
        Dice d1 = new Dice();
        d1.rolls();
        int dave = d1.rolls();

        Dice d2 = new Dice();
        d2.rolls();
        int joe = d2.rolls();

        Dice d3 = new Dice();
        d3.rolls();
        int kurt = d1.rolls() + d2.rolls();

        int sum1 = d1.rolls() + d2.rolls(); 
        sum1 += d1.rolls() + d2.rolls();


        while (dave != 1 && joe != 1){        
            System.out.println("you have rolled " + kurt);
        }   
    }      
} 

class Dice {

    public int rolls() {
        Random r = new Random();
        return r.nextInt(6) +1;
    }
}

5 个答案:

答案 0 :(得分:3)

你应该在循环中调用rolls()。在片段中你粘贴的卷在循环之前制作一次,然后在里面你只打印这一卷结果(这确实是无限循环或者在滚动成为正确的情况下不会执行,因为从不制作卷再次当你进入while)时。

答案 1 :(得分:1)

此代码并不完美,但它可以解决您的问题。

public static void main(String[] args) {
 int dave = 0;
 int joe = 0;
 int kurt = 0;
 while (dave != 1 && joe != 1){
   Dice d1 = new Dice();
   d1.rolls();
   dave = d1.rolls();

   Dice d2 = new Dice();
   d2.rolls();
   joe = d2.rolls();

   Dice d3 = new Dice();
   d3.rolls();
   kurt = d1.rolls() + d2.rolls();

   int sum1 = d1.rolls() + d2.rolls(); 
   sum1 += d1.rolls() + d2.rolls();
   System.out.println("you have rolled " + kurt);
  }
}    

答案 2 :(得分:0)

import java.util.Random;

class PigGame 
{
  public static void main(String[] args) 
  {
    int dave = 0;
    int joe = 0;
    int kurt = 0;
    int roll_count = 0;

    while (dave != 1 && joe != 1)
    {
      System.out.println("-----------------------------------------------");
      System.out.println("Roll number: " + roll_count++);
      System.out.println("-----------------------------------------------");

      dave = new Dice().roll();
      joe  = new Dice().roll();
      kurt = new Dice().roll();

      System.out.println("Dave rolled: " + dave);
      System.out.println("Joe rolled: " + joe);
      System.out.println("Kurt rolled: " + kurt);
      System.out.println("");
    }
  }   
} 


class Dice {
  public int roll() { return new Random().nextInt(6) + 1;   }
}

答案 3 :(得分:0)

我不认为您提供了满足给定要求的解决方案:

  1. 两名球员(但你有三名球员 - 乔,戴夫和库尔特)。
  2. 如果玩家连续两次滚动,那么该玩家的分数将被重置(例如,如果玩家Dave滚动一个然后在下一回合滚动另一个,则他的分数将重置为0)。
  3. 在我之前的回答中,我只是想整理你的代码。我没有看到实际的要求。

    如果玩家每次掷骰子获得一个点数,或者他们滚动的值被添加到他们的分数中,你还没有提到。我会认为它是后者。

    你应该包括另一个名为" Player"的类。 Player类将包含用于存储当前滚动的内容,前一滚动内容以及玩家当前得分的变量。它还应该包括掷骰子的方法,检查玩家是否达到100分,检查玩家是否连续两次滚动。

    这是一个非常简单的实现(它将告诉你哪个玩家最终获胜,以及获胜者赢得了多少分):

    import java.util.Random;
    
    // -----------------------------------------------------------------------
    // -----------------------------------------------------------------------
    class PigGame 
    {
      public static void main(String[] args) 
      {
        Player player1 = new Player("Dave");
        Player player2 = new Player("Joe");
        int roll_count = 0;
    
        while (!player1.reachedFinishingScore() && !player2.reachedFinishingScore())
        {
          int p1_roll = player1.roll();
          int p2_roll = player2.roll();
    
          System.out.println("-----------------------------------------------------");
          System.out.println("Roll number: " + roll_count++);
          System.out.println("-----------------------------------------------------");
          System.out.println(player1.get_name() + " rolled: " + p1_roll + ". Total Score: " + player1.get_score());
          System.out.println(player2.get_name() + " rolled: " + p2_roll + ". Total Score: " + player2.get_score());
          System.out.println("");
        }
    
        if (player1.get_score() == player2.get_score()) 
          System.out.println("It was a draw!");
        else if (player1.get_score() > player2.get_score()) 
          System.out.println(player1.get_name() + " wins by " + (player1.get_score() - player2.get_score()) + " points!");
        else 
          System.out.println(player2.get_name() + " wins by " + (player2.get_score() - player1.get_score()) + " points!");
    
        System.out.println("");
      }
    } 
    
    // -----------------------------------------------------------------------
    // -----------------------------------------------------------------------
    class Dice {
      public int roll() { return new Random().nextInt(6) + 1;   }
    }
    
    // -----------------------------------------------------------------------
    // -----------------------------------------------------------------------
    class Player
    {
      private String name; // Player's name.
      private int prev_roll = 0; // Value of Player's previous roll.
      private int curr_roll = 0; // Value of Player's current roll.
      private int score = 0;     // The Player's score.
    
      public Player(String name) { this.name = name; }
    
      public int roll()
      {
        int curr_roll = new Dice().roll();
        this.prev_roll = this.curr_roll; // Make previous roll value of last current roll.
        this.curr_roll = curr_roll;      // Set value of current roll to what was just rolled.
        this.score += curr_roll;
        if (rolledTwoOnes()) this.score = 0;
    
        return curr_roll;
      }
    
      private boolean rolledTwoOnes() { return (this.prev_roll == 1 && this.curr_roll == 1); }
      public boolean reachedFinishingScore() { return this.score >= 100; }
      public int get_score() { return score; }
      public String get_name() { return this.name; }
    }
    

    上述实施可以通过"硬编码"来改善。 player1和player2。相反,你可以使用一系列玩家,这样可以更容易地限制玩家数量(即你可以拥有100名玩家)。

答案 4 :(得分:0)

好的,最后一个(我保证):

import java.util.Random;
import java.util.Arrays;

// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
class Config
{
  public static final int NUM_OF_PLAYERS = 10;
  public static final int NUM_OF_DICE_FACES = 6;
  public static final int WINNING_SCORE = 100;
}

// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
class PigGame 
{
  // -----------------------------------------------------------------------
  // -----------------------------------------------------------------------      
  public static void create_players(Player[] p, int num_players)
  {
    for (int i = 0; i < num_players; i++)
      p[i] = new Player("Player " + String.format("%02d", i + 1));
  }


  // -----------------------------------------------------------------------
  // -----------------------------------------------------------------------      
  public static void all_players_roll_dice(Player[] p)
  {
    for (int i = 0; i < p.length; i++)
      p[i].roll();
  }

  // -----------------------------------------------------------------------
  // -----------------------------------------------------------------------
  public static boolean no_winners(Player[] p)
  {
    int i = 0;
    boolean bWinner = false;
    while (i < p.length && (bWinner = !p[i].reachedFinishingScore()))
      i++;

    return bWinner;
  }

  // -----------------------------------------------------------------------
  // -----------------------------------------------------------------------
  public static void display_roll_details(Player[] p, int current_roll)
  {
    System.out.println("\n--------------------------------------------------------");
    System.out.println("CURRENT ROLL: " + (current_roll + 1));
    System.out.println("--------------------------------------------------------");
    for (int i = 0; i < p.length; i++)
      System.out.println(p[i].get_name() + " rolled: " + p[i].get_roll() + 
        ". Score: " + p[i].get_score());
  }

  // -----------------------------------------------------------------------
  // -----------------------------------------------------------------------    
  public static void display_final_scores(Player[] p, int roll_count)
  {
    System.out.println("\n\n**********************************************************");
    System.out.println("FINAL SCORES AFTER " + roll_count + " ROLLS (HIGHEST TO LOWEST):");
    System.out.println("**********************************************************\n");
    for (int i = 0; i < p.length; i++)
    {
      Arrays.sort(p);
      System.out.println(p[i].get_name() + " scored: " + p[i].get_score());
    }

    System.out.println("\n\nDon't be a player hater!\n\n");
  }

  // -----------------------------------------------------------------------
  // -----------------------------------------------------------------------
  public static void main(String[] args) 
  {
    Player[] players = new Player[Config.NUM_OF_PLAYERS];

    create_players(players, Config.NUM_OF_PLAYERS);

    int roll_count = 0;
    while (no_winners(players))
    {
      all_players_roll_dice(players);
      display_roll_details(players, roll_count++);
    }

    display_final_scores(players, roll_count);

  }
} 

// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
class Dice {
  public int roll() { return new Random().nextInt(Config.NUM_OF_DICE_FACES) + 1;    }
}

// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
class Player implements Comparable<Player>
{
  private String name; // Player's name.
  private int prev_roll = 0; // Value of Player's previous roll.
  private int curr_roll = 0; // Value of Player's current roll.
  private int score = 0;     // The Player's score.

  public Player(String name) { this.name = name; }

  public int roll()
  {
    int curr_roll = new Dice().roll();
    this.prev_roll = this.curr_roll; // Make previous roll value of last current roll.
    this.curr_roll = curr_roll;      // Set value of current roll to what was just rolled.
    this.score += curr_roll;
    if (rolledTwoOnes()) this.score = 0;

    return curr_roll;
  }

  private boolean rolledTwoOnes() { return (this.prev_roll == 1 && this.curr_roll == 1); }
  public boolean reachedFinishingScore() { return this.score >= Config.WINNING_SCORE; }
  public int get_score() { return this.score; }
  public int get_roll() { return this.curr_roll; }
  public String get_name() { return this.name; }

  // For sorting the array (from highest scores to lowest).
  @Override
  public int compareTo(Player p) 
  { 
    return ((Integer)p.get_score()).compareTo(((Integer)get_score())); 
  }
}

使用上述内容,您可以轻松更改玩家数量,骰子面数以及获胜所需的分数。