Ok, so i had some help with this dice game but it is not compling and I would also like some assistance in rewriting it without the arrays in a more of a beginners terminology. Thanks in advance.
#include <cstdlib>
#include <iostream>
usingnamespace std;
int main(int argc, char *argv[])
{
class dice
{
int[] diceRound = newint[3]; // Holds the random numbers
int[] values = newint[3]; // Holds the highest values
/** highestValue
* Takes the values array, and adds together to return the score of the game.
* @return: returns sum of highest values */
publicint highestValue() {
int sum = 0;
for (int i = 0;i < values.length;i++) {
sum = ((sum + values[i]));
}
return sum;
}
/** diceNumber
* Generates a random number
*@return: returns the randomNumber */
publicint diceNumber()
{
int realNum = (int) Math.floor(Math.random() * 6) + 1;
return realNum;
}
/** rollDice
*This methid plays the game, it rolls the dice once with 3 random numbers, gives user option
*to keep the highest or roll again, maximum 2 trys, then does this all the way down to one dice
*
*Assigns highest values to array "values".
*/
publicvoid rollDice()
{
dice generate = new dice();
int largest = Integer.MIN_VALUE;
boolean UserRollsAgain = false;
int length = diceRound.length;
int moveRolls = 1;
int counter = 0;
String answer = " ";
while (moveRolls <= 3) {// Allows user to play the game 3 times
UserRollsAgain = false;
if (moveRolls == 2) {
length = diceRound.length - 1; largest = 0;// If second roll then, two dice must be used
}
elseif (moveRolls == 3) {
length = diceRound.length - 2; largest = 0; // If third roll then, one dice.
}
// Loops program while counter < 2 and user rolls again
while (!UserRollsAgain) {
for (int i = 0;i < length;i++) {// Rolls dice and determines highest value
diceRound[i] = generate.diceNumber();// and assigns it to the array values.
System.out.print(diceRound[i]+" | ");
if (diceRound[i] > largest) {
largest = diceRound[i];
}
values[i] = largest;
}
// If statements, determines whether uses can roll again or not.
if (counter < 2) {
System.out.print("\nKeep "+largest+" or roll again? y or n: ");
Scanner in = new Scanner(System.in);
answer = in.nextLine();
}
if (counter == 2) {
System.out.println ("\nYour Number is "+largest+" You Have No more rolls\n");
UserRollsAgain = true;
moveRolls++;
counter = 0;
}
elseif (answer.length() == 1 && answer.charAt(0)=='y') {
counter++;
largest = 0;
UserRollsAgain = false;
}
elseif (answer.length() == 1 && answer.charAt(0)=='n') {
System.out.println ("\nYour Number is "+largest+"\n");
UserRollsAgain = true;
moveRolls++;
}
else { System.out.println ("Error"); UserRollsAgain = true; counter = 2;}
}
}
System.out.println(" ");
} // rollDice ends;
/** main method
*runs the game, and displays highest value.
*gives user option to play game again
*/
publicstaticvoid main(String[] args) {
dice game = new dice();
boolean loopGame = false;
while (!loopGame) {
game.rollDice();
System.out.println("Your Score is: "+game.highestValue()+"\n");
System.out.print("Would you like to play again y or n: ");
Scanner in = new Scanner(System.in);
String answer = in.nextLine();
if (answer.length() == 1 && answer.charAt(0)=='y') {
loopGame = false;
}
elseif (answer.length() == 1 && answer.charAt(0)=='n') {
loopGame = true;
}
else { loopGame = true; }
}
}
}