Yahtzee应用程序类

时间:2014-04-18 17:47:11

标签: java

在编程方面,我有点像菜鸟,我可以真正使用帮助完成我一直在做的任务。我正在尝试使用这个预期输出的yahtzee程序(滚动的骰子是随机的)

Round 1 
Roll 1: 
5 6 5 5 2 
Which die value do you want to keep? 
5 
Roll 2: 
5 6 5 5 3 
Which die value do you want to keep? 
5 
Roll 3: 
5 1 5 5 5 
Which location would you like to record this round? 
Scores: 
1s: 0 points 
2s: 0 points 
3s: 0 points 
4s: 0 points 
5s: 0 points 
These values are automatically output 
to the console when you call the 
showDice method in the GUI class.  Page 6 of 9
6s: 0 points 
5 
Round 1 complete. 
Scores: 
1s: 0 points 
2s: 0 points 
3s: 0 points 
4s: 0 points 
5s: 20 points 
6s: 0 points 

Round 2 
Roll 1: 
1 2 4 4 3 
Which die value do you want to keep? 
4 
Roll 2: 
3 4 4 4 6 
Which die value do you want to keep? 
4 
Roll 3: 
4 4 4 4 4 
Which location would you like to record this round? 
Scores: 
1s: 0 points 
2s: 0 points 
3s: 0 points 
4s: 0 points 
5s: 20 points 
6s: 0 points 
4 
Round 2 complete. 
Scores: 
1s: 0 points 
2s: 0 points 
3s: 0 points 
4s: 20 points 
5s: 20 points 
6s: 0 points 

Round 3 
Roll 1: 
5 1 4 1 6 
Which die value do you want to keep? 
5 
Roll 2: 
5 2 4 1 3 
Which die value do you want to keep? 
5 
Roll 3: 
5 6 3 1 3 
Which location would you like to record this round? 
Scores: 
1s: 0 points 
2s: 0 points 
3s: 0 points 
4s: 20 points 
5s: 20 points 
6s: 0 points  Page 7 of 9
5 
There is already a score for location 5. Which location would you like to 
record this round? 
Scores: 
1s: 0 points 
2s: 0 points 
3s: 0 points 
4s: 20 points 
5s: 20 points 
6s: 0 points 
4 
There is already a score for location 4. Which location would you like to 
record this round? 
Scores: 
1s: 0 points 
2s: 0 points 
3s: 0 points 
4s: 20 points 
5s: 20 points 
6s: 0 points 
3 
Round 3 complete. 
Scores: 
1s: 0 points 
2s: 0 points 
3s: 6 points 
4s: 20 points 
5s: 20 points 
6s: 0 points 

Round 4 
Roll 1: 
1 3 2 3 1 
Which die value do you want to keep? 
1 
Roll 2: 
1 2 2 5 1 
Which die value do you want to keep? 
2 
Roll 3: 
2 2 2 2 1 
Which location would you like to record this round? 
Scores: 
1s: 0 points 
2s: 0 points 
3s: 6 points 
4s: 20 points 
5s: 20 points 
6s: 0 points 
2 
Round 4 complete. 
Scores: 
1s: 0 points 
2s: 8 points 
3s: 6 points 
4s: 20 points 
5s: 20 points 
6s: 0 points 

The player can choose to 
keep the value 1 after the 
first roll, but choose to 
keep value 2 after the 
second roll. 
If the player attempts to enter a score in 
a location that already has a score, they 
are told the location already has a score 
and are asked again to choose a 
location. They will continue to be 
asked until they enter an empty 
location.  Page 8 of 9
Round 5 
Roll 1: 
4 5 1 5 6 
Which die value do you want to keep? 
6 
Roll 2: 
1 2 5 4 6 
Which die value do you want to keep? 
6 
Roll 3: 
6 1 1 1 6 
Which location would you like to record this round? 
Scores: 
1s: 0 points 
2s: 8 points 
3s: 6 points 
4s: 20 points 
5s: 20 points 
6s: 0 points 
6 
Round 5 complete. 
Scores: 
1s: 0 points 
2s: 8 points 
3s: 6 points 
4s: 20 points 
5s: 20 points 
6s: 12 points 

Round 6 
Roll 1: 
1 6 4 3 2 
Which die value do you want to keep? 
1 
Roll 2: 
1 6 6 1 2 
Which die value do you want to keep? 
1 
Roll 3: 
1 5 5 1 6 
Which location would you like to record this round? 
Scores: 
1s: 0 points 
2s: 8 points 
3s: 6 points 
4s: 20 points 
5s: 20 points 
6s: 12 points 
1 
Round 6 complete. 
Scores: 
1s: 2 points 
2s: 8 points 
3s: 6 points 
4s: 20 points 
5s: 20 points 
6s: 12 points 

游戏结束!你的总分是68

这是我到目前为止的代码

  package proj5;
    import java.util.Scanner;

    public class Project5App {

        public static void main (String[] args)
        {
            Scanner sc = new Scanner(System.in);
            GUI diceTable = new GUI();
            DiceArray dice = new DiceArray(5);
            int[] scoreCard = new int[]{0,0,0,0,0,0};
            int count = 1;

            for(int j = 1; j < 7; j++)// j: rounds
            {
                System.out.println("Round " + j);
                for (int i = 1; i <= 3; i++)// i: the rolls 
                {
                    if(i < 3)
                    {
                        System.out.println("Roll " + i + ":");
                        dice.roll();
                        diceTable.showDice(dice);
                        System.out.println("Which die value do you want to keep?");
                        int keep = sc.nextInt();
                        dice.keep(keep);
                    }

                    if (i == 3)
                    {   
                        System.out.println("Roll " + i + ":");
                        dice.roll();
                        diceTable.showDice(dice);
                        System.out.println("Which location would you like to record this round?\nScores:");
                        for(int k = 0; k < 6; k++)//k: score card location
                        {
                            if (scoreCard[k] > 0)
                                System.out.println("There is already a score for location " );
                                System.out.println(count + "s: " + scoreCard[k] + " points");
                                count++;
                        }
                        count = 1;
                        int location = sc.nextInt();
                        int sum = dice.calculateTotal(location);
                        System.out.println(location);

                        System.out.println("Round " + j + " complete.");
                        dice.reset();   
                    }
                }
            }
        }   
    }

我不太确定如何在不进行2次循环的情况下让我的记分卡在一轮中显示两次,而且我不知道如何在本轮结束时将记分卡中显示的添加骰子显示出来。我不能自己想出这一个。任何人都可以提供任何见解吗?

编辑:下面是整个DiceArray类我的预期输出和我想要它做的是上面写的。

    package proj5;
/**
 * <p>Title: Project 5</p>
 *
 * <p>Description: Creates an array of dice that rolls, keeps and displays the values of each die. </p>
 *
 * @author 
 */

public class DiceArray 
{
    //instance variables
    private int[] dice;
    private boolean[] diceToRoll;

    /**
     * Name: Parameterized Constructor </P>
     * Description: Receives the number of dice, and instantiates both instances variables. </p>
     * @param numDice the number of dice to be used
     */
    public DiceArray(int numDice)
    {
        dice = new int[numDice];
        diceToRoll = new boolean[numDice];

        for(int i = 0; i < dice.length; i++)
        {   
            dice[i] = -1;
            diceToRoll[i] = true;
        }   
    }

    /**
     * Name: toString method </P>
     * Description: Create and return a string with the values of the dice, and whether or not they are kept. </p>
     * @return a String with the values of the die and whether or not they are true or false
     */
    public String toString()
    {
        String output = "";
        for(int i = 0; i < dice.length; i++)
        {
            output += dice[i] + " ";
        }
        output += "\n";
        for(int i = 0; i < dice.length; i++)
        {   
            if (diceToRoll[i] == true)
                output += "T" + " ";
            else
                output += "F" + " ";
        }

        return output;
    }

    /**
     * Name: roll method </P>
     * Description: Rolls dice based on which ones are set to 'true'. </p>
     */
    public void roll()
    {
        for (int i = 0; i < dice.length; i++)
        {   
            if (diceToRoll[i] == true)
                dice[i] = (int)(Math.random() * 6 + 1);
        }   
    }

    /**
     * Name: getDice method </P>
     * Description: Return the reference of the array dice. </p>
     * @return the value of the reference of the array.
     */
    public int[] getDice()
    {
        return dice;
    }

    /**
     * Name: reset method </P>
     * Description: Sets all the diceToRoll values to true, allowing them all to be 
     * rolled the next time the roll method is called. </p>
     */
    public void reset()
    {
        for (int i = 0; i < dice.length; i++)
            diceToRoll[i] = true;
    }

    /**
     * Name: keep method </P>
     * Description: Will accept an int that indicates which of the dice value that should be kept. </p>
     * @param diceVal the value to be compared to the dice values
     */
    public void keep(int diceVal)
    {
        for(int i = 0; i < dice.length; i++)
        {
            if(dice[i] == diceVal)
                diceToRoll[i] = false;
            else
                diceToRoll[i] = true;
        }
    }

    /**
     * Name: calculateTotal method </P>
     * Description: Will accept an int indicating which of the values to calculate a total for. </p>
     * @param val the value of the dice values to be added together
     * @return an int of the sum of the values
     */ 
    public int calculateTotal(int val)
    {
        int sum = 0;

        for(int i = 0; i < dice.length; i++)
        {
            if (dice[i] == val)
                sum = sum + val;
        }

        return sum;
    }


}

0 个答案:

没有答案