-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnakeLadder.java
154 lines (129 loc) · 3.67 KB
/
SnakeLadder.java
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
import java.util.*;
public class SnakeLadder {
//Initialize Start Position as Zero
private int position;
private int countResult;
private int winningPosition;
SnakeLadder() {
this.position = 0;
this.countResult = 0;
this.winningPosition = 100;
}
SnakeLadder(int winningPosition) {
this.winningPosition=winningPosition;
}
/**
* @return the countResult
*/
public int getCountResult() {
return countResult;
}
/**
* @param countResult the countResult to set
*/
public void setCountResult(int countResult) {
this.countResult = countResult;
}
//Function for rolling a dice
private int rollDice() {
Random rand = new Random();
//generating Random Number from 0 to 5 and adding 1 as LowerLimit
int resultDice = rand.nextInt(6) + 1;
return resultDice;
}
//function for playing game
public void play(String playerName) {
this.countResult += 1;
//Rolling a dice
int resultDice = this.rollDice();
this.setCountResult(this.getCountResult() + 1);
System.out.println("The number on dice is "+resultDice);
//Getting position for the die
int positonPlayer = this.getPosition();
//Calling checkOption function to generate Option
int optionResult = this.checkOption();
switch(optionResult){
//Ladder Position
case 1 :
System.out.println("You got a Ladder");
positonPlayer += resultDice;
if(positonPlayer > this.getWinningPosition()){
int numberNeed = this.getWinningPosition() - this.getPosition();
System.out.println("Sorry, You need " + numberNeed + " to Win");
positonPlayer -= resultDice;
}
if(positonPlayer == this.getWinningPosition()) {
this.setPosition(positonPlayer);
System.out.println("---------Winning Player-------------");
System.out.println(playerName);
System.out.println("Hurray You Win, You are at "+this.getPosition());
System.out.println("The total number of times dice is thrown is "+this.getCountResult());
System.exit(0);
}
this.setPosition(positonPlayer);
System.out.println("The position of the player is "+this.getPosition());
//If got ladder then calling play function
this.play(playerName);
break;
//Snake Position
case 2:
System.out.println("You got a Snake");
positonPlayer -= resultDice;
this.setPosition(positonPlayer);
System.out.println("The position of the player is "+this.getPosition());
break;
//No Play
default:
System.out.println("You got a NoPlay");
System.out.println("The position of the player is "+this.getPosition());
}
}
/**
* @return the winningPosition
*/
public int getWinningPosition() {
return winningPosition;
}
/**
* @return the position
*/
public int getPosition() {
return position;
}
/**
* @param position the position to set
*/
public void setPosition(int position) {
if(position < 0){
position = 0;
}
this.position = position;
}
/*
* 0- No Play
* 1-Ladder
* 2-Snake
*/
public int checkOption(){
Random rand = new Random();
int option = rand.nextInt(3);
return option;
}
public static void main(String[] args) throws InterruptedException {
//Creating object for SnakeLadder Class
int numberOfPlayers=2;
ArrayList<SnakeLadder> playerList = new ArrayList<SnakeLadder>(numberOfPlayers);
for(int i = 0; i < numberOfPlayers; i++) {
SnakeLadder ObjSnakeLadder = new SnakeLadder();
playerList.add(ObjSnakeLadder);
}
while(true) {
for(int i = 0; i < playerList.size(); i++) {
int playerNumber = i + 1;
System.out.println("Game Started for Player "+playerNumber);
System.out.println();
playerList.get(i).play("Player "+playerNumber);
}
}
}
}