-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakesAndLadders.java
More file actions
132 lines (126 loc) · 5.91 KB
/
SnakesAndLadders.java
File metadata and controls
132 lines (126 loc) · 5.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
import java.util.Scanner;
import java.util.Random;
public class SnakesAndLadders{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
//Let players enter their names
System.out.print("Enter player 1 name: ");
String player1 = scanner.nextLine();
System.out.print("Enter player 2 name: ");
String player2 = scanner.nextLine();
//setting up variables for later use
int currentPlayerTurn = 1;
int player1_score = 0;
int player2_score = 0;
//while loop, so the program will be ticking
while(true){
//creating instance of random class and getting random number
int randomDice = new Random().nextInt(6) + 1;
if(currentPlayerTurn == 1){ //switching between player turns
currentPlayerTurn = 2;
player1_score += randomDice;
System.out.println(player1 + ", press enter to roll a dice: ");
scanner.nextLine();
System.out.println(player1 + ", your score is now " + player1_score);
}else{
currentPlayerTurn = 1;
player2_score += randomDice;
System.out.println(player2 + ", press enter to roll a dice: ");
scanner.nextLine();
System.out.println(player2 + ", your score is now " + player2_score);
}
//win
if(player1_score >= 25){
System.out.println(player1 + ", you won!");
break;
}else if(player2_score >= 25){
System.out.println(player2 + ", you won!");
break;
}
//putting both player in array so we can perform switch for each player without tonns of code
int players[] = {player1_score, player2_score};
int player = 1;
for(int playerScore : players){
//switching between different actions to do
switch(playerScore){
case 3:
if(player == 1){
player1_score = 22;
System.out.println(player1 + ", it is a ladder. Your score is now " + player1_score);
}else{
player2_score = 22;
System.out.println(player2 + ", it is a ladder. Your score is now " + player2_score);
}
break;
case 5:
if(player == 1){
player1_score = 8;
System.out.println(player1 + ", it is a ladder. Your score is now " + player1_score);
}else{
player2_score = 8;
System.out.println(player2 + ", it is a ladder. Your score is now " + player2_score);
}
break;
case 11:
if(player == 1){
player1_score = 26;
System.out.println(player1 + ", it is a ladder. Your score is now " + player1_score);
}else{
player2_score = 26;
System.out.println(player2 + ", it is a ladder. Your score is now " + player2_score);
}
break;
case 20:
if(player == 1){
player1_score = 29;
System.out.println(player1 + ", it is a ladder. Your score is now " + player1_score);
}else{
player2_score = 29;
System.out.println(player2 + ", it is a ladder. Your score is now " + player2_score);
}
break;
case 17:
if(player == 1){
player1_score = 4;
System.out.println(player1 + ", it is a snake. Your score is now " + player1_score);
}else{
player2_score = 4;
System.out.println(player2 + ", it is a snake. Your score is now " + player2_score);
}
break;
case 19:
if(player == 1){
player1_score = 7;
System.out.println(player1 + ", it is a snake. Your score is now " + player1_score);
}else{
player2_score = 7;
System.out.println(player2 + ", it is a snake. Your score is now " + player2_score);
}
break;
case 21:
if(player == 1){
player1_score = 9;
System.out.println(player1 + ", it is a snake. Your score is now " + player1_score);
}else{
player2_score = 9;
System.out.println(player2 + ", it is a snake. Your score is now " + player2_score);
}
break;
case 27:
if(player == 1){
player1_score = 1;
System.out.println(player1 + ", it is a snake. Your score is now " + player1_score);
}else{
player2_score = 1;
System.out.println(player2 + ", it is a snake. Your score is now " + player2_score);
}
break;
default:
break;
}
player = 2;
}
}
scanner.close();
}
}