2017-09-21 14 views
-4

コードがコンパイラを通過するたびに、同じ数が無限に印刷されます。乱数を数回コンパイルする必要があります。私は、whileループを使って、より多くの時間を印刷しようとしました。ただし、同じ番号が印刷されます。このゲームの目的は、2人のプレーヤー(AIと1人)が最初に100ポイントに達することを競うことです。プレイヤーが2つのポイントを獲得するたびにポイントがリセットされます。サイコロゲームの問題

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

**あなたのループが始まる前に、サイコロを振るだけです**。そして、ループが実行されている間、彼らはずっと同じ値を持っています。 –

+0

どうすればこの問題を解決できますか? – agradable

+4

ループ内のダイスを再ロールします。 – Berger

答えて

3

ループの内側にrolls()を呼び出す必要があります。スニペットで貼り付けたロールはループの前に1回作成され、その中にこの1つのロールの結果のみが印刷されます(実際には無限ループになるか、すぐにロールする場合は実行されません。あなたの中にはwhile)。

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

私はあなたが与えられた要件に対する解決策を提供しているとは思わない:

  1. 2人のプレイヤー(ただし、3人含まれている - ジョー、デイブとクルトが)。
  2. プレーヤーが2つ連続している場合、そのプレーヤーのスコアはリセットされます(たとえば、Daveが1つをロールし、次のターンに別のロールをロールすると、スコアは0にリセットされます)。

私の以前の答えでは、あなたのコードをきちんと整理しようとしていました。私は実際の要件を見ていませんでした。

プレイヤーがサイコロの1ポイントにつき1ポイントを獲得した場合、またはポイントした値がスコアに加算された場合は、あなたは言及していません。私はそれが後者であると仮定しよう。

「プレーヤー」という別のクラスを含める必要があります。 Playerクラスには、現在のロールの内容、前のロールのロール、プレーヤーの現在のスコアを格納する変数が含まれています。また、サイコロを振る方法、プレイヤーが100点に達しているかどうかをチェックし、プレイヤーが2つの連続したものをロールしたかどうかをチェックする方法も含まれていなければなりません。

はここで非常に単純な実装です(勝者がで勝ったことは、最後に勝ったプレイヤーを教えてくれますし、どのように多くのポイント):

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人の選手を選ぶことができるように)選手の配列を使用することができます。

0

さて、最後の1(私は約束):上記を使用して

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

は、あなたが簡単にプレーヤーの数、サイコロの面の数、そして勝利のために到達する必要があり、スコアを変更することができます。

関連する問題