forked from hacktoberfest17/programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYahtzee.java
More file actions
107 lines (103 loc) · 3.68 KB
/
Yahtzee.java
File metadata and controls
107 lines (103 loc) · 3.68 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
/**
* User interaction - Yahtzee game
* @author Felipe Custodio, Gabriel Scalici
*/
public class Yahtzee {
public Yahtzee() {
}
public static void main(java.lang.String[] args) throws java.io.IOException {
String toRoll;
int position;
int[] results = new int[5];
int playCounter;
int userInput;
int usedPosition;
int finalScore = 0;
RollDice dice = new RollDice(5);
Score score = new Score();
/* 10 rounds */
for (int i = 0; i < 10; i++) {
System.out.println("ROUND " + i+1);
System.out.print("Press ENTER to start! ");
Input.readString();
playCounter = 0;
System.out.println("Rolling dices...");
results = dice.roll();
System.out.println("Your results:");
/* show dices */
dice.display();
/* player has three attmepts to roll the dices */
while (playCounter < 3) {
System.out.println("Roll again or insert results in scoreboard?");
System.out.println("1) Roll 2) Insert in scoreboard");
userInput = Input.readInt();
if (userInput == 1) {
System.out.print("Pick the dices to be rolled again (separated by space) ");
toRoll = Input.readString();
results = dice.roll(toRoll);
System.out.println("Your results:");
/* show dices */
dice.display();
playCounter++;
} else {
if (userInput == 2) {
playCounter = 3;
}
}
}
/* add score to scoreboard */
usedPosition = 0;
position = 0;
while (usedPosition != 1) {
System.out.println("Pick the position to insert");
System.out.println("1 2 3 4 5 6 7 8 9 10");
position = Input.readInt();
switch (position) {
case 1:
position = 0;
break;
case 2:
position = 3;
break;
case 3:
position = 6;
break;
case 4:
position = 2;
break;
case 5:
position = 5;
break;
case 6:
position = 8;
break;
case 7:
position = 1;
break;
case 8:
position = 4;
break;
case 9:
position = 7;
break;
case 10:
position = 10;
break;
}
if (score.plays[position] == -1) {
score.add(position, results);
score.display();
usedPosition = 1;
} else {
System.out.println("You already used this position! Choose another.");
usedPosition = 0;
}
}
}
/* game over */
finalScore = score.getScore();
System.out.println("GAME OVER!");
score.display();
System.out.print("Final score: " + finalScore);
}
}