Skip to content

Commit

Permalink
Merge pull request #230 from dhruvaaArya05/add-bat-ball-stump-game
Browse files Browse the repository at this point in the history
Added Bat Ball Stump Game #164
  • Loading branch information
trishan9 authored Oct 19, 2024
2 parents bdc7896 + 691f1f7 commit d6a4dd4
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Bat Ball Stump Game/cricket.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
text-align: center;
}
82 changes: 82 additions & 0 deletions Bat Ball Stump Game/cricket.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="cricket.css">
<title>Cricket Game</title>
</head>

<body>
<h1>Bat Ball Stump Game</h1>
<button onclick="
let computerChoice = generateComputerChoice();
let resultMsg = getResult('Bat',computerChoice);
showResult('Bat',computerChoice,resultMsg);
">Bat</button>

<button onclick="
computerChoice = generateComputerChoice();
resultMsg = getResult('Ball',computerChoice);
showResult('Ball',computerChoice,resultMsg);
">Ball</button>

<button onclick="
computerChoice = generateComputerChoice();
resultMsg = getResult('Stump',computerChoice);
showResult('Stump',computerChoice,resultMsg);
">Stump</button>
</body>

<script>
function generateComputerChoice() {
let randomNumber = Math.random() * 3;

if (randomNumber > 0 && randomNumber <= 1) {
return 'Bat';
//console.log('Computer choice is bat');
} else if (randomNumber > 1 && randomNumber <= 2) {
return 'Ball';
//console.log('Computer choice is ball');
} else {
return 'Stump';
//console.log('Computer choice is stump');
}
}

function getResult(userMove, computerMove) {
if (userMove === 'Bat') {
if (computerMove === 'Ball') {
return `User Won`;
} else if (computerMove === 'Bat') {
return `It's a Tie`;
} else {
return `Computer Won`;
}
} else if (userMove === 'Ball') {
if (computerMove === 'Ball') {
return `It's a Tie`;
} else if (computerMove === 'Bat') {
return `Computer Won`;
} else {
return 'User Won';
}
} else {
if (computerMove === 'Ball') {
return `Computer Won`;
} else if (computerMove === 'Bat') {
return `User Won`;
} else {
return `It's a Tie`;
}
}
}

function showResult(userMove, computerMove, result) {
alert(`You have chosen ${userMove}. Computer choice is ${computerMove} and ${result}`);
}
</script>

</html>

0 comments on commit d6a4dd4

Please sign in to comment.