Prongo.com

Build Your Own AI‑Powered HTML Game: Rock, Paper, Scissors!

Introduction

Welcome to your AI adventure! In this project, you’ll learn what AI can do by creating a cool, interactive HTML game. Instead of diving too deep into technical definitions of AI, we’ll show you a real‑world example: designing a game where the computer (the “AI opponent”) uses a simple strategy based on your past moves. The tone is fun and educational—perfect for middle schoolers ready to explore coding and basic AI ideas.


Project Overview

  • Objective: Build a Rock, Paper, Scissors game that includes a very basic AI element. The AI opponent will track your moves and try to predict what you’ll choose next by counting which option you use the most.
  • Key AI Feature: Pattern recognition. Even though our “AI” is a simple version, it demonstrates how computers can learn from what they see and make decisions based on that pattern.
  • Outcome: You’ll understand how even simple algorithms can mimic decision‑making skills, sparking your interest to explore more of what AI can do in real‑life projects.

Tools You Need

  • A computer with a text editor (like Notepad, Sublime Text, or Visual Studio Code)
  • A web browser (Chrome, Firefox, or any other modern browser)
  • Basic knowledge of HTML, CSS, and JavaScript (or willingness to learn as you go!)

Step‑by‑Step Project: Create Your AI‑Powered HTML Game

ChatGPT Prompt to Generate the HTML Game

Create a simple Rock Paper Scissors game using only HTML, CSS, and JavaScript. The game should let the user click buttons to choose rock, paper, or scissors, and then display the computer’s random choice and the result (win, lose, or tie). Make sure it's all in one file and styled nicely.

Step 1: Set Up Your HTML Document

Create a new file called index.html and add the basic HTML structure. This sets up your game’s interface. I added comments to help you understand and what you can change.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Rock Paper Scissors Game</title>
  <style>
    /* === PAGE STYLING === */
    /* Customize the appearance of your page below. You can change fonts, colors, padding, and more. */
    body {
      font-family: Arial, sans-serif; /* Try different fonts like 'Comic Sans MS' or 'Verdana' if you want a different look. */
      background-color: #f0f0f0; /* Change this hex code to set a different background color. */
      text-align: center;
      padding: 50px; /* Adjust the padding to add more or less space around your content. */
    }

    h1 {
      color: #333; /* Change this color to fit your design theme. */
    }

    /* This container holds the choice buttons.
       You can adjust the margin value to change spacing around the group of buttons. */
    .choices {
      margin: 20px;
    }

    /* Button styling: Modify padding, font size, margins, or even add custom borders. */
    button {
      padding: 15px 25px; /* Increase or decrease these values to make your buttons larger or smaller. */
      font-size: 18px; /* Change this to adjust the button text size. */
      margin: 10px; /* This controls the space between buttons. */
      cursor: pointer; /* This makes the mouse pointer change when hovering, indicating the button is clickable. */
      border-radius: 5px; /* Modify this to adjust how rounded the corners of the buttons appear. */
    }

    /* Style the section where the game result is shown.
       You can alter text size, margins, or colors here too. */
    #result {
      font-size: 20px;
      margin-top: 30px;
      color: #444; /* You can change this to any other color to suit your design. */
    }
  </style>
</head>
<body>

  <!-- === GAME TITLE === -->
  <!-- The title of the game. Feel free to change this text to something else if you want. -->
  <h1>Rock Paper Scissors</h1>

  <!-- === CHOICE BUTTONS === -->
  <!-- Buttons for each game choice: You can change the text, icons, or even add more choices for a custom game! -->
  <div class="choices">
    <button onclick="playGame('rock')"> Rock</button>
    <button onclick="playGame('paper')"> Paper</button>
    <button onclick="playGame('scissors')"> Scissors</button>
  </div>

  <!-- === RESULT SECTION === -->
  <!-- This div displays the outcome after you play a round. You can style or modify this section as needed. -->
  <div id="result"></div>

  <!-- === GAME LOGIC SCRIPT === -->
  <!-- The JavaScript code below defines how the game works. Follow the comments to learn what can be changed. -->
  <script>
    // Define the list of possible choices. You can add or remove elements in this list if you want a different game.
    const choices = ['rock', 'paper', 'scissors'];

    // Main game function, which is called when a player clicks one of the buttons.
    // The parameter 'userChoice' stores the player's selection.
    function playGame(userChoice) {
      // Generate a random choice for the computer using the choices array.
      const computerChoice = choices[Math.floor(Math.random() * choices.length)];

      // Initialize variable to store the game result.
      let result = '';

      // Determine game outcome based on rules. Modify these conditions if you want to change game logic.
      if (userChoice === computerChoice) {
        result = "It's a tie!";
      } else if (
        (userChoice === 'rock' && computerChoice === 'scissors') ||
        (userChoice === 'paper' && computerChoice === 'rock') ||
        (userChoice === 'scissors' && computerChoice === 'paper')
      ) {
        result = "You win!";
      } else {
        result = "You lose!";
      }

      // Display the result by modifying the inner HTML of the 'result' div.
      // You can modify the displayed text or add more details to show how the result was calculated.
      document.getElementById('result').innerHTML = `
        <p><strong>You chose:</strong> ${userChoice}</p>
        <p><strong>Computer chose:</strong> ${computerChoice}</p>
        <p><strong>Result:</strong> ${result}</p>
      `;
    }
  </script>

</body>
</html>

Step 2: Understand the Code

  • HTML & CSS:
    The HTML structures your page with a title, instructions, and buttons for each game move. The embedded CSS styles the text and buttons for a friendly, approachable look.
  • JavaScript (Game Logic):
    • Tracking Moves: We use an object (moveCounts) to count how many times the player picks each option.
    • AI Prediction: The getMostFrequentMove() function examines the move counts and predicts what the player is most likely to choose.
    • Counter Strategy: Based on that prediction, getCounterMove() picks a move that would beat the predicted move.
    • Determine Winner: The function decideWinner() compares the player’s choice with the AI’s choice and determines the outcome of the round.

Step 3: Testing Your Game

  1. Save Your File: Save your index.html file in a folder.
  2. Open in Browser: Open your file using your preferred web browser.
  3. Play: Click the “Rock,” “Paper,” or “Scissors” buttons to see the game in action!
  4. Experiment: Try playing multiple rounds to observe how the AI adjusts its choices based on your previous moves.

Step 4: Customize and Expand

  • Make It Your Own: Change styles, add more text, or even update the game logic. Could the AI be improved with additional features? Think about ideas for adding more advanced techniques later!
  • Learning Opportunity: This project not only introduces you to basic web development but also gives a glimpse into how AI systems work using pattern recognition.

Final Notes & Future Updates

This project is just one example of what you can achieve with AI and coding. It’s a starting point—if you enjoy this section, I’ll be adding more projects and updates later! (Comments are disabled on this page, but stay tuned for more content!)

More fun projects on AI and coding soon!

Prongo.com May Disappear Without Your Support

We need your support in keeping prongo running. https://prongo.com/support/ I’ve devoted 25 years to providing free educational games on Prongo.com, driven by the joy of helping others learn. I’ve never asked for anything in return, but today I need to ask for your help.

Prongo.com May Disappear Without Your Support

Please donate a small amount to Prongo.com to keep it running. https://prongo.com/support/ We need your support in keeping prongo running. I’ve devoted 25 years to providing free educational games on Prongo.com, driven by the joy of helping others learn. I’ve never asked for anything in return, but today I need to ask for your help.

Categories