Skip to content

Commit

Permalink
My-RPG init and setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivg2901 committed Dec 11, 2024
1 parent a86d44a commit 0236f6c
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 0 deletions.
77 changes: 77 additions & 0 deletions .gitignore
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 added My-RPG/README.md
Empty file.
21 changes: 21 additions & 0 deletions My-RPG/package.json
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"
}
41 changes: 41 additions & 0 deletions My-RPG/src/game-engine/index.js
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;
}
}
37 changes: 37 additions & 0 deletions My-RPG/src/index.js
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();
}
});
});

0 comments on commit 0236f6c

Please sign in to comment.