give me html, javascript, css all in one file of rock paper scissors
- User
give me html, javascript, css all in one file of rock paper scissors
- Assistant
give me html, javascript, css all in one file of rock paper scissors
Sure! Below is a complete HTML file that includes HTML, CSS, and JavaScript for a simple Rock-Paper-Scissors game. You can copy this code into a file with a .html extension (e.g., rock-paper-scissors.html) and open it in your web browser to play the game.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Rock Paper Scissors Game</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
text-align: center;
padding: 50px;
}
h1 {
color: #333;
}
.choices {
margin: 20px 0;
}
.choice-btn {
padding: 10px 20px;
margin: 0 10px;
font-size: 16px;
cursor: pointer;
border: none;
border-radius: 5px;
transition: background-color 0.3s;
}
.choice-btn:hover {
background-color: #ddd;
}
.result {
margin-top: 30px;
font-size: 20px;
min-height: 40px;
}
.scoreboard {
margin-top: 20px;
font-size: 18px;
}
/* Responsive Design */
@media (max-width: 600px) {
.choice-btn {
margin: 10px 0;
width: 80%;
}
}
</style>
</head>
<body>
<h1>Rock Paper Scissors</h1>
<div class="choices">
<button class="choice-btn" data-choice="rock">Rock</button>
<button class="choice-btn" data-choice="paper">Paper</button>
<button class="choice-btn" data-choice="scissors">Scissors</button>
</div>
<div class="result" id="result"></div>
<div class="scoreboard">
<span id="user-score">User: 0</span> |
<span id="computer-score">Computer: 0</span> |
<span id="ties">Ties: 0</span>
</div>
<script>
// Initialize scores
let userScore = 0;
let computerScore = 0;
let ties = 0;
// Get DOM elements
const buttons = document.querySelectorAll('.choice-btn');
const resultDiv = document.getElementById('result');
const userScoreSpan = document.getElementById('user-score');
const computerScoreSpan = document.getElementById('computer-score');
const tiesSpan = document.getElementById('ties');
// Possible choices
const choices = ['rock', 'paper', 'scissors'];
// Add event listeners to buttons
buttons.forEach(button => {
button.addEventListener('click', () => {
const userChoice = button.getAttribute('data-choice');
const computerChoice = getComputerChoice();
const outcome = determineWinner(userChoice, computerChoice);
updateScores(outcome);
displayResult(userChoice, computerChoice, outcome);
});
});
// Function to get computer's choice
function getComputerChoice() {
const randomIndex = Math.floor(Math.random() * choices.length);
return choices[randomIndex];
}
// Function to determine the winner
function determineWinner(user, computer) {
if (user === computer) {
return 'tie';
}
if (
(user === 'rock' && computer === 'scissors') ||
(user === 'paper' && computer === 'rock') ||
(user === 'scissors' && computer === 'paper')
) {
return 'win';
} else {
return 'lose';
}
}
// Function to update scores
function updateScores(outcome) {
if (outcome === 'win') {
userScore++;
} else if (outcome === 'lose') {
computerScore++;
} else {
ties++;
}
userScoreSpan.textContent = `User: ${userScore}`;
computerScoreSpan.textContent = `Computer: ${computerScore}`;
tiesSpan.textContent = `Ties: ${ties}`;
}
// Function to display the result
function displayResult(user, computer, outcome) {
let message = `You chose <strong>${capitalize(user)}</strong>. Computer chose <strong>${capitalize(computer)}</strong>. `;
if (outcome === 'win') {
message += 'You <span style="color: green;">win!</span>';
} else if (outcome === 'lose') {
message += 'You <span style="color: red;">lose!</span>';
} else {
message += "It's a <span style='color: gray;'>tie</span>!";
}
resultDiv.innerHTML = message;
}
// Helper function to capitalize first letter
function capitalize(word) {
return word.charAt(0).toUpperCase() + word.slice(1);
}
</script>
</body>
</html>
HTML Structure:
<h1>): Displays the title of the game..choices div): Contains three buttons for "Rock," "Paper," and "Scissors." Each button has a data-choice attribute to identify the user's selection.#result div): Shows the outcome of each round..scoreboard div): Displays the current scores for the user, computer, and the number of ties.CSS Styling:
JavaScript Functionality:
getComputerChoice(): Randomly selects between "rock," "paper," and "scissors" for the computer.determineWinner(user, computer): Determines if the user wins, loses, or ties based on the classic rules.updateScores(outcome): Updates the score counters based on the outcome.displayResult(user, computer, outcome): Displays a descriptive message of the round's result.capitalize(word): Utility function to capitalize the first letter of a given word for better display.Save the Code: Copy the above code into a text editor and save it with a .html extension, for example, rock-paper-scissors.html.
Open in Browser: Double-click the saved file or open it using your preferred web browser.
Play the Game: Click on "Rock," "Paper," or "Scissors" to play against the computer. The result and updated scores will be displayed on the page.
Feel free to customize the styling or enhance the functionality further as per your preferences!
Not yet included in a published release.