DEV Community

Arhan Khan
Arhan Khan

Posted on

Build a Rock Paper Scissors Game with JavaScript

Rock Paper Scissors is a small game where you pick rock, paper, or scissors, and the computer picks one too. It is a great beginner project because it teaches you game logic, random choices, and score keeping — and the result is a game you can actually play with friends.

How does it work? The player clicks a button, the computer picks a random choice, and the app compares them with the classic rules: rock beats scissors, scissors beats paper, paper beats rock. The score updates after every round.

<!-- index.html -->
<h2>Choose your move</h2>
<button data-choice="rock">🪨 Rock</button>
<button data-choice="paper">📄 Paper</button>
<button data-choice="scissors">✂️ Scissors</button>
<p id="round">Make your first move!</p>
<p>Wins: <span id="wins">0</span> | Losses: <span id="losses">0</span> | Draws: <span id="draws">0</span></p>
Enter fullscreen mode Exit fullscreen mode
// script.js — the game logic in one small function
const choices = ['rock', 'paper', 'scissors'];
const beats = { rock: 'scissors', paper: 'rock', scissors: 'paper' };

let wins = 0;
let losses = 0;
let draws = 0;

const roundEl = document.getElementById('round');

document.querySelectorAll('button[data-choice]').forEach((btn) => {
  btn.addEventListener('click', () => {
    const player = btn.dataset.choice;
    const computer = choices[Math.floor(Math.random() * 3)];

    if (player === computer) {
      draws++;
      roundEl.textContent = `Both picked ${player} — it's a draw!`;
    } else if (beats[player] === computer) {
      wins++;
      roundEl.textContent = `${player} beats ${computer} — you win!`;
    } else {
      losses++;
      roundEl.textContent = `${computer} beats ${player} — computer wins!`;
    }

    document.getElementById('wins').textContent = wins;
    document.getElementById('losses').textContent = losses;
    document.getElementById('draws').textContent = draws;
  });
});
Enter fullscreen mode Exit fullscreen mode

This project teaches you the core of game programming: take an input, make a random choice, compare them with rules, and update the score. The same structure — input, random, compare, score — powers quiz apps, card games, and guessing games.

The limits are worth knowing. The computer picks randomly, so there is no strategy to learn from it — a real opponent reads your patterns. And the game is only fair because each choice has an equal one-in-three chance, which is exactly what Math.random() gives you.

Start today: build the game above, play ten rounds, and check your score. Then add one extra feature — a "best of 5" mode that declares a winner when someone reaches 3 wins.

Top comments (0)