• Home
  • /
  • Rock Paper Scissors with HTML/CSS/JS

Rock Paper Scissors with HTML/CSS/JS

HTML

				
					<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Professional Rock-Paper-Scissors | Learn with Arshyan</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #0a192f, #172a45);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
            color: #e6f1ff;
            overflow-x: hidden;
        }
        
        .container {
            max-width: 900px;
            width: 100%;
            background: rgba(10, 25, 47, 0.85);
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.3);
            overflow: hidden;
            padding: 40px;
            text-align: center;
            position: relative;
            z-index: 10;
            border: 1px solid rgba(100, 255, 218, 0.1);
        }
        
        .decoration {
            position: absolute;
            width: 200px;
            height: 200px;
            background: rgba(100, 255, 218, 0.05);
            border-radius: 50%;
            z-index: -1;
        }
        
        .dec-1 {
            top: -80px;
            left: -80px;
        }
        
        .dec-2 {
            bottom: -100px;
            right: -80px;
            width: 250px;
            height: 250px;
        }
        
        header {
            margin-bottom: 30px;
            position: relative;
            padding-bottom: 20px;
            border-bottom: 1px solid rgba(100, 255, 218, 0.2);
        }
        
        header h1 {
            font-size: 3.2rem;
            margin-bottom: 10px;
            color: #e6f1ff;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
        }
        
        .brand {
            color: #64ffda;
            font-weight: 700;
        }
        
        .tagline {
            font-size: 1.1rem;
            color: #8892b0;
            margin-top: 5px;
            font-weight: 400;
        }
        
        .scores {
            display: flex;
            justify-content: space-around;
            margin: 30px 0 40px;
            font-size: 1.2rem;
            background: rgba(23, 42, 69, 0.7);
            padding: 20px;
            border-radius: 15px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
            border: 1px solid rgba(100, 255, 218, 0.1);
        }
        
        .score-box {
            padding: 15px 30px;
            border-radius: 12px;
            background: rgba(10, 25, 47, 0.8);
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.15);
            min-width: 140px;
            border: 1px solid rgba(100, 255, 218, 0.1);
        }
        
        .score-box h3 {
            color: #64ffda;
            margin-bottom: 10px;
            font-size: 1.1rem;
            font-weight: 500;
        }
        
        .score-box p {
            font-size: 2.5rem;
            font-weight: 700;
            color: #e6f1ff;
        }
        
        .choices {
            display: flex;
            justify-content: center;
            gap: 40px;
            margin: 40px 0;
        }
        
        .choice {
            width: 140px;
            height: 140px;
            border-radius: 50%;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 4.5rem;
            cursor: pointer;
            transition: all 0.3s ease;
            border: 2px solid transparent;
            background: rgba(23, 42, 69, 0.7);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
            position: relative;
            overflow: hidden;
        }
        
        .choice::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(100, 255, 218, 0.1);
            opacity: 0;
            transition: opacity 0.3s ease;
        }
        
        .choice:hover {
            transform: translateY(-8px);
            border-color: #64ffda;
        }
        
        .choice:hover::before {
            opacity: 1;
        }
        
        .choice.selected {
            transform: scale(1.12);
            box-shadow: 0 0 30px rgba(100, 255, 218, 0.3);
            border-color: #64ffda;
        }
        
        .choice i {
            color: #64ffda;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
        }
        
        .vs {
            font-size: 2.2rem;
            font-weight: 700;
            color: #64ffda;
            display: flex;
            align-items: center;
            margin: 0 20px;
        }
        
        .result-area {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 50px;
            margin: 40px 0;
        }
        
        .player-choice, .computer-choice {
            width: 160px;
            height: 160px;
            background: rgba(23, 42, 69, 0.7);
            border-radius: 50%;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 4.5rem;
            border: 2px solid rgba(100, 255, 218, 0.2);
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
        }
        
        .player-choice i, .computer-choice i {
            color: #64ffda;
        }
        
        .result-text {
            margin: 30px 0;
            min-height: 70px;
            font-size: 2.4rem;
            font-weight: 700;
            padding: 20px 40px;
            border-radius: 50px;
            display: inline-block;
            background: rgba(23, 42, 69, 0.7);
            border: 1px solid rgba(100, 255, 218, 0.2);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }
        
        .win {
            color: #64ffda;
            border-color: rgba(100, 255, 218, 0.4);
            box-shadow: 0 0 20px rgba(100, 255, 218, 0.3);
        }
        
        .lose {
            color: #ff5555;
            border-color: rgba(255, 85, 85, 0.4);
            box-shadow: 0 0 20px rgba(255, 85, 85, 0.2);
        }
        
        .draw {
            color: #8892b0;
            border-color: rgba(136, 146, 176, 0.4);
        }
        
        .action-btn {
            background: transparent;
            color: #64ffda;
            border: 2px solid #64ffda;
            padding: 16px 45px;
            font-size: 1.2rem;
            border-radius: 50px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: 600;
            margin-top: 20px;
            letter-spacing: 1px;
            position: relative;
            overflow: hidden;
        }
        
        .action-btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(100, 255, 218, 0.2), transparent);
            transition: 0.5s;
        }
        
        .action-btn:hover {
            background: rgba(100, 255, 218, 0.1);
            box-shadow: 0 0 15px rgba(100, 255, 218, 0.3);
        }
        
        .action-btn:hover::before {
            left: 100%;
        }
        
        .action-btn:active {
            transform: translateY(2px);
        }
        
        .rules {
            margin-top: 40px;
            background: rgba(23, 42, 69, 0.7);
            padding: 25px;
            border-radius: 15px;
            text-align: left;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
            border: 1px solid rgba(100, 255, 218, 0.1);
        }
        
        .rules h3 {
            color: #64ffda;
            margin-bottom: 20px;
            font-size: 1.6rem;
            text-align: center;
            font-weight: 600;
        }
        
        .rules ul {
            padding-left: 25px;
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
        }
        
        .rules li {
            margin: 12px 0;
            line-height: 1.6;
            font-size: 1.1rem;
            color: #a8b2d1;
            padding: 12px 18px;
            border-radius: 8px;
            background: rgba(10, 25, 47, 0.5);
            border-left: 3px solid #64ffda;
        }
        
        .rules li strong {
            color: #64ffda;
        }
        
        .history {
            margin-top: 30px;
            padding: 20px;
            background: rgba(23, 42, 69, 0.7);
            border-radius: 15px;
            max-height: 180px;
            overflow-y: auto;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
            border: 1px solid rgba(100, 255, 218, 0.1);
        }
        
        .history h3 {
            color: #64ffda;
            margin-bottom: 20px;
            font-size: 1.6rem;
            text-align: center;
            font-weight: 600;
        }
        
        .history-item {
            padding: 12px;
            border-bottom: 1px solid rgba(100, 255, 218, 0.1);
            display: flex;
            justify-content: space-between;
            align-items: center;
            background: rgba(10, 25, 47, 0.5);
            margin-bottom: 10px;
            border-radius: 8px;
        }
        
        .history-item span {
            color: #a8b2d1;
        }
        
        .win {
            color: #64ffda;
        }
        
        .lose {
            color: #ff5555;
        }
        
        .draw {
            color: #8892b0;
        }
        
        .result-icon {
            font-size: 1.2rem;
            margin-left: 8px;
        }
        
        @media (max-width: 768px) {
            .container {
                padding: 30px 20px;
            }
            
            header h1 {
                font-size: 2.5rem;
            }
            
            .choices {
                gap: 20px;
            }
            
            .choice {
                width: 110px;
                height: 110px;
                font-size: 3.5rem;
            }
            
            .result-area {
                gap: 20px;
            }
            
            .player-choice, .computer-choice {
                width: 120px;
                height: 120px;
                font-size: 3.5rem;
            }
            
            .result-text {
                font-size: 1.8rem;
                padding: 15px 25px;
            }
            
            .scores {
                padding: 15px;
            }
            
            .score-box {
                padding: 12px 15px;
                min-width: 100px;
            }
            
            .score-box p {
                font-size: 2rem;
            }
        }
        
        @media (max-width: 480px) {
            .choices {
                flex-direction: column;
                align-items: center;
                gap: 25px;
            }
            
            .result-area {
                flex-direction: column;
                align-items: center;
                gap: 25px;
            }
            
            .vs {
                transform: rotate(90deg);
                padding: 20px 0;
            }
            
            .rules ul {
                grid-template-columns: 1fr;
            }
        }
        
        /* Animation for choices */
        @keyframes pulse {
            0% { transform: scale(1); }
            50% { transform: scale(1.05); }
            100% { transform: scale(1); }
        }
        
        .pulse {
            animation: pulse 0.4s ease;
        }
        
        /* History scrollbar styling */
        .history::-webkit-scrollbar {
            width: 6px;
        }
        
        .history::-webkit-scrollbar-track {
            background: rgba(10, 25, 47, 0.3);
            border-radius: 4px;
        }
        
        .history::-webkit-scrollbar-thumb {
            background: #64ffda;
            border-radius: 4px;
        }
    </style>
</head>
<body>
    <div class="decoration dec-1"></div>
    <div class="decoration dec-2"></div>
    
    <div class="container">
        <header>
            <h1>ROCK <span class="brand">PAPER</span> SCISSORS</h1>
            <div class='code-block code-block-2' style='margin: 8px 0; clear: both;'>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2094722033678002"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-2094722033678002"
     data-ad-slot="2248279416"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<p class="tagline">Learn with Arshyan - Professional Edition</p>
        </header>
        
        <div class="scores">
            <div class="score-box">
                <h3>PLAYER</h3>
                <p id="player-score">0</p>
            </div>
            <div class="score-box">
                <h3>COMPUTER</h3>
                <p id="computer-score">0</p>
            </div>
            <div class="score-box">
                <h3>ROUNDS</h3>
                <p id="rounds">0</p>
            </div>
        </div>
        
        <div class="choices">
            <div class="choice" id="rock" data-choice="rock">
                <i class="fa-solid fa-hand-back-fist"></i>
            </div>
            <div class="choice" id="paper" data-choice="paper">
                <i class="fa-solid fa-hand"></i>
            </div>
            <div class="choice" id="scissors" data-choice="scissors">
                <i class="fa-solid fa-hand-scissors"></i>
            </div>
        </div>
        
        <div class="result-area">
            <div class="player-choice">
                <i class="fa-solid fa-question"></i>
            </div>
            <div class="vs">VS</div>
            <div class="computer-choice">
                <i class="fa-solid fa-question"></i>
            </div>
        </div>
        
        <div class="result-text">Select your move to begin</div>
        
        <button class="action-btn" id="reset-btn">
            <i class="fas fa-sync-alt"></i> RESET GAME
        </button>
        
        <div class="rules">
            <h3>GAME RULES</h3>
            <ul>
                <li><strong>ROCK</strong> defeats <strong>SCISSORS</strong> (rock crushes scissors)</li>
                <li><strong>PAPER</strong> defeats <strong>ROCK</strong> (paper covers rock)</li>
                <li><strong>SCISSORS</strong> defeats <strong>PAPER</strong> (scissors cut paper)</li>
                <li>Identical selections result in a <strong>DRAW</strong></li>
            </ul>
        </div>
        
        <div class="history">
            <h3>GAME HISTORY</h3>
            <div id="history-list">No moves recorded yet</div>
        </div>
    </div>
    
    <script>
        // Game state
        const gameState = {
            playerScore: 0,
            computerScore: 0,
            rounds: 0,
            history: []
        };
        
        // DOM elements
        const playerScoreEl = document.getElementById('player-score');
        const computerScoreEl = document.getElementById('computer-score');
        const roundsEl = document.getElementById('rounds');
        const choices = document.querySelectorAll('.choice');
        const playerChoiceEl = document.querySelector('.player-choice');
        const computerChoiceEl = document.querySelector('.computer-choice');
        const resultTextEl = document.querySelector('.result-text');
        const resetBtn = document.getElementById('reset-btn');
        const historyListEl = document.getElementById('history-list');
        
        // Choice icons mapping
        const choiceIcons = {
            rock: 'fa-hand-back-fist',
            paper: 'fa-hand',
            scissors: 'fa-hand-scissors'
        };
        
        // Result icons
        const resultIcons = {
            win: 'fa-trophy',
            lose: 'fa-times',
            draw: 'fa-equals'
        };
        
        // Initialize game
        function initGame() {
            updateScores();
            choices.forEach(choice => {
                choice.addEventListener('click', () => {
                    // Remove selected class from all choices
                    choices.forEach(c => c.classList.remove('selected'));
                    
                    // Add selected class to clicked choice
                    choice.classList.add('selected');
                    choice.classList.add('pulse');
                    
                    // Remove animation class after animation completes
                    setTimeout(() => {
                        choice.classList.remove('pulse');
                    }, 400);
                    
                    // Get player choice
                    const playerChoice = choice.dataset.choice;
                    
                    // Play the round
                    playRound(playerChoice);
                });
            });
            
            resetBtn.addEventListener('click', resetGame);
        }
        
        // Play a round
        function playRound(playerChoice) {
            // Get computer choice
            const computerChoice = getComputerChoice();
            
            // Display choices
            displayChoice(playerChoiceEl, playerChoice);
            displayChoice(computerChoiceEl, computerChoice);
            
            // Determine winner
            const result = determineWinner(playerChoice, computerChoice);
            
            // Update scores
            if (result === 'win') {
                gameState.playerScore++;
                resultTextEl.textContent = 'PLAYER WINS!';
                resultTextEl.className = 'result-text win';
            } else if (result === 'lose') {
                gameState.computerScore++;
                resultTextEl.textContent = 'COMPUTER WINS!';
                resultTextEl.className = 'result-text lose';
            } else {
                resultTextEl.textContent = "DRAW!";
                resultTextEl.className = 'result-text draw';
            }
            
            // Update game state
            gameState.rounds++;
            
            // Add to history
            const historyEntry = {
                round: gameState.rounds,
                player: playerChoice,
                computer: computerChoice,
                result: result
            };
            
            gameState.history.unshift(historyEntry);
            updateHistory();
            
            // Update UI
            updateScores();
        }
        
        // Get computer choice
        function getComputerChoice() {
            const choices = ['rock', 'paper', 'scissors'];
            const randomIndex = Math.floor(Math.random() * 3);
            return choices[randomIndex];
        }
        
        // Display choice
        function displayChoice(element, choice) {
            element.innerHTML = `<i class="fa-solid ${choiceIcons[choice]}"></i>`;
            element.classList.add('pulse');
            
            // Remove animation class after animation completes
            setTimeout(() => {
                element.classList.remove('pulse');
            }, 400);
        }
        
        // Determine winner
        function determineWinner(player, computer) {
            if (player === computer) return 'draw';
            
            if (
                (player === 'rock' && computer === 'scissors') ||
                (player === 'paper' && computer === 'rock') ||
                (player === 'scissors' && computer === 'paper')
            ) {
                return 'win';
            }
            
            return 'lose';
        }
        
        // Update scores display
        function updateScores() {
            playerScoreEl.textContent = gameState.playerScore;
            computerScoreEl.textContent = gameState.computerScore;
            roundsEl.textContent = gameState.rounds;
        }
        
        // Update history display
        function updateHistory() {
            if (gameState.history.length === 0) {
                historyListEl.innerHTML = 'No moves recorded yet';
                return;
            }
            
            historyListEl.innerHTML = '';
            
            gameState.history.slice(0, 5).forEach(entry => {
                const historyItem = document.createElement('div');
                historyItem.className = 'history-item';
                
                let resultClass = entry.result;
                let resultText = '';
                let resultIcon = '';
                
                if (entry.result === 'win') {
                    resultText = 'Win';
                    resultIcon = resultIcons.win;
                } else if (entry.result === 'lose') {
                    resultText = 'Lose';
                    resultIcon = resultIcons.lose;
                } else {
                    resultText = 'Draw';
                    resultIcon = resultIcons.draw;
                }
                
                historyItem.innerHTML = `
                    <span><strong>Round ${entry.round}:</strong> 
                    ${capitalize(entry.player)} vs ${capitalize(entry.computer)}</span>
                    <span class="${resultClass}">${resultText} <i class="fas ${resultIcon} result-icon"></i></span>
                `;
                
                historyListEl.appendChild(historyItem);
            });
        }
        
        // Capitalize string
        function capitalize(str) {
            return str.charAt(0).toUpperCase() + str.slice(1);
        }
        
        // Reset game
        function resetGame() {
            gameState.playerScore = 0;
            gameState.computerScore = 0;
            gameState.rounds = 0;
            gameState.history = [];
            
            playerChoiceEl.innerHTML = '<i class="fa-solid fa-question"></i>';
            computerChoiceEl.innerHTML = '<i class="fa-solid fa-question"></i>';
            resultTextEl.textContent = 'Select your move to begin';
            resultTextEl.className = 'result-text';
            
            choices.forEach(choice => choice.classList.remove('selected'));
            
            updateScores();
            updateHistory();
            
            // Add animation to reset button
            resetBtn.classList.add('pulse');
            setTimeout(() => {
                resetBtn.classList.remove('pulse');
            }, 400);
        }
        
        // Initialize the game
        initGame();
    </script>
</body>
</html>
				
			

Share this post

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top