school/java/Project/DiceCup.java

91 lines
1.6 KiB
Java

import java.util.ArrayList;
public class DiceCup{
private ArrayList<MyDie> dice;
private int creditBalance;
public DiceCup(int Dx) {
dice = new ArrayList<MyDie>();
creditBalance = 10;
MyDie die;
for (int i = 0; i <3; i++){
if (Dx == 8){
die = new EightDie();
}
else{
die = new SixDie();
}
dice.add(die);
}
}
/* Accessors */
public int getCredits(){
return creditBalance;
}
public ArrayList<MyDie> getDice(){
return dice;
}
public int getTotal(){
int total = 0;
for (MyDie die:dice){
total += die.getValue();
}
return total;
}
/* Actions */
public void roll(){
for (MyDie die:dice){
die.roll();
}
}
public void updateCredits(){
creditBalance -=1;
checkTriplets();
checkDoubles();
checkLarge();
}
public boolean enoughCredits(){
if (creditBalance > 0){
return true;
}
else{
return false;
}
}
public void testRoll(int [] values){
//roll dice until desired 3 int array happens
if (enoughCredits()){
creditBalance -= 1;
for (int i =0; i<dice.size(); i++){
while (dice.get(i).getValue() != values[i]){
dice.get(i).roll();
}
}
}
}
/* Win conditions */
public void checkTriplets(){
if ( dice.get(0).compareTo(dice.get(1)) == 0 &&
dice.get(0).compareTo(dice.get(2)) == 0){
creditBalance +=1;
}
}
public void checkDoubles(){
if ( dice.get(0).compareTo(dice.get(1)) == 0 ||
dice.get(0).compareTo(dice.get(2)) == 0 ||
dice.get(1).compareTo(dice.get(2)) == 0){
creditBalance +=1;
}
}
public void checkLarge(){
if (getTotal() >= 10){
creditBalance +=1;
}
}
}