-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLadderAndSnake.java
More file actions
187 lines (168 loc) · 4.91 KB
/
LadderAndSnake.java
File metadata and controls
187 lines (168 loc) · 4.91 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
/**
* @author layana muhdi al tounsi
* Comp249 (PP)
* ID 40125569
* assignment 1
* Part 1
* DUE DATE February 8th 2020
*
*/
import java.util.Random;
/**
* the class LadderAndSnake contains all the necessary methods and variables that will help the driver class run smoothly
*/
public class LadderAndSnake {
private int players;
/**
*
* @param ThePlayers
* parametarized constructor for the number of players
*/
public LadderAndSnake (int ThePlayers) {
this.players= ThePlayers;
}
/**
* building the board using arrays and displaying each block that has a ladder
* showing where the player must end .
*/
public void play() {
int[] board = new int [101];
board[1]=38; // ladder on block will end on block 38
board[4]= 14;// ladder on block 4 will end on block 14
board[9]=31;// ladder on block 9 will end on block 31
board[21]=42;// ladder on block 21 will end on block 42
board[28]=84;// ladder on block 28 will end on block 84
board[36]=44;// ladder on block 36 will end on block 44
board[51]=67;// ladder on block 51 will end on block 67
board[71]=91;// ladder on block 71 will end on block 91
board[80]=100;// ladder on block 80 will end on block 100 which is the last block
//snakes :
board[16]=6;
board[48]=30;
board[64]=60;
board[79]=19;
board[93]=68;
board[95]=24;
board[97]=76;
board[98]=78;
playerss[] myplayers = new playerss[players];
for (int i = 0; i < players; i++) {
myplayers[i] = new playerss(i + 1);
}
int[] dice_throws = new int[players];
boolean already_found = false; // to make sure that no two players have the same first roll
for (int i = 0; i < players;) {
int temp = flipDice();
for (int j = i+1; j < players; j++) {
if (temp == dice_throws[j])
already_found = true;
}
if (already_found)
continue;
else {
dice_throws[i] = temp;
myplayers[i].setPosition(temp);
/**
* the setPosition method will set a temporary position for the player each time the dice is thrown
*/
i++;
}
}
int rank = 1;
for (int j = 6; j >= 1; j--) {
for (int i = 0; i < players; i++) {
if (dice_throws[i] == j) {
System.out.println("Player "+(i+1)+" got a dice value of "+j);
myplayers[i].setOrder(rank++);
}
}
}
System.out.println("Reached final decision on order");
for (int i=4;i>=1;i--) {
for (int j=0;j<players;j++) {
if (myplayers[j].getOrder()==i) {
System.out.print("Player "+(j+1));
}
}
}
System.out.println();
for (int i=0;i<players;i++) {
myplayers[i].setPosition(0);
}
boolean someone_won = false;
while (!someone_won) {
for (int i = 0; i < players; i++) {
int dice_roll=flipDice();
int current_location=myplayers[i].getPosition()+dice_roll;
myplayers[i].setPosition(current_location);
if (current_location<=100) {
System.out.println("Player "+(i+1)+" rolled a "+dice_roll+", and is now in location "+myplayers[i].getPosition());
int new_location=board[current_location];
if (new_location!=current_location&&new_location!=0) {
myplayers[i].setPosition(new_location);
System.out.println("Now he/she had moved to the new position "+myplayers[i].getPosition());
}
}
if (myplayers[i].getPosition()>=100||current_location>=100) {
System.out.println("Game has ended\n Congratulations Player "+(i+1));
/**
* program will end and a winner will be displayed
*/
System.exit(0);
}
}
System.out.println();
}
}
/**
* method flipDice() will act like a dice and will give the user a random number from 1-6
* @return a random number of type integer from 1-6
*/
public int flipDice() {
Random random = new Random();
return (random.nextInt(6 - 1 + 1) + 1);
}
class playerss {
/**
* the class players will set all the different characteristics for the player
* taking into consideration their names if asked , position, and order
* @param posotion will set the position of the player on the board
* @param order will determine who goes first in order of players
*/
private int name;
private int position;
private int order;
public playerss(int name) {
this.name=name;
}
/**
* method setPosition will set for each player an initial position
* @param p the initial player position
*/
public void setPosition(int p) {
this.position=p;
}
/**
* method getPosition give us what position each player is at
* @return returns the position we set in the setvalue method
*/
public int getPosition() {
return this.position;
}
/**
*
* method getOrder gives player order
* @return returns the order in which each player will go
*/
public int getOrder() {
return this.order;
}
/**
* SetOrder method will set this order for the players each time
* @param t is the order that the program will assign to the players
*/
public void setOrder(int t) {
this.order=t;
}
}
}