-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Node modules | ||
node_modules/ | ||
**/node_modules/ | ||
|
||
# Logs | ||
logs/ | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
|
||
# Environment variables | ||
.env | ||
.env.* | ||
**/.env | ||
**/.env.* | ||
|
||
# Build output | ||
dist/ | ||
build/ | ||
**/dist/ | ||
**/build/ | ||
|
||
# Dependency directories | ||
bower_components/ | ||
jspm_packages/ | ||
|
||
# Testing | ||
coverage/ | ||
**/coverage/ | ||
|
||
# IDE and editor files | ||
.idea/ | ||
.vscode/ | ||
*.swp | ||
*.swo | ||
*.sublime-workspace | ||
*.sublime-project | ||
|
||
# OS-specific files | ||
.DS_Store | ||
Thumbs.db | ||
|
||
# Optional lockfiles | ||
package-lock.json | ||
yarn.lock | ||
pnpm-lock.yaml | ||
|
||
# Temporary files | ||
*.tmp | ||
*.temp | ||
tmp/ | ||
**/tmp/ | ||
*.cache | ||
.cache/ | ||
|
||
# Specific tools or libraries | ||
.nyc_output/ | ||
*.tgz | ||
.parcel-cache/ | ||
.vite/ | ||
.next/ | ||
out/ | ||
|
||
# Generated files | ||
*.map | ||
*.tsbuildinfo | ||
**/*.tsbuildinfo | ||
|
||
# Static files | ||
public/static/ | ||
**/public/static/ | ||
|
||
# Ignore any files explicitly marked as "private" | ||
private/ | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "rpg-cli-game", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"description": "", | ||
"dependencies": { | ||
"chalk": "^5.3.0", | ||
"inquirer": "^12.2.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^5.1.2", | ||
"mocha": "^11.0.1" | ||
}, | ||
"type": "module" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import chalk from 'chalk'; | ||
|
||
export class GameEngine { | ||
constructor() { | ||
this.player = { name: '', hp: 100, level: 1, inventory: [] }; | ||
this.isOver = false; | ||
} | ||
|
||
setName(name) { | ||
if (name.trim()) { | ||
this.player.name = name.trim(); | ||
return `Welcome to the game, ${chalk.green.bold(this.player.name)}!`; | ||
} else { | ||
return "Name cannot be empty. Please provide a valid name."; | ||
} | ||
} | ||
|
||
handleCommand(command) { | ||
switch (command.toLowerCase()) { | ||
case 'help': | ||
return chalk.blue("Available commands: help, stats, explore, attack, quit"); | ||
case 'stats': | ||
return chalk.magenta( | ||
`Player Stats:\nName: ${chalk.bold(this.player.name || 'Unknown')}\nHP: ${this.player.hp}\nLevel: ${this.player.level}` | ||
); | ||
case 'explore': | ||
return chalk.yellow("You explore the area and find nothing... for now."); | ||
case 'attack': | ||
return chalk.red("You swing your weapon and deal 10 damage!"); | ||
case 'quit': | ||
this.isOver = true; | ||
return chalk.gray("Quitting the game."); | ||
default: | ||
return chalk.red("Unknown command. Type 'help' for options."); | ||
} | ||
} | ||
|
||
isGameOver() { | ||
return this.isOver; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import chalk from 'chalk'; | ||
import readline from 'readline'; | ||
import { GameEngine } from './game-engine/index.js'; | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
const game = new GameEngine(); | ||
|
||
console.log(chalk.green("Welcome to the RPG CLI Game!")); | ||
console.log(chalk.blue("Let's start by entering your name.")); | ||
|
||
rl.question(chalk.yellow("Enter your name: "), (name) => { | ||
const welcomeMessage = game.setName(name); | ||
console.log(chalk.cyan(welcomeMessage)); | ||
|
||
if (!game.player.name) { | ||
console.log(chalk.red("Name cannot be empty. Restart the game to try again.")); | ||
rl.close(); | ||
return; | ||
} | ||
|
||
console.log(chalk.green("Type 'help' to see the list of commands.")); | ||
|
||
rl.on('line', (input) => { | ||
const output = game.handleCommand(input.trim()); | ||
console.log(output); | ||
|
||
if (game.isGameOver()) { | ||
console.log(chalk.red("Game Over! Thanks for playing.")); | ||
rl.close(); | ||
} | ||
}); | ||
}); | ||
|