diff --git a/cricket-scorer/README.md b/cricket-scorer/README.md new file mode 100644 index 0000000..a04b5fb --- /dev/null +++ b/cricket-scorer/README.md @@ -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) diff --git a/cricket-scorer/code/index.html b/cricket-scorer/code/index.html new file mode 100644 index 0000000..e288985 --- /dev/null +++ b/cricket-scorer/code/index.html @@ -0,0 +1,42 @@ + + + + + + Cricket Scorer + + + +
+
+
Team A
+
+ 0/0 +
+
+ Overs: 0.0 +
+
+
+
+ + + + + + +
+
+ + + +
+
+ + +
+
+
+ + + diff --git a/cricket-scorer/code/script.js b/cricket-scorer/code/script.js new file mode 100644 index 0000000..3e75991 --- /dev/null +++ b/cricket-scorer/code/script.js @@ -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 }) + }; +} diff --git a/cricket-scorer/code/style.css b/cricket-scorer/code/style.css new file mode 100644 index 0000000..7733260 --- /dev/null +++ b/cricket-scorer/code/style.css @@ -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; +} diff --git a/cricket-scorer/code/test.js b/cricket-scorer/code/test.js new file mode 100644 index 0000000..89e93cc --- /dev/null +++ b/cricket-scorer/code/test.js @@ -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!');