Skip to content
This repository has been archived by the owner on Oct 29, 2023. It is now read-only.

fix: added rock paper scissor game #321

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions rockPaper/rock.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html>
<head>
<title>Rock Paper Scissors Game</title>
</head>
<body>
<h1>Rock Paper Scissors</h1>
<div id="result"></div>
<button onclick="playGame('rock')">Rock</button>
<button onclick="playGame('paper')">Paper</button>
<button onclick="playGame('scissors')">Scissors</button>

<script>
function playGame(playerChoice) {
const choices = ['rock', 'paper', 'scissors'];
const computerChoice = choices[Math.floor(Math.random() * 3)];

const result = getResult(playerChoice, computerChoice);
displayResult(playerChoice, computerChoice, result);
}

function getResult(player, computer) {
if (player === computer) {
return "It's a tie!";
} else if (
(player === 'rock' && computer === 'scissors') ||
(player === 'paper' && computer === 'rock') ||
(player === 'scissors' && computer === 'paper')
) {
return "You win!";
} else {
return "Computer wins!";
}
}

function displayResult(player, computer, result) {
const resultDiv = document.getElementById('result');
resultDiv.innerHTML = `You chose ${player}. Computer chose ${computer}. Result: ${result}`;
}
</script>
</body>
</html>