Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions cricket-scorer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 🏏 Cricket Scorer

A simple web-based cricket scoring application. This project is a submission for the Vibe Coding Event.

---

## ✅ Problem Statement

The goal of this project is to create a simple, easy-to-use cricket scoring application. It should allow a user to keep track of the score, wickets, and overs for a cricket match. This is a simplified clone of the popular CricHeroes app, focusing on the core scoring functionality.

---

## 🛠 Tech Stack

- **HTML:** For the basic structure of the application.
- **CSS:** For styling the application and making it visually appealing.
- **JavaScript:** For the scoring logic and interactivity.

---

## 📦 Setup / Run Instructions

1. Clone this repository.
2. Navigate to the `cricket-scorer/code` directory.
3. Open the `index.html` file in your web browser.

---

## 👥 Contributors

- Jules (AI Agent)
42 changes: 42 additions & 0 deletions cricket-scorer/code/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cricket Scorer</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="scoreboard">
<div class="team-name">Team A</div>
<div class="score">
<span id="runs">0</span>/<span id="wickets">0</span>
</div>
<div class="overs">
Overs: <span id="overs">0</span>.<span id="balls">0</span>
</div>
</div>
<div class="controls">
<div class="runs-buttons">
<button onclick="addRuns(0)">0</button>
<button onclick="addRuns(1)">1</button>
<button onclick="addRuns(2)">2</button>
<button onclick="addRuns(3)">3</button>
<button onclick="addRuns(4)">4</button>
<button onclick="addRuns(6)">6</button>
</div>
<div class="extra-buttons">
<button onclick="addExtra('wide')">Wide</button>
<button onclick="addExtra('no-ball')">No Ball</button>
<button onclick="addWicket()">Wicket</button>
</div>
<div class="utility-buttons">
<button onclick="undo()">Undo</button>
<button onclick="reset()">Reset</button>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
120 changes: 120 additions & 0 deletions cricket-scorer/code/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
let runs = 0;
let wickets = 0;
let overs = 0;
let balls = 0;
let history = [];

let runsEl, wicketsEl, oversEl, ballsEl;

if (typeof window !== 'undefined') {
runsEl = document.getElementById('runs');
wicketsEl = document.getElementById('wickets');
oversEl = document.getElementById('overs');
ballsEl = document.getElementById('balls');
} else {
// Mock DOM elements for testing in Node.js
runsEl = { innerText: '' };
wicketsEl = { innerText: '' };
oversEl = { innerText: '' };
ballsEl = { innerText: '' };
}


function addRuns(run) {
if (wickets < 10) {
runs += run;
addBall();
updateScore();
addToHistory({ runs: run, type: 'run' });
}
}

function addExtra(type) {
if (wickets < 10) {
runs++;
if (type === 'no-ball') {
addBall();
}
updateScore();
addToHistory({ runs: 1, type: type });
}
}

function addWicket() {
if (wickets < 10) {
wickets++;
addBall();
updateScore();
addToHistory({ type: 'wicket' });
}
}

function addBall() {
if (balls < 5) {
balls++;
} else {
balls = 0;
overs++;
}
}

function updateScore() {
runsEl.innerText = runs;
wicketsEl.innerText = wickets;
oversEl.innerText = overs;
ballsEl.innerText = balls;
}

function undo() {
if (history.length > 0) {
const lastAction = history.pop();
if (lastAction.type === 'run') {
runs -= lastAction.runs;
removeBall();
} else if (lastAction.type === 'wide' || lastAction.type === 'no-ball') {
runs -= 1;
if (lastAction.type === 'no-ball') {
removeBall();
}
} else if (lastAction.type === 'wicket') {
wickets--;
removeBall();
}
updateScore();
}
}

function removeBall() {
if (balls > 0) {
balls--;
} else {
if (overs > 0) {
overs--;
balls = 5;
}
}
}

function reset() {
runs = 0;
wickets = 0;
overs = 0;
balls = 0;
history = [];
updateScore();
}

function addToHistory(action) {
history.push(action);
}

if (typeof module !== 'undefined' && module.exports) {
module.exports = {
addRuns,
addExtra,
addWicket,
undo,
reset,
getScore: () => ({ runs, wickets, overs, balls })
};
}
85 changes: 85 additions & 0 deletions cricket-scorer/code/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}

.container {
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
width: 350px;
padding: 20px;
}

.scoreboard {
text-align: center;
margin-bottom: 20px;
}

.team-name {
font-size: 24px;
font-weight: bold;
margin-bottom: 10px;
}

.score {
font-size: 48px;
font-weight: bold;
margin-bottom: 10px;
}

.overs {
font-size: 18px;
}

.controls {
display: flex;
flex-direction: column;
gap: 10px;
}

.runs-buttons,
.extra-buttons,
.utility-buttons {
display: flex;
justify-content: space-around;
}

button {
background-color: #4CAF50;
color: white;
border: none;
padding: 15px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}

button:hover {
background-color: #45a049;
}

.extra-buttons button {
background-color: #f44336;
}

.extra-buttons button:hover {
background-color: #da190b;
}

.utility-buttons button {
background-color: #008CBA;
}

.utility-buttons button:hover {
background-color: #007ba7;
}
81 changes: 81 additions & 0 deletions cricket-scorer/code/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const assert = require('assert');
const scorer = require('./script.js');

function runTest(testFunction) {
scorer.reset();
testFunction();
}

function testAddRuns() {
scorer.addRuns(4);
let score = scorer.getScore();
assert.strictEqual(score.runs, 4, 'testAddRuns: Runs should be 4');
assert.strictEqual(score.balls, 1, 'testAddRuns: Balls should be 1');
console.log('testAddRuns passed');
}

function testAddWicket() {
scorer.addWicket();
let score = scorer.getScore();
assert.strictEqual(score.wickets, 1, 'testAddWicket: Wickets should be 1');
assert.strictEqual(score.balls, 1, 'testAddWicket: Balls should be 1');
console.log('testAddWicket passed');
}

function testFullOver() {
for (let i = 0; i < 6; i++) {
scorer.addRuns(1);
}
let score = scorer.getScore();
assert.strictEqual(score.overs, 1, 'testFullOver: Overs should be 1');
assert.strictEqual(score.balls, 0, 'testFullOver: Balls should be 0');
console.log('testFullOver passed');
}

function testExtras() {
scorer.addExtra('wide');
let score = scorer.getScore();
assert.strictEqual(score.runs, 1, 'testExtras (wide): Runs should be 1');
assert.strictEqual(score.balls, 0, 'testExtras (wide): Balls should be 0');

scorer.addExtra('no-ball');
score = scorer.getScore();
assert.strictEqual(score.runs, 2, 'testExtras (no-ball): Runs should be 2');
assert.strictEqual(score.balls, 1, 'testExtras (no-ball): Balls should be 1');
console.log('testExtras passed');
}

function testUndo() {
scorer.addRuns(4);
scorer.addWicket();
scorer.undo();
let score = scorer.getScore();
assert.strictEqual(score.wickets, 0, 'testUndo: Wickets should be 0');
assert.strictEqual(score.balls, 1, 'testUndo: Balls should be 1');
scorer.undo();
score = scorer.getScore();
assert.strictEqual(score.runs, 0, 'testUndo: Runs should be 0');
assert.strictEqual(score.balls, 0, 'testUndo: Balls should be 0');
console.log('testUndo passed');
}

function testReset() {
scorer.addRuns(6);
scorer.addWicket();
scorer.reset();
let score = scorer.getScore();
assert.strictEqual(score.runs, 0, 'testReset: Runs should be 0');
assert.strictEqual(score.wickets, 0, 'testReset: Wickets should be 0');
assert.strictEqual(score.overs, 0, 'testReset: Overs should be 0');
assert.strictEqual(score.balls, 0, 'testReset: Balls should be 0');
console.log('testReset passed');
}

runTest(testAddRuns);
runTest(testAddWicket);
runTest(testFullOver);
runTest(testExtras);
runTest(testUndo);
runTest(testReset);

console.log('All tests passed!');