-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriverSnakeLadder.java
More file actions
195 lines (181 loc) · 5.3 KB
/
DriverSnakeLadder.java
File metadata and controls
195 lines (181 loc) · 5.3 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
195
import java.util.Scanner;
/**
*
* @author layana muhdi al tounsi
* Comp249 (PP)
* ID 40125569
* assignment 1
* Part 2
* DUE DATE February 8th 2020
*
*
*//**
* the class DriverSnakeLadder is the class where
* the game will come to life and he user will be prompted to input the number of players
*
*/
public class DriverSnakeLadder {
/**
* constructor DriverSnakeLadder is a default contrsuctor with no parameters
*/
public DriverSnakeLadder() {
}
/**
*
* @param args
* prompting the user to enter the number of players
*/
public static void main(String[] args)
{
Scanner kb=new Scanner(System.in);
System.out.println("--------------------------------------------------");
System.out.println("------------Snake and Laddeers, the game.---------");
System.out.println("--------------------------------------------------");
System.out.println("Hello and welcome to the game of Snakes and Ladders!");
System.out.println("How many players are playing?( Allowed number of player is 2-4 inclusively )");
int attempts = 0;
int players=kb.nextInt();
while ((players > 4 || players <= 1) && attempts < 5)
{
if(attempts == 3)
{
System.out.println("Tries exceeded, goodbye!");
/**
* the number of players cannot exceed 4 or be less than 2 , if this happens the
* players are asked three more times to adjust their numbers
* when the attempts are all used and the users have not adjusted their number
* the program will terminate completely using the exit method
*/
System.exit(0);
}
else
{
System.out.println(" INVALID - PLEASE TRY AGAIN - How many players are playing ? ");
System.out.println("Remaining attempts:"+(3-attempts));
players = kb.nextInt();
attempts++;
}
}
int [] order = new int [players];
int [] dice = new int [players];
for (int i = 0; i < players; i++)
{
dice[i] = flipDice();
order[i] = i+1;
System.out.println((i+1)+" player has rolled: "+dice[i]);
}
/**
* initiative method to compare the order in which the players went originally
* @param players is the number of players playing the game
* @param dice refers to the value of the dice 1 to 6 first time
* @param order is the order in which they played
*/
initiative (players, dice, order);
reRoll (players,dice,order);
LadderAndSnake X = new LadderAndSnake ( players );
X.play();
kb.close();
}
/**
* method flipDice() will act like a dice and will give the user a random number from 1-6
* @return a random number which is diceValue of type integer from 1-6
* diceValue which is initialized o zero is the value that the generator will later assign
* to the player's dice
*/
public static int flipDice()
{
int diceValue = 0;
diceValue = (int)(Math.random()*6 )+1;
return diceValue;
}
/**
* method swap ; it will swap the players at specified positions
* @param A1 specific array that is used to swap
* @param p1 p1 will be swapped with p2
* @param p2 p2 will be swapped with p1
*/
public static void swap (int[] A1, int p1, int p2)
{
int temp = A1[p1];
A1[p1] = A1[p2];
A1[p2] = temp;
}
/**
* initiative method to compare the order in which the players went originally
* @param players is the number of players playing the game
* @param dice refers to the value of the dice 1 to 6 first time
* @param order is the order in which they played
*/
public static void initiative (int players, int[] dice, int [] order)
{
for (int i = 0; i < players; i++)
{
int largest = i;
for (int x = i+1; x < players; x++)
{
if(dice[largest] < dice[x])
{
largest = x;
}
}
swap(dice, i, largest);
swap(order, i, largest);
}
}
/**
*
* @param players # of players
* @param dice array for the repeated dice value
* @param order array for the repeated player order
* method reRoll will take action when we have a repeated dice values and will help solve this tie
*/
public static void reRoll(int players, int[] dice, int [] order)
{
int [] aRepeat;
int [] aRoll;
for (int i = 0; i < players-1; i++)
{
int repeated = 0;
for (int x = i+1; x < players ; x++)
{
if (dice [i] == dice [x])
{
repeated++;
}
else
{
break;
}
}
if (repeated > 0)
{
aRepeat = new int [repeated+1];
aRoll = new int [repeated+1];
System.out.print(" WARNING : WE HAVE A TIE -Player ");
for (int j = 0; j < aRepeat.length; j++)
{
if(j == repeated)
{
System.out.print("is tied with "+ order[i+j] + "!");
}
if(j < repeated)
{
System.out.print("is tied with " + order[i+j] + " ");
}
aRoll[j] = flipDice();
aRepeat[j] = order[j+i];
}
System.out.println(" Breaking the tie :) ");
for (int j = 0; j < aRepeat.length; j++)
{
System.out.println("player " +aRepeat[j]+ " rolled: " + aRoll[j]);
}
initiative (repeated+1, aRoll, aRepeat);
reRoll(repeated+1, aRoll, aRepeat);
for(int j = 0; j < aRepeat.length; j++)
{
order[i+j] = aRepeat[j];
}
}
}}
}