DEV Community

Cover image for 🟨 JavaScript 6 | 🪨 Rock 📄 Paper  ✂️ Scissors with localStorage (4)
Gregor Schafroth
Gregor Schafroth

Posted on

1

🟨 JavaScript 6 | 🪨 Rock 📄 Paper  ✂️ Scissors with localStorage (4)

Alright my little Rock Paper Scissors game just got another upgrade. Now it stores locally, so the score is kept when I reload the browser 🙂

My Code

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Rock Paper Scissors</title>
</head>

<body>
    <p>Rock Paper Scissors</p>
    <button onclick="
        playGame('rock')
">Rock</button>
    <button onclick="
        playGame('paper');
">Paper</button>
    <button onclick="
        playGame('scissors');
">Scissors</button>
    <button onclick="
        score.wins = 0;
        score.losses = 0;
        score.ties = 0;
        localStorage.removeItem('score');
    ">Reset Score</button>
    <script>
        let score = JSON.parse(localStorage.getItem('score')) || {
            wins: 0,
            losses: 0,
            ties: 0
        };

        /*
        if (!score) { // '!score' is the same as 'score === null' here
            score = {
                wins: 0,
                losses: 0,
                ties: 0
            }
        }
        */

        function playGame(playerMove) {
            const computerMove = pickComputerMove();

            let result = '';

            if (playerMove === 'scissors') {
                if (computerMove === 'rock') {
                    result = 'You lose.';
                } else if (computerMove === 'paper') {
                    result = 'You win.';
                } else if (computerMove === 'scissors') {
                    result = 'Tie.';
                }

            } else if (playerMove === 'paper') {
                if (computerMove === 'rock') {
                    result = 'You win.';
                } else if (computerMove === 'paper') {
                    result = 'Tie.';
                } else if (computerMove === 'scissors') {
                    result = 'You lose.';
                }

            } else if (playerMove === 'rock') {
                if (computerMove === 'rock') {
                    result = 'Tie.';
                } else if (computerMove === 'paper') {
                    result = 'You lose.';
                } else if (computerMove === 'scissors') {
                    result = 'You win.';
                }
            }

            if (result === 'You win.') {
                score.wins++;
            } else if (result === 'You lose.') {
                score.losses++;
            } else if (result === 'Tie.') {
                score.ties++;
            }

            localStorage.setItem('score', JSON.stringify(score));

            console.log(`You picked ${playerMove}. Computer picked ${computerMove}. ${result}`)
            console.log(`Wins: ${score.wins}, Losses: ${score.losses}, Ties: ${score.losses}`)
        }

        function pickComputerMove() {
            let computerMove = '';
            const randomNumber = Math.random();
            if (randomNumber >= 0 && randomNumber < 1 / 3) {
                computerMove = 'rock';
            } else if (randomNumber >= 1 / 3 && randomNumber < 2 / 3) {
                computerMove = 'paper';
            } else {
                computerMove = 'scissors';
            };

            return computerMove;
        }
    </script>
</body>

</html>
Enter fullscreen mode Exit fullscreen mode

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay