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!
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
- Rock crushes Scissors โ > โ๏ธ
- Scissors cuts Paper โ๏ธ > โ
- Paper covers Rock โ > โ
- ๐จ 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
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
- 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
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 --versiongit clone https://github.com/rishiverma12031/rock-paper-scissors.git
cd rock-paper-scissorsnpm install prompt-sync
npm install chalkOr install both at once:
npm install prompt-sync chalknode app.mjs- Start the game by running
node app.mjs - Enter your name when prompted
- Read the rules displayed on screen
- Make your choice by typing:
Rfor RockPfor PaperSfor Scissors
- Watch the results - Computer makes its choice and winner is announced
- Track your score - First to 3 points wins
- Play again? - Type
Yto restart or any other key to quit
- Handles each individual round
- Gets player input and generates computer choice
- Determines winner using switch statement and ternary operators
- Updates scores and displays results
- Main game loop
- Continues until either player or computer reaches 3 points
- Displays final results and winner
- Offers "play again" option
- Resets scores to 0
- Restarts the game loop
- Maintains player name
- โ 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
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
rock-paper-scissors/
โ
โโโ app.mjs # Main game file
โโโ package.json # Project dependencies (create this)
โโโ README.md # Documentation
If you don't have a package.json, create one:
npm init -yThen 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.
Want to enhance the game? Try these:
- Change colors in chalk functions
- Modify win condition (first to 5, 7, etc.)
- Add more detailed stats (win rate, total games)
- Add difficulty levels with weighted computer choices
- Include game history/log of all rounds
- Add sound effects using node packages
- Create a tournament mode
- 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
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
Error: "Cannot find module 'prompt-sync'"
npm install prompt-syncError: "Cannot find module 'chalk'"
npm install chalkError: "SyntaxError: Cannot use import statement outside a module"
- Make sure your file uses
.mjsextension, OR - Add
"type": "module"to yourpackage.json
Game doesn't accept input
- Ensure prompt-sync is installed correctly
- Try:
npm install prompt-sync --save
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
Contributions are welcome! To contribute:
- Fork the project
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add AmazingFeature') - Push to branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is open source and available under the MIT License.
Rishi Verma
- GitHub: @rishiverma12031
- Project Link: https://github.com/rishiverma12031/rock-paper-scissors
- 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
Ready to play? Run node app.mjs and challenge the computer! ๐ฎ
Made with โค๏ธ while learning JavaScript and Node.js