### GAME GLITCH

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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213`` ``````#include #include using namespace std; int main(int argc, char *argv[]) { class dice { int[] diceRound = new int[3]; // Holds the random numbers int[] values = new int[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 */ public int 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 */ public int 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". */ public void 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 } else if (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; } else if (answer.length() == 1 && answer.charAt(0)=='y') { counter++; largest = 0; UserRollsAgain = false; } else if (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 */ public static void 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; } else if (answer.length() == 1 && answer.charAt(0)=='n') { loopGame = true; } else { loopGame = true; } } } } ``````

i'm a little confused, I see some java mixed in with c++ . These statements on line 119, 120 and 121 along with others look like java to me.

 ``1234`` ``````System.out.print("\nKeep "+largest+" or roll again? y or n: "); Scanner in = new Scanner(System.in); answer = in.nextLine(); ``````
Last edited on
Topic archived. No new replies allowed.