kamranahmedse / developer-roadmap

Interactive roadmaps, guides and other educational content to help developers grow in their careers.
https://roadmap.sh
Other
297.98k stars 39.21k forks source link

Rock Paper Scissor #7734

Open CamruthaV opened 5 days ago

CamruthaV commented 5 days ago

What Roadmap is this project for?

Backend

Project Difficulty

Beginner

Add Project Details

Build a rock paper scissor game using random function

JawherKl commented 5 days ago

Suggested roadmap

You will build a Rock-Paper-Scissors game where the computer randomly selects a move, and the player’s move is compared against it to determine a winner. This project will focus on using JavaScript, Node.js, and backend fundamentals.

Project Objectives

Suggested Steps

  1. Setup: Initialize a Node.js project with basic setup and install necessary packages.
  2. Game Logic: Implement functions to generate random moves, receive user input, and compare the choices based on Rock-Paper-Scissors rules.
  3. Run and Test: Test the game locally to ensure the game logic works as expected.
  4. Extend (Optional): Add extra features like score tracking, or implement the game as a simple API for practice in backend requests.

Sample Code Outline


function getRandomMove() {
  const moves = ["rock", "paper", "scissors"];
  return moves[Math.floor(Math.random() * moves.length)];
}

function determineWinner(playerMove, computerMove) {
  if (playerMove === computerMove) return "Draw";
  if (
    (playerMove === "rock" && computerMove === "scissors") ||
    (playerMove === "scissors" && computerMove === "paper") ||
    (playerMove === "paper" && computerMove === "rock")
  ) {
    return "Player wins!";
  } else {
    return "Computer wins!";
  }
}

const playerMove = "rock"; // This could come from user input
const computerMove = getRandomMove();
console.log(`Player: ${playerMove}, Computer: ${computerMove}`);
console.log(determineWinner(playerMove, computerMove));