Reputation: 930
I have the following method in my YahtzeeHand class:
int faceValue(int n){
int count = 0;
for(Die die : dice){
if(die.getCurrentValue() == n){
count++;
}
}
return n * count;
}
dice is an ArrayList of Die objects and .getCurrentValue gets the value of the die.
I am trying to determine how many Die in the the dice ArrayList have a value of n and multiply that number (count) by n.
System.out.printf("%s",faceValue(1));
The method keeps returning 0 regardless of whether there is a Die object with the value of 1 or not. Do you have any suggestions as to what I am doing wrong here and how I can get it to return "n * count"?
EDIT: To clarify things here are my three classes:
DieTester.java
package com.company;
public class DieTester {
public static void main(String[] args){
YahtzeeHand report = new YahtzeeHand();
report.reportLine();
}
}
YahtzeeHand.java
package com.company;
import java.util.ArrayList;
import java.util.Collections;
public class YahtzeeHand {
public int numDice, numSides, occurrences;
private int sum = 0;
private ArrayList<Die> dice = new ArrayList<>();
YahtzeeHand(){
numDice = 5;
numSides = 6;
}
YahtzeeHand(int numDice){
this.numDice = numDice;
}
YahtzeeHand(int numDice, int numSides){
this.numDice = numDice;
this.numSides = numSides;
}
void rollDice(){
for (Die die : dice){
die.roll();
}
}
public String toString() {
String result = "";
for (Die die : dice) {
result = result + " " + die;
}
return result;
}
int countDice(){
return numDice;
}
void setDice(Die firstDice, Die secondDice, Die thirdDice, Die fourthDice, Die fifthDice){
dice.add(firstDice);
dice.add(secondDice);
dice.add(thirdDice);
dice.add(fourthDice);
dice.add(fifthDice);
}
int faceValue(int n){
int count = 0;
for(Die die : dice){
if(die.getCurrentValue() == n){
count++;
}
}
return n * count;
}
int threeKindValue() {
boolean threeKind = false;
ArrayList<Integer> listOfValues = new ArrayList<>();
for (Die die : dice) {
listOfValues.add(die.getCurrentValue());
}
for (Integer num : listOfValues) {
if (Collections.frequency(listOfValues, num)>2 ) threeKind = true;
}
if (threeKind){
for(Integer num2 : listOfValues){
sum += num2;
}
return sum;
} else return 0;
}
int fourKindValue() {
boolean fourKind = false;
ArrayList<Integer> listOfValues = new ArrayList<>();
for (Die die : dice) {
listOfValues.add(die.getCurrentValue());
}
for (Integer num : listOfValues) {
if (Collections.frequency(listOfValues, num)>3 ) fourKind = true;
}
if (fourKind){
for(Integer num2 : listOfValues){
sum += num2;
}
return sum;
} else return 0;
}
int fullHouseValue(){
boolean fullHouseThree = false;
boolean fullHouseTwo = false;
ArrayList<Integer> listOfValues = new ArrayList<>();
for (Die die : dice) {
listOfValues.add(die.getCurrentValue());
}
for (Integer num : listOfValues) {
if (Collections.frequency(listOfValues, num) == 3) fullHouseThree = true;
}
for (Integer num : listOfValues) {
if (Collections.frequency(listOfValues, num) == 2) fullHouseTwo = true;
}
if (fullHouseThree && fullHouseTwo) return 25;
else return 0;
}
int largeStraightValue(){
ArrayList<Integer> listOfValues = new ArrayList<>();
for (Die die : dice) {
listOfValues.add(die.getCurrentValue());
}
for (int i = 0; i < listOfValues.size() - 1; i++) {
if (listOfValues.get(i) != listOfValues.get(i + 1) - 1) {
return 40;
}
}
return 0;
}
int yahtzeeValue(){
boolean yahtzee = false;
ArrayList<Integer> listOfValues = new ArrayList<>();
for (Die die : dice) {
listOfValues.add(die.getCurrentValue());
}
for (Integer num : listOfValues) {
if (Collections.frequency(listOfValues, num) == 5 ) yahtzee = true;
}
if (yahtzee){ return 50; } else return 0;
}
int chanceValue(){
ArrayList<Integer> listOfValues = new ArrayList<>();
for (Die die : dice) {
listOfValues.add(die.getCurrentValue());
}
for (Integer num : listOfValues) {
sum += num;
}
return sum;
}
void reportLine (int lineNum){
YahtzeeHand hand = new YahtzeeHand();
Die die1 = new Die(hand.numSides);
Die die2 = new Die(hand.numSides);
Die die3 = new Die(hand.numSides);
Die die4 = new Die(hand.numSides);
Die die5 = new Die(hand.numSides);
hand.setDice(die1, die2, die3, die4, die5);
hand.rollDice();
System.out.printf("%s. %s %s", lineNum, hand.toString(), faceValue(1));
}
}
Die.java
import java.util.*;
public class Die {
private int numSides, roll;
private Random random = new Random();
Die(int numSides) { this.numSides = numSides; }
public Die() { this(6); }
public int getCurrentValue() { return roll; }
public String toString() { return roll + " "; }
int roll() {
roll = random.nextInt(numSides) + 1;
return roll;
}
public void cheat(int cheater) {
if (cheater < 0 ) System.out.println("Can't be negative");
else this.roll = cheater;
}
public void reallycheat(int reallyCheat) {
this.roll = reallyCheat;
System.out.println("Stop trying to cheat so much!");
}
}
Upvotes: 1
Views: 253
Reputation: 393781
Your problem is that you have two YahtzeeHand
instances.
The first is created in your main
:
public static void main(String[] args){
YahtzeeHand report = new YahtzeeHand();
report.reportLine();
}
However, in reportLine
, you create a second instance:
void reportLine (int lineNum)
{
YahtzeeHand hand = new YahtzeeHand(); // second instance created
Die die1 = new Die(hand.numSides);
Die die2 = new Die(hand.numSides);
Die die3 = new Die(hand.numSides);
Die die4 = new Die(hand.numSides);
Die die5 = new Die(hand.numSides);
hand.setDice(die1, die2, die3, die4, die5); // dice added to second instance
hand.rollDice();
System.out.printf("%s. %s %s", lineNum, hand.toString(), faceValue(1)); // faceValue(1) called for original instance
}
You call setDice
for the second instance (hand.setDice(die1, die2, die3, die4, die5)
), but call faceValue(1)
for the original instance, which has no dice.
Just eliminate the second instance:
void reportLine (int lineNum)
{
Die die1 = new Die(numSides);
Die die2 = new Die(numSides);
Die die3 = new Die(numSides);
Die die4 = new Die(numSides);
Die die5 = new Die(numSides);
setDice(die1, die2, die3, die4, die5);
rollDice();
System.out.printf("%s. %s %s", lineNum, toString(), faceValue(1));
}
Upvotes: 3
Reputation: 109547
YahtzeeHand(int numDice)
will not set numSides, hence rolls will have an upper limit of 0.
One way to prevent this kind of out-of-date constructors is to call one single full constructor:
YahtzeeHand() {
this(5, 6);
}
YahtzeeHand(int numDice) {
this(numDice, 6);
}
YahtzeeHand(int numDice, int numSides) {
this.numDice = numDice;
this.numSides = numSides;
}
Upvotes: 3