-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLudo.java
More file actions
63 lines (45 loc) · 1.9 KB
/
Ludo.java
File metadata and controls
63 lines (45 loc) · 1.9 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
import java.util.Scanner;
import java.util.Random;
public class ludo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
System.out.print("Enter number of players (2-4): ");
int numPlayers = scanner.nextInt();
if (numPlayers < 2 || numPlayers > 4) {
System.out.println("Invalid number of players. Please restart and enter 2 to 4 players.");
scanner.close();
return;
}
System.out.println("\nStarting Ludo game with " + numPlayers + " players!");
System.out.println("First player to reach position 30 wins.\n");
int[] positions = new int[numPlayers];
boolean gameOver = false;
int currentPlayer = 0;
while (!gameOver) {
System.out.println("Player " + (currentPlayer + 1) + "'s turn");
System.out.print("Press 1 to roll the dice: ");
int choice = scanner.nextInt();
if (choice != 1) {
System.out.println("Invalid input. Turn skipped.\n");
} else {
int dice = random.nextInt(6) + 1;
System.out.println("Dice rolled: " + dice);
positions[currentPlayer] += dice;
if (positions[currentPlayer] > 30) {
positions[currentPlayer] = 30;
}
System.out.println("Player " + (currentPlayer + 1) +
" position: " + positions[currentPlayer]);
if (positions[currentPlayer] == 30) {
System.out.println("\n🎉 Player " + (currentPlayer + 1) + " WINS THE GAME! 🎉");
gameOver = true;
break;
}
}
System.out.println();
currentPlayer = (currentPlayer + 1) % numPlayers;
}
scanner.close();
}
}