Repository navigation
Expand file tree
/
Copy pathPigDiceGame.java
More file actions
112 lines (98 loc) · 4.2 KB
/
Copy pathPigDiceGame.java
File metadata and controls
112 lines (98 loc) · 4.2 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
import java.util.Scanner;
import java.util.Random;
public class PigDiceGame {
private static int scoreOne = 0;
private static int scoreTwo = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
System.out.println(
"Welcome To Pig Dice! First Player to get 30 accumulative points wins! (Or however much you set it to)");
System.out.print("Please enter player one's username: ");
String nameOne = scanner.nextLine().trim();
System.out.print("Please enter player two's username: ");
String nameTwo = scanner.nextLine().trim();
System.out.print("Do you want to set a custom limit, 'Yes' or 'No' (Default limit is 30 points): ");
String limitAsk = scanner.nextLine().trim().toUpperCase();
int limit = 30;
if (limitAsk.equals("YES")) {
System.out.print("Please enter the score limit: ");
limit = scanner.nextInt();
}
int currentPlayer = 1;
boolean gameOver = false;
while (!gameOver) {
String currentPlayerName = (currentPlayer == 1) ? nameOne : nameTwo;
System.out.println(currentPlayerName + " is rolling...");
int roll = random.nextInt(6) + 1;
printDice(roll);
if (roll == 1) {
System.out.println("Oh no... You lose all your points");
if (currentPlayer == 1) {
scoreOne = 0;
} else {
scoreTwo = 0;
}
currentPlayer = (currentPlayer == 1) ? 2 : 1;
} else {
if (currentPlayer == 1) {
scoreOne += roll;
} else {
scoreTwo += roll;
}
System.out.println("Current Score");
System.out.println(nameOne + ": " + scoreOne + " | " + nameTwo + ": " + scoreTwo);
System.out.print("Roll again or hold? (R = Roll / H = Hold): ");
String choice = scanner.next().trim().toUpperCase();
if (choice.equals("H")) {
currentPlayer = (currentPlayer == 1) ? 2 : 1;
}
}
if (scoreOne >= limit || scoreTwo >= limit) {
gameOver = true;
}
}
String winnerName = (scoreOne >= limit) ? nameOne : nameTwo;
int winnerScore = (scoreOne >= limit) ? scoreOne : scoreTwo;
System.out.println("Winner!\n" + winnerName + " wins with a score of " + winnerScore);
}
public static void printDice(int roll) {
if (roll == 1) {
System.out.println("[-----]");
System.out.println("[ ]");
System.out.println("[ 0 ]");
System.out.println("[ ]");
System.out.println("[-----]");
} else if (roll == 2) {
System.out.println("[-----]");
System.out.println("[ 0 ]");
System.out.println("[ ]");
System.out.println("[ 0 ]");
System.out.println("[-----]");
} else if (roll == 3) {
System.out.println("[-----]");
System.out.println("[ ]");
System.out.println("[0 0 0]");
System.out.println("[ ]");
System.out.println("[-----]");
} else if (roll == 4) {
System.out.println("[-----]");
System.out.println("[0 0]");
System.out.println("[ ]");
System.out.println("[0 0]");
System.out.println("[-----]");
} else if (roll == 5) {
System.out.println("[-----]");
System.out.println("[0 0]");
System.out.println("[ 0 ]");
System.out.println("[0 0]");
System.out.println("[-----]");
} else if (roll == 6) {
System.out.println("[-----]");
System.out.println("[0 0 0]");
System.out.println("[ ]");
System.out.println("[0 0 0]");
System.out.println("[-----]");
}
}
}