forked from hacktoberfest17/programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDice.java
More file actions
84 lines (69 loc) · 1.55 KB
/
Dice.java
File metadata and controls
84 lines (69 loc) · 1.55 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
/**
* Dice class used in the yahtzee game. It can display the number rolled
* and have n number of sides.
* @author Felipe Scrochio Custódio
*/
public class Dice {
private int sides;
private int result;
/**
* Standard 6 sides dice constructor
*/
public Dice() {
sides = 6;
}
/**
* n number of sides dice constructor
* @param n Number of sides in the dice
*/
public Dice(int n) {
sides = n;
}
/**
* Gets the last rolled result of this dice
* @return Last dice result
*/
public int getResult() {
return result;
}
/**
* Rolls the dice
* @return Number from 1 to n sides.
*/
public int roll() {
Random r = new Random();
result = Math.abs((r.getIntRand(sides)));
result++; /* change min from 0 to 1 */
return result;
}
/**
* Prints the last result of the dice
*/
public void display() {
String d;
if (sides == 6) {
switch(result) {
case 1:
d = "+-----+\n| |\n| * |\n| |\n+-----+";
System.out.print(d);
case 2:
d = "+-----+\n| *|\n| |\n|* |\n+-----+";
System.out.print(d);
case 3:
d = "+-----+\n| *|\n| * |\n|* |\n+-----+";
System.out.print(d);
case 4:
d = "+-----+\n|* *|\n| |\n|* *|\n+-----+";
System.out.print(d);
case 5:
d = "+-----+\n|* *|\n| * |\n|* *|\n+-----+";
System.out.print(d);
case 6:
d = "+-----+\n|* *|\n|* *|\n|* *|\n+-----+";
System.out.print(d);
}
} else {
System.out.println("This type of dice can't be printed.");
}
}
}