forked from hacktoberfest17/programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRollDice.java
More file actions
194 lines (174 loc) · 4.18 KB
/
RollDice.java
File metadata and controls
194 lines (174 loc) · 4.18 KB
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
/**
* Handles the dice rolls for the yahtzee game
* Adapted code from university assignment
* @author Felipe Scrochio Custodio, Gabriel Scalici
*/
public class RollDice {
Dice[] dices;
/**
* Creates and initializes several Dice objects
* @param n Number of dices
*/
public RollDice(int n) {
dices = new Dice[n];
for (int i = 0; i < n; i++) {
dices[i] = new Dice();
}
}
/**
* Rolls all dices.
* @return Returns the result of every dice in the array
*/
public int[] roll() {
int[] results = new int[dices.length];
/* Number de results = Number of dices */
for (int i = 0; i < dices.length; i++) results[i] = dices[i].roll();
return results;
}
/**
* Rolls some of the dices.
* @param whichDices - Array of booleans that indicates which dices to roll
* Each position represents a dice in the dices array
* @return Returns the result of every dice in the array
*/
public int[] roll(boolean[] whichDices) {
int[] results = new int[dices.length];
for (int i = 0; i < dices.length; i++) {
if (whichDices[i]) {
results[i] = dices[i].roll();
} else {
results[i] = dices[i].getResult();
}
}
return results;
}
/**
* Rolls some of the dices.
* @param s - String with the dices indexes to roll.
* @return Returns the result of every dice in the array
*/
public int[] roll(java.lang.String s) {
int i, j, k;
/* converts indexes in string to int */
String aux[] = s.trim().split(" ");
int indices[] = new int[aux.length];
for (i = 0; i < aux.length; i++) {
indices[i] = Integer.parseInt(String.valueOf(aux[i]));
}
/* rolls dices */
int[] results = new int[dices.length];
k = 0;
j = indices[k] - 1;
for (i = 0; i < dices.length; i++) {
/* verifies if I'm supposed to roll this dice */
if (i == j) {
results[i] = dices[i].roll();
k++;
if (k < indices.length) {
j = indices[k] - 1;
}
} else {
results[i] = dices[i].getResult();
}
}
return results;
}
/**
* Shows all dices horizontally in the screen
*/
public void display() {
int aux = 0, cont = 0;
int i = 0, j=0, k=0;
String[] d1 = new String[5];
String[] d2 = new String[5];
String[] d3 = new String[5];
String[] d4 = new String[5];
String[] d5 = new String[5];
String[] d6 = new String[5];
String[][] mat = new String[5][35];
/* Dice 1 */
d1[0] = "+-----+";
d1[1] = "| |";
d1[2] = "| * |";
d1[3] = "| |";
d1[4] = "+-----+";
/* Dice 2 */
d2[0] = "+-----+";
d2[1] = "| *|";
d2[2] = "| |";
d2[3] = "|* |";
d2[4] = "+-----+";
/* Dice 3 */
d3[0] = "+-----+";
d3[1] = "| *|";
d3[2] = "| * |";
d3[3] = "|* |";
d3[4] = "+-----+";
/* Dice 4 */
d4[0] = "+-----+";
d4[1] = "|* *|";
d4[2] = "| |";
d4[3] = "|* *|";
d4[4] = "+-----+";
/* Dice 5 */
d5[0] = "+-----+";
d5[1] = "|* *|";
d5[2] = "| * |";
d5[3] = "|* *|";
d5[4] = "+-----+";
/* Dice 6 */
d6[0] = "+-----+";
d6[1] = "|* *|";
d6[2] = "|* *|";
d6[3] = "|* *|";
d6[4] = "+-----+";
for(i = 0; i < 5; i++){
aux = dices[i].getResult();
switch(aux){
case 1:
/* j controls line flow */
for(j = 0; j < 5; j++){
/* k iterates the matrix */
mat[j][k] = d1[j];
}
/* get the other dices */
k++;
break;
case 2:
for(j = 0; j < 5; j++){
mat[j][k] = d2[j];
}
k++;
break;
case 3:
for(j = 0; j < 5; j++){
mat[j][k] = d3[j];
}
k++;
break;
case 4:
for(j = 0; j < 5; j++){
mat[j][k] = d4[j];
}
k++;
break;
case 5:
for(j = 0; j < 5; j++){
mat[j][k] = d5[j];
}
k++;
break;
case 6:
for(j = 0; j < 5; j++){
mat[j][k] = d6[j];
}
k++;
break;
}
}
/* print the matrix */
for(i = 0; i < 5; i++){
System.out.print(mat[i][0] +"\t"+ mat[i][1] +"\t"+ mat[i][2] +"\t"+ mat[i][3] +"\t"+ mat[i][4] +"\t\n");
}
}
}