Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f6cbc23

Browse files
authored
Create 13. Cricket ScoreBoard Application.md
1 parent f1d4277 commit f6cbc23

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
### Cricket ScoreBoard Application
2+
* Build a cricket scorecard system that displays the team score and each player's performance.
3+
* Inputs: Number of players per team, number of overs, and batting order.
4+
* Ball-by-ball input includes runs (including wides, no balls, or wickets).
5+
- At the end of every over, print:
6+
- Individual scores, balls faced, number of 4s and 6s.
7+
- Total team score, wickets.
8+
- Implement strike changes, handle extras, and determine the match winner.
9+
10+
```js
11+
class CricketScoreBoard {
12+
constructor(numberOfPlayers, numberOfOvers) {
13+
this.players = []; // Store player performance
14+
this.numberOfPlayers = numberOfPlayers; // Players per team
15+
this.numberOfOvers = numberOfOvers; // Total overs
16+
this.totalScore = 0; // Team score
17+
this.totalWickets = 0; // Total wickets fallen
18+
this.currentOver = []; // Store current over balls
19+
this.currentPlayerIndex = 0; // Index of striker
20+
this.nextPlayerIndex = 1; // Index of next batsman
21+
this.isStrikeChange = false; // To handle strike changes
22+
23+
// Initialize players
24+
for (let i = 0; i < numberOfPlayers; i++) {
25+
this.players.push({
26+
id: i + 1,
27+
runs: 0,
28+
balls: 0,
29+
fours: 0,
30+
sixes: 0,
31+
isOut: false,
32+
});
33+
}
34+
}
35+
36+
// Process ball-by-ball input
37+
ballInput(input) {
38+
if (this.totalWickets === this.numberOfPlayers - 1) {
39+
console.log("All players are out, innings over.");
40+
return;
41+
}
42+
43+
if (this.currentOver.length >= 6) {
44+
console.log("Over already completed. Print results for this over.");
45+
return;
46+
}
47+
48+
switch (input) {
49+
case 'W': // Wicket
50+
console.log(`Player ${this.players[this.currentPlayerIndex].id} is OUT!`);
51+
this.players[this.currentPlayerIndex].isOut = true;
52+
this.totalWickets++;
53+
if (this.totalWickets === this.numberOfPlayers - 1) {
54+
console.log("Innings over, all players are out!");
55+
return;
56+
}
57+
this.currentPlayerIndex = this.nextPlayerIndex;
58+
this.nextPlayerIndex++;
59+
break;
60+
61+
case 'NB': // No-ball
62+
case 'WD': // Wide
63+
this.totalScore++; // Extras increase the total score
64+
console.log(`${input} - Extra run added.`);
65+
break;
66+
67+
default:
68+
const runs = Number(input);
69+
if (!isNaN(runs)) {
70+
this.totalScore += runs;
71+
let player = this.players[this.currentPlayerIndex];
72+
player.runs += runs;
73+
player.balls++;
74+
if (runs === 4) player.fours++;
75+
if (runs === 6) player.sixes++;
76+
console.log(`Player ${player.id}: Hits ${runs}!`);
77+
78+
// Handle strike change for odd runs
79+
if (runs % 2 !== 0) {
80+
this.isStrikeChange = !this.isStrikeChange;
81+
}
82+
} else {
83+
console.log("Invalid input.");
84+
}
85+
}
86+
87+
this.currentOver.push(input);
88+
89+
// If over completes, print the results and handle strike change
90+
if (this.currentOver.length === 6) {
91+
console.log(`Over completed.`);
92+
this.printOverResult();
93+
this.currentOver = [];
94+
this.isStrikeChange = !this.isStrikeChange;
95+
this.updateStrike();
96+
}
97+
}
98+
99+
// Update the strike after an over
100+
updateStrike() {
101+
if (this.isStrikeChange) {
102+
const tempIndex = this.currentPlayerIndex;
103+
this.currentPlayerIndex = this.nextPlayerIndex - 1;
104+
this.nextPlayerIndex = tempIndex + 1;
105+
}
106+
}
107+
108+
// Print the result of the current over
109+
printOverResult() {
110+
console.log("=================================");
111+
console.log("End of Over Summary:");
112+
this.players.forEach((player) => {
113+
if (!player.isOut && (player.runs > 0 || player.balls > 0)) {
114+
console.log(
115+
`Player ${player.id}: Runs=${player.runs}, Balls=${player.balls}, 4s=${player.fours}, 6s=${player.sixes}`
116+
);
117+
}
118+
});
119+
console.log(`Total Score: ${this.totalScore}, Wickets: ${this.totalWickets}`);
120+
console.log("=================================");
121+
}
122+
123+
// Print the final match result
124+
printMatchSummary() {
125+
console.log("=================================");
126+
console.log("Match Summary:");
127+
console.log(`Total Score: ${this.totalScore}`);
128+
console.log(`Wickets Fallen: ${this.totalWickets}`);
129+
this.players.forEach((player) => {
130+
console.log(
131+
`Player ${player.id}: Runs=${player.runs}, Balls=${player.balls}, 4s=${player.fours}, 6s=${player.sixes}`
132+
);
133+
});
134+
console.log("=================================");
135+
}
136+
}
137+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /