forked from dharmanshu1921/Java
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
Latest commit
170 lines (150 loc) · 5.06 KB
/
Copy pathApp.java
File metadata and controls
170 lines (150 loc) · 5.06 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
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.Random;
publicclassApp {
publicstaticvoidmain(String[] args) throwsException {
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
System.out.println("=====WELCOME TO SNAKE LADDER PROBLEM=====");
System.out.println();
System.out.println("1. Play with friends");
System.out.println("2. Play with computer");
System.out.println();
System.out.println("q. To exit");
System.out.println();
System.out.print("Enter leading option character to choose: ");
charquery=br.readLine().charAt(0);
Matchmatch=newMatch();
switch (query) {
case'1': System.out.println();
System.out.print("Total players: ");
intn=Integer.parseInt(br.readLine());
for (inti=0; i<n; i++) {
System.out.print("Enter player "+(i+1)+" name: ");
match.addPlayer(br.readLine());
}
match.start();
break;
case'2': System.out.println();
System.out.print("Player name: ");
match.addPlayer(br.readLine());
match.addPlayer("Computer");
match.start();
break;
case'q': System.out.println("Exiting...");
break;
default: System.out.println("Invalid option");
System.out.println("Exiting...");
break;
}
System.out.println("Solved by CRYP73R");
}
}
classMatch {
privateArrayList<Player> players=newArrayList<Player>();
Machinemachine;
Match() {
machine=newMachine();
}
voidaddPlayer(Stringname) {
players.add(newPlayer(name));
}
voidstart() {
while (true) {
for (Playerplayer : players) {
intdieValue=machine.rollDie();
intcurrentPosition=player.getPosition();
System.out.println(player.getName()+" rolled die, got: "+dieValue);
currentPosition=machine.movePlayer(currentPosition, dieValue);
if (machine.isLadder(currentPosition)) {
currentPosition=machine.moveLadder(currentPosition);
} elseif (machine.isSnakeBite(currentPosition)) {
currentPosition=machine.moveSnakeBite(currentPosition);
}
player.setPosition(currentPosition);
if (player.getPosition()==100) {
winner(player);
return;
}
}
System.out.println();
for (Playerplayer : players) {
System.out.println(player.getName()+" is at: "+player.getPosition());
}
System.out.println();
System.out.println();
}
}
privatevoidwinner(Playerplayer) {
System.out.println();
System.out.println();
System.out.println(player.getName()+" won the match.");
System.out.println();
}
}
classMachine {
privateHashMap<Integer, Integer> ladder=newHashMap<Integer, Integer>();
privateHashMap<Integer, Integer> snake=newHashMap<Integer, Integer>();
Machine() {
// ladder
ladder.put(9, 50);
ladder.put(13, 33);
ladder.put(20, 58);
ladder.put(27, 46);
ladder.put(36, 64);
ladder.put(48, 92);
ladder.put(60, 81);
ladder.put(63, 97);
ladder.put(66, 86);
// snake bite
snake.put(23, 3);
snake.put(37, 5);
snake.put(52, 7);
snake.put(55, 34);
snake.put(79, 43);
snake.put(85, 45);
snake.put(89, 51);
snake.put(95, 73);
snake.put(98, 25);
}
booleanisLadder(intcurrentPosition) {
returnladder.containsKey(currentPosition);
}
booleanisSnakeBite(intcurrentPosition) {
returnsnake.containsKey(currentPosition);
}
intmoveLadder(intcurrentPosition) {
returnladder.get(currentPosition);
}
intmoveSnakeBite(intcurrentPosition) {
returnsnake.get(currentPosition);
}
intmovePlayer(intcurrentPosition, intdieValue) {
if ((currentPosition+dieValue)<=100) {
return (currentPosition+dieValue);
}
returncurrentPosition;
}
introllDie() {
Randomrandom=newRandom();
return (int) random.ints(1, 1, 7).toArray()[0];
}
}
classPlayer {
privateStringname;
privateintcurrentPosition;
Player(Stringname) {
this.name=name;
currentPosition=0;
}
voidsetPosition(intnewPosition) {
currentPosition=newPosition;
}
intgetPosition() {
returncurrentPosition;
}
StringgetName() {
returnname;
}
}