I'm trying to simulate a game of Yahtzee by using various methods to roll, compare, and sum up dice.
This is an assignment and I'm required to use these three static methods to do all of my work in.
When I try to run my code however I get this error
exit status 1
Main.java:6: error: non-static method rollDice(int[]) cannot be referenced from a static context
rollDice(dice);
^
Main.java:7: error: non-static method rollDice(int[]) cannot be referenced from a static context
dice= rollDice(dice);
^
2 errors
I tried running the methods before assigning the values they return to variables but that didn't seem to work. Here is my complete code:
class Main {
public static void main(String[] args) {
// int array to hold 5 dice
int dice[] = new int[5];
rollDice(dice);
dice= rollDice(dice);
fiveOfaKind(dice);
int points = fiveOfaKind(dice);
getChance(dice,points);
int printpoint = getChance(dice,points);
// You may roll the dice up to 3 times to try to get 5 of a Kind
// If you get 5 of a Kind, stop if not keep trying.
// If you do not get 5 of a Kind after the 3rd roll, you must take the
// CHANCE score.
System.out.println("Your score is " + printpoint);
}// end of main method
public int[] rollDice(int dice[]){
// generate 5 random numbers / update dice array
for (int i = 0; i < dice.length; i++) {
dice[i] = (int)(Math.random() * 6 + 1);
}
return dice;
}// end of rollDice - rolls 5 dice and puts them in an array
// All 5 dice must be rolled each time
public static int fiveOfaKind(int dice[]){
int pts = 0;
int rNum=0;
for(int i=0; rNum<3; rNum++){
if(dice[0]==dice[1]&&dice[1]==dice[2]&&dice[2]==dice[3]&&dice[3]==dice[4]&&dice[4]==dice[5]&&dice[5]==dice[6]){
pts=50;
}
else if(rNum==3){
pts=0;
}
else{
int[] rollDice;
}
}//end of forloop
// use Array dice - evaluate if all elements are equal
// if true = pts = 50
// if false - pts = 0
return pts;
}// end of fiveOfaKind - evaluates the dice roll to see if all 5 are equal
// Returns 50 points if they do
public static int getChance(int dice[], int points){
if(points<50){
points=0;
for (int i = 0; i < dice.length; i++) {
points+=dice[i];
}
}
// use Array dice - sum the elements of the Array
// pys = calculated value of the sum
int p = points;
return p;
}// end of getChance - adds the total value of the dice
// Returns the total points calculated
}// end of class