我是初学者的java课程。我现在有这个yahtzee计划已经有3个星期了,我仍然无法理解这一点。我需要两次掷骰子,看看我是否得到了一个yahtzee(5个相同的模具)我在第一次滚动中保存我的模具再次滚动时遇到问题我的代码如下。我确信有很多东西可以简化(如果进入switch语句)但是现在我关心的是让这些方法起作用。
我们的老师为我们提供了如下使用的模具课程
import java.util.*;
public class Yahtzee
{
int a, b, c, d, e;
Die die1 = new Die();
Die die3 = new Die();
Die die4 = new Die();
Die die5 = new Die();
Die die2 = new Die();
Scanner sc = new Scanner(System.in);
ArrayList<Integer> dice2;
int arrayLength;
Die[] dice = new Die[5];
//Constructor
public Yahtzee()
{
for(int i = 0; i < dice.length; i ++)
{
dice[i] = new Die();
}
}
public void roll()
{
for(int i = 0; i < dice.length; i ++)
{
dice[i].roll();
}
}
public void saveDice()
{
dice2 = new ArrayList<Integer>();
for(int i = 0; i < dice.length; i ++)
{
dice[i].getVal();
for(int i2 = 0; i2 < dice.length; i2 ++)
{
if(i != i2)
{
if(dice[i] == dice[i2])
{
dice2.add(dice[i].getVal());
dice2.add(dice[i2].getVal());
a = dice[i].getVal();
if(a == 5)
{
System.out.println("You have " + dice2.size() + "6's");
}
else if(a == 5)
{
System.out.println("You have " + dice2.size() + "5's");
}
else if(a == 4)
{
System.out.println("You have " + dice2.size() + "4's");
}
else if(a == 3)
{
System.out.println("You have " + dice2.size() + "3's");
}
else if(a == 2)
{
System.out.println("You have " + dice2.size() + "2's");
}
else if(a == 1)
{
System.out.println("You have " + dice2.size() + "1's");
}
b = dice2.size();
}
if(dice2.size() == 0)
{
if(a == 6)
{
System.out.println("No dice are the same. We kept 6 because its the largest face value.");
}
else if(a == 5)
{
System.out.println("No dice are the same. We kept 5 because its the largest face value.");
}
else if(a == 4)
{
System.out.println("No dice are the same. We kept 4 because its the largest face value.");
}
else if(a == 3)
{
System.out.println("No dice are the same. We kept 3 because its the largest face value.");
}
else if(a == 2)
{
System.out.println("No dice are the same. We kept 2 because its the largest face value.");
}
else if(a == 1)
{
System.out.println("No dice are the same. We kept 1 because its the largest face value.");
}
}
}
}
}
}
public void rollAgain()
{
arrayLength = dice2.size();
System.out.println(arrayLength);
}
}
我的Yahtzee代码。
public class YahtzeeFinal
{
public static void main(String [] args)
{
Yahtzee yaht = new Yahtzee();
yaht.roll();
yaht.saveDice();
}
}
我的掷骰再次方法未完成,因为我的保存骰子不起作用。
我的驱动程序如下,但在输出中没有任何操作,这是我的问题的一部分
{{1}}
感谢您的任何建议。
答案 0 :(得分:1)
从您的代码中可以看出saveDice()
正在尝试自动为其他角色保存最佳配置。遇到这样的问题时,请始终尝试使用示例手动运行代码。
例如,dice = {1, 2, 3, 4, 5}
会发生什么,dice = {1, 1, 2, 2, 3}
会发生什么(想想第二次)。
我建议首先仔细考虑算法。例如,如果算法的目标是保持骰子数量最多,那么您需要考虑如何逐步实现目标。我会开始像:
从这两个简单点可以得到以下结论:
public static void Main(String []){
Yahtzee game = new Yahtzee();
game.roll();
game.saveHighest();
game.reroll();
}
public Yahtzee{
// A boolean may help me save which dice to roll ?
boolean [] saved = new boolean[5];
public Yahtzee(){
//initialise saved to false;
}
...
public void saveHighest(){
int highestOccuringNumber = getHighest();
for (int i = 0; i < dice.length; i ++){
if(highestOccuringNumber == dice[i]){
saved[i] = true;
}
}
}
//Nothing complicated just need to get the highest number
public int getHighest(){
//Loop through and get the number which occurs most often
// For example dice={1,1,2,3,4}. Highest = 1;
//If no number occurs more than once just return highest occurring number
//For example dice={1,2,3,4,5}. Highest = 5;
}
///Reroll what isn't saved
public void reroll(){
for (int i = 0; i < dice.length; i ++){
if(!saved[i]){
dice[i].roll();
}
}
}
}
请注意,上面的代码不是执行此问题的最佳方式。它是一个例子,说明如何逐个解决问题将帮助您提出解决方案。
希望这会让你走上正轨。
答案 1 :(得分:1)
不确定您的代码是否正常工作,或者您是否还在使用它,但这是我想出的。我有点玩得开心。正如您可能已经注意到的那样,我试图简化您所拥有的代码的某些部分,所以也许您会发现其中一些有用:)如果您确实使用了其中的一部分并遇到问题,请告诉我,我和我#39;再看看。
import java.util.*;
public class Yahtzee {
ArrayList<Die> savedDice = new ArrayList<Die>();
// Constructor
public Yahtzee() {
}
// renamed method to avoid confusion
public void rollDice(int num) {
Die[] dice = new Die[num];
for (int d = 0; d < dice.length; d++) {
Die nextDie = new Die();
nextDie.roll();
dice[d] = nextDie;
}
saveDice(dice);
}
public void saveDice(Die[] dice) {
// int array will count occurrences of each face value
int[] values = new int[] { 0, 0, 0, 0, 0, 0 };
for (Die d : dice) {
values[d.getVal() - 1]++;
}
int most = -1;
int temp = 0;
for (int j = 0; j < values.length; j++) {
if (values[j] >= temp) {
most = j + 1;
temp = values[j];
}
}
// Thought something like this looked much cleaner than having conditions for each value.
if (temp > 1) {
System.out.println("You have " + temp + " " + most + "'s");
} else {
System.out.println("No dice are the same. We kept " + most
+ " because it's the largest face value.");
}
for (Die d : dice) {
if (d.getVal() == most) {
savedDice.add(d);
}
}
}
}