Skip to content

rishiverma12031/rock-paper-scissors

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

9 Commits
ย 
ย 
ย 
ย 

Repository files navigation

Rock Paper Scissors CLI Game ๐Ÿชจ๐Ÿ“„โœ‚๏ธ

A fun command-line Rock-Paper-Scissors game built with Node.js. Play against the computer in your terminal with colorful output and interactive gameplay. First to 3 points wins!

Node.js JavaScript CLI

๐ŸŽฎ About The Game

This is a terminal-based implementation of the classic Rock-Paper-Scissors game where you compete against a computer opponent. The game features:

  • Best of 5 format: First to score 3 points wins
  • Colorful terminal output: Using Chalk for enhanced visual experience
  • Interactive prompts: Real-time user input with prompt-sync
  • Score tracking: Live scoreboard updated after each round
  • Play again feature: Quick restart without relaunching

Game Rules

  • Rock crushes Scissors โœŠ > โœŒ๏ธ
  • Scissors cuts Paper โœŒ๏ธ > โœ‹
  • Paper covers Rock โœ‹ > โœŠ

โœจ Features

  • ๐ŸŽจ Colorful CLI Output - Beautiful colored text using Chalk
  • ๐ŸŽฏ Smart Input Handling - Type R, P, or S for quick play
  • ๐Ÿ“Š Live Score Tracking - Real-time scoreboard display
  • ๐Ÿค– Random Computer AI - Unpredictable computer choices
  • ๐Ÿ”„ Instant Replay - Quick "play again" option
  • โœ… Input Validation - Handles invalid inputs gracefully
  • ๐Ÿ† Win Conditions - First to 3 points wins the match
  • ๐Ÿ’ฌ Player Personalization - Enter your name for a custom experience

๐Ÿ“ธ Demo

Are you ready to play rock paper scissors?

Enter your name: Rishi
Welcome to the game Rishi!

Rules of the Game: 
-> Type R, P, or S to enter your choice
-> "R" stands for "Rock"
-> "P" stands for "Paper"
-> "S" stands for "Scissors"
-> Rock beats Scissors, Scissors beats Paper, and Paper beats Rock
-> Whoever scores 3 points first, will win the game!

Enjoy!

Score:
Rishi: 0 || Computer: 0

Enter your choice: R
You have chosen Rock , The computer have chosen Scissors
Rishi wins!
Rishi: 1 || Computer: 0

๐Ÿ› ๏ธ Built With

  • Node.js - JavaScript runtime environment
  • ES6 Modules (.mjs) - Modern JavaScript module syntax
  • prompt-sync - Synchronous user input in Node.js
  • chalk - Terminal string styling and colors

๐Ÿ“‹ Prerequisites

Before running this game, make sure you have:

  • Node.js (v12.0.0 or higher)
  • npm (Node Package Manager)

Check your versions:

node --version
npm --version

๐Ÿš€ Installation & Setup

1. Clone the Repository

git clone https://github.com/rishiverma12031/rock-paper-scissors.git
cd rock-paper-scissors

2. Install Dependencies

npm install prompt-sync
npm install chalk

Or install both at once:

npm install prompt-sync chalk

3. Run the Game

node app.mjs

๐ŸŽฏ How to Play

  1. Start the game by running node app.mjs
  2. Enter your name when prompted
  3. Read the rules displayed on screen
  4. Make your choice by typing:
    • R for Rock
    • P for Paper
    • S for Scissors
  5. Watch the results - Computer makes its choice and winner is announced
  6. Track your score - First to 3 points wins
  7. Play again? - Type Y to restart or any other key to quit

๐Ÿ’ป Code Structure

Main Components

playTurn() Function

  • Handles each individual round
  • Gets player input and generates computer choice
  • Determines winner using switch statement and ternary operators
  • Updates scores and displays results

playGame() Function

  • Main game loop
  • Continues until either player or computer reaches 3 points
  • Displays final results and winner
  • Offers "play again" option

playAgain() Function

  • Resets scores to 0
  • Restarts the game loop
  • Maintains player name

Key Programming Concepts Used

  • โœ… ES6 Modules - Modern import/export syntax
  • โœ… Switch Statements - For handling player input
  • โœ… Ternary Operators - Compact conditional logic
  • โœ… While Loops - Game continues until win condition
  • โœ… Math.random() - Random computer choice generation
  • โœ… Array Manipulation - Computer choice array
  • โœ… String Methods - trim() and toUpperCase() for input handling
  • โœ… Template Literals - Dynamic string formatting

๐ŸŽจ Color Scheme

The game uses Chalk for terminal colors:

  • ๐ŸŸฃ Magenta - Welcome messages and results
  • ๐Ÿ”ต Blue - Player name prompt
  • ๐ŸŸข Green - Player wins and welcome
  • ๐Ÿ”ด Red - Computer wins
  • ๐ŸŸก Yellow - Rules and draw messages
  • ๐Ÿ”ท Cyan - Score display

๐Ÿ“ Project Structure

rock-paper-scissors/
โ”‚
โ”œโ”€โ”€ app.mjs              # Main game file
โ”œโ”€โ”€ package.json         # Project dependencies (create this)
โ””โ”€โ”€ README.md           # Documentation

Creating package.json

If you don't have a package.json, create one:

npm init -y

Then add to your package.json:

{
  "type": "module",
  "dependencies": {
    "prompt-sync": "^4.2.0",
    "chalk": "^4.1.2"
  }
}

Note: Use Chalk v4.x for CommonJS compatibility, or Chalk v5.x if using pure ESM.

๐Ÿ”ง Customization Ideas

Want to enhance the game? Try these:

Easy Modifications

  • Change colors in chalk functions
  • Modify win condition (first to 5, 7, etc.)
  • Add more detailed stats (win rate, total games)

Medium Challenges

  • Add difficulty levels with weighted computer choices
  • Include game history/log of all rounds
  • Add sound effects using node packages
  • Create a tournament mode

Advanced Features

  • Implement Rock-Paper-Scissors-Lizard-Spock variant
  • Add AI that learns from your patterns
  • Create multiplayer mode (two players on same machine)
  • Save high scores to a file
  • Build a GUI version with Electron

๐ŸŽ“ Learning Outcomes

This project demonstrates:

  • Node.js Fundamentals - Running JavaScript outside the browser
  • NPM Package Management - Installing and using third-party packages
  • Game Logic Implementation - Writing rules and win conditions
  • User Input Handling - Synchronous prompts in CLI
  • Control Flow - Loops, switches, and conditionals
  • ES6+ Features - Modern JavaScript syntax and patterns
  • Error Handling - Validating user input
  • Code Organization - Breaking code into reusable functions

๐Ÿ› Troubleshooting

Common Issues

Error: "Cannot find module 'prompt-sync'"

npm install prompt-sync

Error: "Cannot find module 'chalk'"

npm install chalk

Error: "SyntaxError: Cannot use import statement outside a module"

  • Make sure your file uses .mjs extension, OR
  • Add "type": "module" to your package.json

Game doesn't accept input

  • Ensure prompt-sync is installed correctly
  • Try: npm install prompt-sync --save

๐ŸŒŸ Future Enhancements

Planned features:

  • Add persistent leaderboard/stats
  • Implement Rock-Paper-Scissors-Lizard-Spock
  • Add ASCII art for choices
  • Create difficulty levels
  • Add game timer/speed mode
  • Include sound effects
  • Build GUI version with Electron
  • Add multiplayer over network
  • Create web-based version

๐Ÿค Contributing

Contributions are welcome! To contribute:

  1. Fork the project
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add AmazingFeature')
  4. Push to branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ“ License

This project is open source and available under the MIT License.

๐Ÿ‘ค Author

Rishi Verma

๐Ÿ™ Acknowledgments

  • Built to practice JavaScript fundamentals and Node.js
  • Uses prompt-sync for user input
  • Uses chalk for terminal colors
  • Inspired by the timeless rock-paper-scissors game

๐Ÿ“š Resources


Ready to play? Run node app.mjs and challenge the computer! ๐ŸŽฎ

Made with โค๏ธ while learning JavaScript and Node.js

About

Interactive Rock-Paper-Scissors CLI game built with Node.js. Features colorful terminal output, score tracking, and best-of-5 gameplay. First to 3 wins! ๐Ÿชจ๐Ÿ“„โœ‚๏ธ

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors