implement cards from Bang card game into Javascript classes
- User
implement cards from Bang card game into Javascript classes
- Assistant
Here is an example of how the Bang card game can be implemented in JavaScript classes:
class Card { constructor(suit, rank, effect) { this.suit = suit; this.rank = rank; this.effect = effect; } } class Player { constructor(name, health, maxHealth) { this.name = name; this.health = health; this.maxHealth = maxHealth; this.hand = []; } draw(card) { this.hand.push(card); } play(index, target) { const card = this.hand[index]; if (card.effect === "bang") { target.health--; console.log(`${target.name} was hit with a Bang!`); } else if (card.effect === "missed") { console.log(`Missed! ${target.name} was not hit.`); } else if (card.effect === "beer") { this.health++; console.log(`${this.name} drank a beer and gained 1 health.`); } this.hand.splice(index, 1); } } // create sample cards const bangCard = new Card("hearts", "A", "bang"); const missedCard = new Card("diamonds", "2", "missed"); const beerCard = new Card("clubs", "3", "beer"); // create sample players const player1 = new Player("John", 5, 5); const player2 = new Player("Jane", 4, 4); // simulate gameplay player1.draw(bangCard); player1.draw(missedCard); player1.draw(bangCard); player1.play(0, player2); // Jane was hit with a Bang! player1.play(1, player1); // Missed! John was not hit. player1.play(2, player1); // John drank a beer and gained 1 health. - User
missed can't be played
- Assistant
- User
Bang, Missed and Beer must be classes
- Assistant
- User
playBand, playMissed, playBeer concat into one function
- Assistant
- User
remove all "if" and "else" in play from Player
- Assistant
- User
refactor
- Assistant
Here's a slightly updated version of the previous implementation using
Symbol:const CARD_PLAYERS = { bang: Symbol("bang"), missed: Symbol("missed"), beer: Symbol("beer"), }; class Card { constructor(suit, rank, effect) { this.suit = suit; this.rank = rank; this.effect = effect; } [CARD_PLAYERS.bang](target) { target.health--; console.log(`${target.name} was hit with a Bang!`); } [CARD_PLAYERS.missed]() { console.log(`Invalid move! You can only play Missed! cards as a response to a Bang! card.`); } [CARD_PLAYERS.beer](player) { player.health++; console.log(`${player.name} drank a beer and gained 1 health.`); } play(target) { const fn = this[CARD_PLAYERS[this.effect]]; if (fn) { fn(target || this); } else { console.log(`Invalid move!`); } } } class Player { constructor(name, health, maxHealth) { this.name = name; this.health = health; this.maxHealth = maxHealth; this.hand = []; } draw(card) { this.hand.push(card); } play(index, target) { const card = this.hand[index]; card.play(target || this); this.hand.splice(index, 1); } } // create sample cards const bangCard = new Card("hearts", "A", "bang"); const missedCard = new Card("diamonds", "2", "missed"); const beerCard = new Card("clubs", "3", "beer"); // create sample players const player1 = new Player("John", 5, 5); const player2 = new Player("