-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCricket2.0.java
More file actions
143 lines (123 loc) · 5.45 KB
/
Cricket2.0.java
File metadata and controls
143 lines (123 loc) · 5.45 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
import java.util.InputMismatchException;
import java.util.Scanner;
class CricketOrder {
void randomBatting(String[] p_name, int o_count, int p_count) {
int n = p_name.length;
int[] randomOrder = new int[n];
boolean[] used = new boolean[n];
for (int i = 0; i < n; i++) {
int rand;
do {
rand = (int)(Math.random() * n);
} while (used[rand]);
used[rand] = true;
randomOrder[i] = rand;
}
// Batting order
System.out.println("\n--- Batting Order ---");
for (int i = 0; i < n; i++) {
System.out.println("Bat " + (i + 1) + " → Player: " + p_name[randomOrder[i]]);
}
// Wicket keeping order
System.out.println("\n--- Wicket Keeping Order ---");
for (int i = 0; i < n; i++) {
int keeperIndex = (i + 1) % n;
System.out.println("While Player " + (i + 1) + " is batting → Wicket Keeper: " + p_name[randomOrder[keeperIndex]]);
}
// Bowling order
System.out.println("\n--- Bowling Order ---");
for (int i = 0; i < n; i++) {
System.out.println("\nWhen Player " + (i + 1) + " (" + p_name[randomOrder[i]] + ") is batting:");
int overNumber = 1;
int bowlerIndex = (i - 1 + n) % n;
for (int j = 0; j < o_count; j++) {
while (bowlerIndex == i) {
bowlerIndex = (bowlerIndex - 1 + n) % n;
}
System.out.println(" Over " + (overNumber++) + " → Bowler: " + p_name[randomOrder[bowlerIndex]]);
bowlerIndex = (bowlerIndex - 1 + n) % n;
}
}
}
void manualBatting(String[] p_name, int o_count, int p_count) {
System.out.println("\n--- Batting Order ---");
for (int i = 0; i < p_name.length; i++) {
System.out.println("Bat " + (i + 1) + " → Player: " + p_name[i]);
}
System.out.println("\n--- Wicket Keeping Order ---");
for (int i = 0; i < p_count; i++) {
int keeper_index = (i + 1) % p_count;
System.out.println("While Player " + (i + 1) + " is batting → Wicket Keeper: " + p_name[keeper_index]);
}
System.out.println("\n--- Bowling Order ---");
for (int i = 0; i < p_count; i++) {
System.out.println("\nWhen Player " + (i + 1) + " (" + p_name[i] + ") is batting:");
int over_number = 1;
int bowler_index = (i - 1 + p_count) % p_count;
for (int j = 0; j < o_count; j++) {
while (bowler_index == i) {
bowler_index = (bowler_index - 1 + p_count) % p_count;
}
System.out.println(" Over " + (over_number++) + " → Bowler: " + p_name[bowler_index]);
bowler_index = (bowler_index - 1 + p_count) % p_count;
}
}
}
}
public class Cricket {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
CricketOrder game = new CricketOrder();
boolean playAgain;
System.out.println("Welcome to STREET_CRICKET!");
do {
int player_count = 0, overs_count = 0;
boolean validInput = false;
// Input validation loop
while (!validInput) {
try {
System.out.print("\nEnter the number of players (≥2): ");
player_count = sc.nextInt();
if (player_count < 2) {
System.out.println("You need at least 2 players to play.");
continue;
}
System.out.print("Enter the number of overs per player (>0): ");
overs_count = sc.nextInt();
if (overs_count <= 0) {
System.out.println("Overs should be more than 0.");
continue;
}
sc.nextLine(); // Clear buffer
validInput = true;
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter numeric values only.");
sc.nextLine(); // Clear invalid input
}
}
// Player names
String[] player_names = new String[player_count];
System.out.println("\n--- Enter Player Names ---");
for (int i = 0; i < player_names.length; i++) {
System.out.print("Player " + (i + 1) + " name: ");
player_names[i] = sc.nextLine().trim();
}
// Choose batting order format
System.out.print("\nChoose batting order (Y = random, N = manual): ");
String choice = sc.next();
if (choice.equalsIgnoreCase("Y")) {
game.randomBatting(player_names, overs_count, player_count);
} else if (choice.equalsIgnoreCase("N")) {
game.manualBatting(player_names, overs_count, player_count);
} else {
System.out.println("Invalid choice. Please enter 'Y' or 'N'.");
}
// Play again option
System.out.print("\nWould you like to play again? (Y/N): ");
String again = sc.next();
playAgain = again.equalsIgnoreCase("Y");
} while (playAgain);
System.out.println("\n Thanks for playing Street Cricket! Goodbye!");
sc.close();
}
}