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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <cstdlib>
#include <iostream>

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.

1
2
3
4
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.