Open In App

Rock Paper Scissor Game using Tailwind CSS & JavaScript

Rock Paper Scissors Game is a web-based game that simulates the classic hand game. It allows users to play against the computer by choosing between rock, paper, or scissors. The game is built using Tailwind CSS, a utility-first CSS framework, which ensures a responsive and visual design.

Approach to create Rock, Paper, Scissors Game:

Example: Implementation to design rock paper scissor game.






<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>Rock Paper Scissors</title>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <link href=
          rel="stylesheet">
</head>
 
<body class="bg-gray-100">
    <div
        class="game-container border-green-500 max-w-md
               mx-auto text-center bg-gray-100 rounded-lg
               shadow-md border-2 border-green-500 p-8 mt-20">
        <h1 class="text-3xl font-semibold mb-4">
              Rock Paper Scissors
          </h1>
        <div class="button-container">
            <button class="btn bg-blue-500 text-white
                           font-semibold px-4 py-2 rounded mr-2" id="rock">
                  Rock
              </button>
            <button class="btn bg-blue-500 text-white
                           font-semibold px-4 py-2 rounded mr-2" id="paper">
                  Paper
              </button>
            <button class="btn bg-blue-500 text-white
                           font-semibold px-4 py-2 rounded" id="scissors">
                  Scissors
              </button>
        </div>
        <div class="result-container flex flex-col justify-center mt-10">
            <p class="font-semibold">
                  Your Choice: <span id="playerChoice"></span>
              </p>
            <p class="font-semibold">
                  Computer's Choice: <span id="computerChoice"></span>
              </p>
            <p class="font-semibold">
                  Your Score: <span id="playerScore">0</span>
              </p>
            <p class="font-semibold">
                  Computer Score: <span id="computerScore">0</span>
              </p>
            <p class="result-text" id="result"></p>
        </div>
    </div>
    <script>
        let playerScore = 0;
        let computerScore = 0;
        const playerScoreDisplay = document.getElementById('playerScore');
        const computerScoreDisplay = document.getElementById('computerScore');
        const playerChoiceDisplay = document.getElementById('playerChoice');
        const computerChoiceDisplay = document.getElementById('computerChoice');
        const buttons = document.querySelectorAll('.btn');
        const choices = ['rock', 'paper', 'scissors'];
        buttons.forEach(button => {
            button.addEventListener('click', () => {
                const playerChoice = button.id;
                const computerChoice =
                          choices[Math.floor(Math.random() * choices.length)];
                playerChoiceDisplay.textContent =
                      playerChoice.charAt(0).toUpperCase() + playerChoice.slice(1);
                computerChoiceDisplay.textContent =
                      computerChoice.charAt(0).toUpperCase() + computerChoice.slice(1);
                const result = playRound(playerChoice, computerChoice);
                document.getElementById('result').innerText = result;
                playerScoreDisplay.textContent = playerScore;
                computerScoreDisplay.textContent = computerScore;
            });
        });
        function playRound(playerChoice, computerChoice) {
            if (playerChoice === computerChoice) {
                return "It's a tie!";
            } else if (
                (playerChoice === 'rock' && computerChoice === 'scissors') ||
                (playerChoice === 'paper' && computerChoice === 'rock') ||
                (playerChoice === 'scissors' && computerChoice === 'paper')
            ) {
                playerScore++;
                return 'You win!';
            } else {
                computerScore++;
                return 'Computer wins!';
            }
        }
    </script>
</body>
 
</html>

Output:




Article Tags :