fight-club
For this challenge, you will create a JavaScript game that simulates a fight between two characters.
The fight is composed of multiple rounds, which run repeatedly while both characters have health remaining. In each round, characters will take turns attacking each other. The game ends when one character's health falls below zero.
deal with it 😎
Project Guidelines
- We recommend using Node.js to run JavaScript on your machine.
- Fork this repository and implement the
attackCharacter
,runRound
, andendGame()
functions infight.js
.
Characters
Each character is represented by the Character
class, where each character has properties for their name, health, attack and defense:
class Character {
constructor(name, health, attack, defense) {
this.name = name; // string
this.health = health; // number
this.attack = attack; // number
this.defense = defense; // number
}
}
var player = new Character('Edward Norton', 100, 25, 20);
var enemy = new Character('Tyler Durden', 100, 25, 20);
Attacking
The character class also has a prototype function that allows an instantiated character to attack another character:
Character.prototype.attackCharacter = function(defender) {}
You will be implementing the attackCharacter
prototype function. It accepts a Character
object as the defender
, and must satisfy these requirements:
- It determines the base damage, equal to the difference between attacker's attack and the defenders defense.
- It determines the random damage, equal to a random integer between 0 and 5, chosen using the
Math
library - It decrements the total damage from the
defender
's health - It logs
<attackerName> does <totalDamage> damage to <defenderName>
to the console
Example attackCharacter
output:
Edward Norton does 7 damage to Tyler Durden
Rounds
In addition to the attackCharacter
function, you must also implement the runRound
function. The runRound
function is the core component of the fight, and contains turn logic.
function runRound(round, p1, p2) {}
It accepts three arguments, the current round, and the two character objects.
The runRound
function must satisfy the following requirements:
- It logs
----- Round <round> -----
at the beginning of the round - It attacks
p2
withp1
- It attacks
p1
withp2
- It checks if the game has ended after each attack
- It will log each player's health in the format
<name> health: <health>
at the end of the round
Example runRound
output (including attackCharacter
output):
----- Begin Round 13 -----
Edward Norton does 6 damage to Tyler Durden
Tyler Durden does 8 damage to Edward Norton
Edward Norton health: 9
Tyler Durden health: 4
Ending the game
Once a character is out of health, the endGame
function is called:
function endGame(winner, loser) {}
The endGame
function accepts two character objects, and logs end-of-game info to the console in the format:
======== GAME OVER ========
<winnerName> wins against <loserName> with <winnerHealth> health remaining!
Example endGame
output:
======== GAME OVER ========
Edward Norton wins against Tyler Durden with 9 health remaining!