I am in a beginner's java class. I have had this yahtzee program now for 3 weeks and I still cannot get this figured out. I need to roll 5 die twice seeing if I get a yahtzee (5 die the same) I am having trouble saving my die in the first roll to roll again My code is as follows. I'm sure there are many things that can be simplified(if into switch statements) but right now I'm concerned with getting these methods to work.
My Code.
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);
}
}
my roll again method is not complete as my save dice does not work.
My driver is as follows but does nothing so far in the output which is part of my problem
public class YahtzeeFinal
{
public static void main(String [] args)
{
Yahtzee yaht = new Yahtzee();
yaht.roll();
yaht.saveDice();
}
}
Thank you for any advice.
Aucun commentaire:
Enregistrer un commentaire