-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiceGame.java
More file actions
163 lines (140 loc) · 4.43 KB
/
diceGame.java
File metadata and controls
163 lines (140 loc) · 4.43 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
package diceGame;
import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;
public class diceGame {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Welcome to Dice Game!");
String test = "q";
// Condition whether to start the game or no
System.out.println("Enter any key to Start or q to Quit: ");
Scanner keyboard = new Scanner(System.in);
String x = keyboard.nextLine();
boolean tried = x.equals(test);
while (tried != true) {
int players;
try {
// This code will ask how many players
Scanner chooseHowManyPlayers = new Scanner(System.in);
System.out.println("Choose up to 6 players: ");
players = chooseHowManyPlayers.nextInt();
if (players <= 6) {
// Ask a name for each player
System.out.println("Welcome!");
// initialize the arrays for players and number of dice got
String nameOfPlayers[] = new String[6];
int numberGotfromRoll[] = new int[6];
// for loop to ask each player a name
for (int i = 0; i < players; i++) {
int y = i + 1;
y = y++;
Scanner askPlayerName = new Scanner(System.in);
String playerNumber = "P" + y;
System.out.println("Enter Your Name " + playerNumber + ": ");
String playerName = askPlayerName.nextLine();
nameOfPlayers[i] = playerName;
}
// Ask the player to roll the dice
String roll = "no";
boolean toss = true;
Scanner askToRoll = new Scanner(System.in);
System.out.println("Enter 'no' to go back: ");
String ask = askToRoll.nextLine();
boolean flag = ask.equals(roll);
if (toss != flag) {
// Roll the dice.
for (int i = 0; i < players; i++) {
System.out.println("\n Rolling the dice......");
String player = nameOfPlayers[i];
int result = Dice.rollDice(1, 6);
numberGotfromRoll[i] = result;
System.out.println(nameOfPlayers[i] + " got : " + numberGotfromRoll[i]);
/* This will end the recursion if the player got 6 */
if (result == 6) {
int max = i;
String winners = nameOfPlayers[max];
System.out.println("Winner is " + winners + "\n");
break;
} else {
continue;
}
}
// Determine the winner
int highestNum = numberGotfromRoll[0];
int count = 1;
for (int j : numberGotfromRoll) {
if (j > highestNum) {
count = count++;
highestNum = findIndex(numberGotfromRoll, j);
String winnerName = nameOfPlayers[highestNum];
}
}
System.out.println("\nWinner is " + nameOfPlayers[highestNum] + "\n");
keyboard = new Scanner(System.in);
System.out.println("Enter s to Start");
System.out.println("q to quit: ");
x = keyboard.nextLine();
tried = x.equals(test);
if (x == "q") {
tried = true;
System.exit(0);
}
} else {
tried = true;
}
} else {
keyboard = new Scanner(System.in);
System.out.println("Enter s to Start");
System.out.println("q to quit: ");
x = keyboard.nextLine();
tried = x.equals(test);
if (x == "q") {
tried = true;
System.exit(0);
}
}
}
// If the player did not enter a number this code will execute.
catch (Exception e) {
System.out.println("Please enter a number");
keyboard = new Scanner(System.in);
System.out.println("Enter s to Start");
System.out.println("q to quit: ");
x = keyboard.nextLine();
tried = x.equals(test); // flag to determine if the player enter q or not
if (x == "q") {
tried = true;
System.exit(0);
}
}
}
System.exit(0);
}
public static int index(int arr[], int t, int start) {
// base number
// start equals the length of the
// array we return -1
if (start == arr.length)
return -1;
// if element at index start equals t
// we return start
if (arr[start] == t)
return start;
// we find for the rest
// position in the array
return index(arr, t, start + 1);
}
public static int findIndex(int arr[], int t) {
return index(arr, t, 0);
}
public class Dice {
int min;
int max;
public static int rollDice(int min, int max) {
Random random = new Random();
int b = (int) (Math.random() * (max - min + 1) + min);
return b;
}
}
}