-
Notifications
You must be signed in to change notification settings - Fork 113
/
Snake And Ladder Game
56 lines (45 loc) · 1.74 KB
/
Snake And Ladder Game
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
import java.util.Random;
import java.util.Scanner;
public class SnakeAndLadder {
static int playerPosition = 0;
static int[][] snakes = { {27, 5}, {40, 3}, {43, 18}, {54, 31}, {66, 45}, {76, 58}, {89, 53}, {99, 41} };
static int[][] ladders = { {4, 25}, {13, 46}, {33, 49}, {42, 63}, {50, 69}, {62, 81}, {74, 92}, {82, 98} };
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
boolean gameWon = false;
while (!gameWon) {
System.out.println("Press Enter to roll the dice");
scanner.nextLine();
int dice = random.nextInt(6) + 1;
System.out.println("You rolled a " + dice);
playerPosition += dice;
if (playerPosition > 100) {
playerPosition -= dice;
} else {
playerPosition = checkSnakesAndLadders(playerPosition);
}
System.out.println("You are now on square " + playerPosition);
if (playerPosition == 100) {
gameWon = true;
System.out.println("Congratulations! You've won the game!");
}
}
scanner.close();
}
static int checkSnakesAndLadders(int position) {
for (int[] snake : snakes) {
if (position == snake[0]) {
System.out.println("Oops! Bitten by a snake. Slide down to " + snake[1]);
return snake[1];
}
}
for (int[] ladder : ladders) {
if (position == ladder[0]) {
System.out.println("Yay! Climbed a ladder. Move up to " + ladder[1]);
return ladder[1];
}
}
return position;
}
}