UNPKG

salesman-minigames

Version:

A minigame library made with NodeJS.

263 lines (228 loc) 7.3 kB
// |----------------------------| // | -salesman games- | // | | Russian Roulette | | // |----------------------------| var tof = Math.random() < 0.5; /** * Class representing a Russian Roulette game result. */ class RussianRoulette { /** * Creates an instance of RussianRoulette. */ constructor() { this.winorlose = tof; this.stat = ""; this.stat_bool = null; this.run(); } /** * Processes the game result and sets status properties. * @returns {boolean} The survival status (true if survived, false if exploded). */ run() { if (this.winorlose) { this.stat_bool = true; this.stat = "Un-Exploded"; } else { this.stat_bool = false; this.stat = "Exploded"; } return this.winorlose; } /** * Gets the win or lose boolean value. * @returns {boolean} True if survived, false if exploded. */ getWinStat() { return this.winorlose; } /** * Gets the status as a string. * @returns {string} "Un-Exploded" if survived, "Exploded" if not. */ getStat() { return this.stat; } /** * Gets the status as a boolean. * @returns {boolean|null} True if survived, false if exploded, null if not set. */ getStatBool() { return this.stat_bool; } } // -----PART END---- // |-----------------------------| // | -salesman games- | // | | Rock Paper Scissors -1 | | // |-----------------------------| /** * Class representing Rock-Paper-Scissors Minus One game. */ class RPSMinusOne { /** * @param {string[]} p1Choices - Player 1's two choices (e.g. ["rock", "scissors"]) * @param {number} p1RemoveIndex - Player 1's index of choice to remove (0 or 1) * @param {string[]} p2Choices - Player 2's two choices (e.g. ["paper", "rock"]) * @param {number} p2RemoveIndex - Player 2's index of choice to remove (0 or 1) */ constructor(p1Choices, p1RemoveIndex, p2Choices, p2RemoveIndex) { this.p1Choices = p1Choices.map(c => c.toLowerCase()); this.p2Choices = p2Choices.map(c => c.toLowerCase()); this.p1RemoveIndex = p1RemoveIndex; this.p2RemoveIndex = p2RemoveIndex; this.winner = null; this.status = ""; this.validMoves = ["rock", "paper", "scissors"]; this.play(); } play() { if (!this._validateInputs()) { this.status = "Invalid input"; this.winner = null; return this.winner; } const p1Final = this._removeChoice(this.p1Choices, this.p1RemoveIndex); const p2Final = this._removeChoice(this.p2Choices, this.p2RemoveIndex); if (p1Final === p2Final) { this.winner = "draw"; this.status = "Draw"; return this.winner; } const beats = { rock: "scissors", paper: "rock", scissors: "paper" }; if (beats[p1Final] === p2Final) { this.winner = "player1"; this.status = `${p1Final} beats ${p2Final}`; } else { this.winner = "player2"; this.status = `${p2Final} beats ${p1Final}`; } return this.winner; } _validateInputs() { if (this.p1Choices.length !== 2 || this.p2Choices.length !== 2) return false; if (![0,1].includes(this.p1RemoveIndex) || ![0,1].includes(this.p2RemoveIndex)) return false; if (!this.p1Choices.every(c => this.validMoves.includes(c))) return false; if (!this.p2Choices.every(c => this.validMoves.includes(c))) return false; return true; } _removeChoice(choices, removeIndex) { return choices[1 - removeIndex]; } getWinner() { return this.winner; } getStatus() { return this.status; } } // -----PART END---- // |----------------------------| // | -salesman games- | // | | Dice Roll Challenge | | // |----------------------------| /** * Class representing a Dice Roll Challenge game. */ class DiceRollChallenge { /** * @param {number} p1Roll - Player 1's dice roll (1-6). * @param {number} p2Roll - Player 2's dice roll (1-6). */ constructor(p1Roll, p2Roll) { this.p1Roll = p1Roll; this.p2Roll = p2Roll; this.winner = null; this.status = ""; this.play(); } play() { if (!this._validateRoll(this.p1Roll) || !this._validateRoll(this.p2Roll)) { this.status = "Invalid roll"; this.winner = null; return this.winner; } if (this.p1Roll === this.p2Roll) { this.winner = "draw"; this.status = `Draw: Both rolled ${this.p1Roll}`; } else if (this.p1Roll > this.p2Roll) { this.winner = "player1"; this.status = `Player 1 wins with ${this.p1Roll}`; } else { this.winner = "player2"; this.status = `Player 2 wins with ${this.p2Roll}`; } return this.winner; } _validateRoll(roll) { return Number.isInteger(roll) && roll >= 1 && roll <= 6; } getWinner() { return this.winner; } getStatus() { return this.status; } } // -----PART END---- // |---------------------------| // | -salesman games- | // | | Coin Flip Duel | | // |---------------------------| /** * Class representing a Coin Flip Duel game. */ class CoinFlipDuel { /** * @param {string} p1Choice - Player 1's choice ("heads" or "tails"). * @param {string} p2Choice - Player 2's choice ("heads" or "tails"). */ constructor(p1Choice, p2Choice) { this.p1 = p1Choice.toLowerCase(); this.p2 = p2Choice.toLowerCase(); this.winner = null; this.status = ""; this.validChoices = ["heads", "tails"]; this.play(); } play() { if (!this.validChoices.includes(this.p1) || !this.validChoices.includes(this.p2)) { this.status = "Invalid input"; this.winner = null; return this.winner; } if (this.p1 === this.p2) { this.winner = "draw"; this.status = "Draw"; return this.winner; } if ((this.p1 === "heads" && this.p2 === "tails") || (this.p1 === "tails" && this.p2 === "heads")) { this.winner = "player1"; this.status = `Player 1 (${this.p1}) wins against Player 2 (${this.p2})`; } else { this.winner = "player2"; this.status = `Player 2 (${this.p2}) wins against Player 1 (${this.p1})`; } return this.winner; } getWinner() { return this.winner; } getStatus() { return this.status; } } // -----PART END---- //Exports(as mjs-modulejs) export { RussianRoulette, RPSMinusOne, DiceRollChallenge, CoinFlipDuel };