diff --git a/src/viking.js b/src/viking.js index 9017bfc8a..bebce7116 100755 --- a/src/viking.js +++ b/src/viking.js @@ -1,11 +1,108 @@ // Soldier -class Soldier {} +class Soldier { + constructor(health, strength) { + this.health = health; + this.strength = strength; + } + + attack() { + return this.strength; + } + + receiveDamage(damage) { + this.health -= damage; + } +} // Viking -class Viking {} +class Viking extends Soldier { + constructor(name, health, strength) { + super(health, strength); + this.name = name; + } + + receiveDamage(damage) { + this.health -= damage; + if (this.health > 0) + return `${this.name} has received ${damage} points of damage`; + else return `${this.name} has died in act of combat`; + } + + battleCry() { + return "Odin Owns You All!"; + } +} // Saxon -class Saxon {} +class Saxon extends Soldier { + receiveDamage(damage) { + this.health -= damage; + if (this.health > 0) + return `A Saxon has received ${damage} points of damage`; + else return `A Saxon has died in combat`; + } +} + +function getRandomIntInclusive(min, max) { + const minCeiled = Math.ceil(min); + const maxFloored = Math.floor(max); + return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled); // The maximum is inclusive and the minimum is inclusive +} // War -class War {} +class War { + constructor() { + this.vikingArmy = []; + this.saxonArmy = []; + } + addViking(vikingObj) { + this.vikingArmy.push(vikingObj); + } + addSaxon(saxonObj) { + this.saxonArmy.push(saxonObj); + } + + //getting an objects who is attacked (a Saxon or a Viking) and who attacks (Saxon or Viking) + attack(who, byWho) { + return who.receiveDamage(byWho.strength); + } + + getRandomViking() { + return this.vikingArmy[ + getRandomIntInclusive(0, this.vikingArmy.length - 1) + ]; + } + + getRandomSaxon() { + return this.saxonArmy[getRandomIntInclusive(0, this.saxonArmy.length - 1)]; + } + + removeDiedSoldier(soldier, army) { + const indexToRemove = army.indexOf(soldier); + army.splice(indexToRemove, 1); + } + + vikingAttack() { + const saxon = this.getRandomSaxon(); + const viking = this.getRandomViking(); + const resultOfAttack = this.attack(saxon, viking); + + if (resultOfAttack.includes("died")) + this.removeDiedSoldier(saxon, this.saxonArmy); + + return resultOfAttack; + } + + saxonAttack() { + const saxon = this.getRandomSaxon(); + const viking = this.getRandomViking(); + + const resultOfAttack = this.attack(viking, saxon); + + if (resultOfAttack.includes("died")) + this.removeDiedSoldier(viking, this.vikingArmy); + + return resultOfAttack; + } + showStatus() {} +} diff --git a/tests/viking.spec.js b/tests/viking.spec.js index 7db45122e..390719d2c 100755 --- a/tests/viking.spec.js +++ b/tests/viking.spec.js @@ -1,5 +1,4 @@ - -describe('Soldier', () => { +describe("Soldier", () => { let soldier; const strength = 150; const health = 300; @@ -8,44 +7,44 @@ describe('Soldier', () => { soldier = new Soldier(health, strength); }); - describe('class', () => { - it('should receive 2 arguments (health & strength)', () => { + describe("class", () => { + it("should receive 2 arguments (health & strength)", () => { expect(Soldier.length).toEqual(2); }); - it('should receive the health property as its 1st argument', () => { + it("should receive the health property as its 1st argument", () => { expect(soldier.health).toEqual(health); }); - it('should receive the strength property as its 2nd argument', () => { + it("should receive the strength property as its 2nd argument", () => { expect(soldier.strength).toEqual(strength); }); }); - describe('attack() method', () => { - it('should be a declared', () => { - expect(typeof soldier.attack).toBe('function'); + describe("attack() method", () => { + it("should be a declared", () => { + expect(typeof soldier.attack).toBe("function"); }); - it('should receive 0 arguments', () => { + it("should receive 0 arguments", () => { expect(soldier.attack.length).toEqual(0); }); - it('should return the strength property of the Soldier', () => { + it("should return the strength property of the Soldier", () => { expect(soldier.attack()).toEqual(strength); }); }); - describe('receiveDamage() method', () => { - it('should be a declared', () => { - expect(typeof soldier.receiveDamage).toBe('function'); + describe("receiveDamage() method", () => { + it("should be a declared", () => { + expect(typeof soldier.receiveDamage).toBe("function"); }); - it('should receive 1 argument (the damage)', () => { + it("should receive 1 argument (the damage)", () => { expect(soldier.receiveDamage.length).toEqual(1); }); - it('should remove the received damage from the health property', () => { + it("should remove the received damage from the health property", () => { soldier.receiveDamage(50); expect(soldier.health).toEqual(health - 50); }); @@ -56,9 +55,9 @@ describe('Soldier', () => { }); }); -describe('Viking', () => { +describe("Viking", () => { let viking; - const name = 'Harald'; + const name = "Harald"; const strength = 150; const health = 300; @@ -66,52 +65,52 @@ describe('Viking', () => { viking = new Viking(name, health, strength); }); - it('should inherit from Soldier', () => { + it("should inherit from Soldier", () => { expect(viking instanceof Soldier).toEqual(true); }); - describe('class', () => { - it('should receive 3 arguments (name, health & strength)', () => { + describe("class", () => { + it("should receive 3 arguments (name, health & strength)", () => { expect(Viking.length).toEqual(3); }); - it('should receive the name property as its 1st argument', () => { + it("should receive the name property as its 1st argument", () => { expect(viking.name).toEqual(name); }); - it('should receive the health property as its 2nd argument', () => { + it("should receive the health property as its 2nd argument", () => { expect(viking.health).toEqual(health); }); - it('should receive the strength property as its 3rd argument', () => { + it("should receive the strength property as its 3rd argument", () => { expect(viking.strength).toEqual(strength); }); }); - describe('attack() method', () => { - it('should be a declared', () => { - expect(typeof viking.attack).toBe('function'); + describe("attack() method", () => { + it("should be a declared", () => { + expect(typeof viking.attack).toBe("function"); }); - it('should receive 0 arguments', () => { + it("should receive 0 arguments", () => { expect(viking.attack.length).toEqual(0); }); - it('should return the strength property of the Viking', () => { + it("should return the strength property of the Viking", () => { expect(viking.attack()).toEqual(strength); }); }); - describe('receiveDamage() method', () => { - it('should be a declared', () => { - expect(typeof viking.receiveDamage).toBe('function'); + describe("receiveDamage() method", () => { + it("should be a declared", () => { + expect(typeof viking.receiveDamage).toBe("function"); }); - it('should receive 1 argument (the damage)', () => { + it("should receive 1 argument (the damage)", () => { expect(viking.receiveDamage.length).toEqual(1); }); - it('should remove the received damage from the health property', () => { + it("should remove the received damage from the health property", () => { viking.receiveDamage(50); expect(viking.health).toEqual(health - 50); }); @@ -120,7 +119,7 @@ describe('Viking', () => { expect(viking.receiveDamage(50)).toEqual( `${name} has received 50 points of damage` ); - + expect(viking.receiveDamage(75)).toEqual( `${name} has received 75 points of damage` ); @@ -133,22 +132,22 @@ describe('Viking', () => { }); }); - describe('battleCry() method', () => { - it('should be a declared', () => { - expect(typeof viking.battleCry).toBe('function'); + describe("battleCry() method", () => { + it("should be a declared", () => { + expect(typeof viking.battleCry).toBe("function"); }); - it('should receive 0 arguments', () => { + it("should receive 0 arguments", () => { expect(viking.battleCry.length).toEqual(0); }); it('should return "Odin Owns You All!"', () => { - expect(viking.battleCry()).toEqual('Odin Owns You All!'); + expect(viking.battleCry()).toEqual("Odin Owns You All!"); }); }); }); -describe('Saxon', () => { +describe("Saxon", () => { let saxon; const health = 60; const strength = 25; @@ -157,68 +156,68 @@ describe('Saxon', () => { saxon = new Saxon(health, strength); }); - it('should inherit from Soldier', () => { + it("should inherit from Soldier", () => { expect(saxon instanceof Soldier).toEqual(true); }); - describe('class', () => { - it('should receive the health property as its 1st argument', () => { + describe("class", () => { + it("should receive the health property as its 1st argument", () => { expect(saxon.health).toEqual(health); }); - it('should receive the strength property as its 2nd argument', () => { + it("should receive the strength property as its 2nd argument", () => { expect(saxon.strength).toEqual(strength); }); }); - describe('attack() method', () => { - it('should be a declared', () => { - expect(typeof saxon.attack).toBe('function'); + describe("attack() method", () => { + it("should be a declared", () => { + expect(typeof saxon.attack).toBe("function"); }); - it('should receive 0 arguments', () => { + it("should receive 0 arguments", () => { expect(saxon.attack.length).toEqual(0); }); - it('should return the strength property of the Saxon', () => { + it("should return the strength property of the Saxon", () => { expect(saxon.attack()).toEqual(strength); }); }); - describe('receiveDamage() method', () => { - it('should be a declared', () => { - expect(typeof saxon.receiveDamage).toBe('function'); + describe("receiveDamage() method", () => { + it("should be a declared", () => { + expect(typeof saxon.receiveDamage).toBe("function"); }); - it('should receive 1 argument (the damage)', () => { + it("should receive 1 argument (the damage)", () => { expect(saxon.receiveDamage.length).toEqual(1); }); - it('should remove the received damage from the health property', () => { + it("should remove the received damage from the health property", () => { saxon.receiveDamage(50); expect(saxon.health).toEqual(health - 50); }); it('should return "A Saxon has received DAMAGE points of damage", if the Saxon is still alive', () => { expect(saxon.receiveDamage(45)).toEqual( - 'A Saxon has received 45 points of damage' + "A Saxon has received 45 points of damage" ); expect(saxon.receiveDamage(10)).toEqual( - 'A Saxon has received 10 points of damage' + "A Saxon has received 10 points of damage" ); }); it('should return "A Saxon has died in combat", if the Saxon dies', () => { - expect(saxon.receiveDamage(health)).toEqual('A Saxon has died in combat'); + expect(saxon.receiveDamage(health)).toEqual("A Saxon has died in combat"); }); }); }); -describe('War', () => { +describe("War", () => { let viking, saxon, war; function generateViking() { - const name = 'Harald'; + const name = "Harald"; const strength = 150; const health = 300; return new Viking(name, health, strength); @@ -236,30 +235,30 @@ describe('War', () => { war = new War(); }); - describe('class', () => { - it('should receive 0 arguments', () => { + describe("class", () => { + it("should receive 0 arguments", () => { expect(War.length).toEqual(0); }); - it('should assign an empty array to the vikingArmy property', () => { + it("should assign an empty array to the vikingArmy property", () => { expect(war.vikingArmy).toEqual([]); }); - it('should assign an empty array to the saxonArmy property', () => { + it("should assign an empty array to the saxonArmy property", () => { expect(war.saxonArmy).toEqual([]); }); }); - describe('addViking() method', () => { - it('should be a declared', () => { - expect(typeof war.addViking).toBe('function'); + describe("addViking() method", () => { + it("should be a declared", () => { + expect(typeof war.addViking).toBe("function"); }); - it('should receive 1 argument (a Viking object)', () => { + it("should receive 1 argument (a Viking object)", () => { expect(war.addViking.length).toEqual(1); }); - it('should add the received Viking to the army', () => { + it("should add the received Viking to the army", () => { war.addViking(viking); expect(war.vikingArmy).toEqual([viking]); }); @@ -269,16 +268,16 @@ describe('War', () => { }); }); - describe('addSaxon() method', () => { - it('should be a declared', () => { - expect(typeof war.addSaxon).toBe('function'); + describe("addSaxon() method", () => { + it("should be a declared", () => { + expect(typeof war.addSaxon).toBe("function"); }); - it('should receive 1 argument (a Saxon object)', () => { + it("should receive 1 argument (a Saxon object)", () => { expect(war.addSaxon.length).toEqual(1); }); - it('should add the received Saxon to the army', () => { + it("should add the received Saxon to the army", () => { war.addSaxon(saxon); expect(war.saxonArmy).toEqual([saxon]); }); @@ -288,79 +287,79 @@ describe('War', () => { }); }); - describe('Armies Attack', () => { + describe("Armies Attack", () => { beforeEach(() => { war.addViking(viking); war.addSaxon(saxon); }); - describe('vikingAttack() method', () => { - it('should be a declared', () => { - expect(typeof war.vikingAttack).toBe('function'); + describe("vikingAttack() method", () => { + it("should be a declared", () => { + expect(typeof war.vikingAttack).toBe("function"); }); - it('should receive 0 arguments', () => { + it("should receive 0 arguments", () => { expect(war.vikingAttack.length).toEqual(0); }); - it('should make Saxon receiveDamage() equal to the strength of a Viking', () => { + it("should make Saxon receiveDamage() equal to the strength of a Viking", () => { let oldHealth = saxon.health; war.vikingAttack(); expect(saxon.health).toEqual(oldHealth - viking.strength); }); - it('should remove dead saxons from the army', () => { + it("should remove dead saxons from the army", () => { war.vikingAttack(); expect(war.saxonArmy.length).toEqual(0); }); - it('should return result of calling receiveDamage() of a Saxon with the strength of a Viking', () => { - expect(war.vikingAttack()).toEqual('A Saxon has died in combat'); + it("should return result of calling receiveDamage() of a Saxon with the strength of a Viking", () => { + expect(war.vikingAttack()).toEqual("A Saxon has died in combat"); }); }); - describe('saxonAttack() method', () => { - it('should be a declared', () => { - expect(typeof war.saxonAttack).toBe('function'); + describe("saxonAttack() method", () => { + it("should be a declared", () => { + expect(typeof war.saxonAttack).toBe("function"); }); - it('should receive 0 arguments', () => { + it("should receive 0 arguments", () => { expect(war.saxonAttack.length).toEqual(0); }); - it('should make a Viking receiveDamage() equal to the strength of a Saxon', () => { + it("should make a Viking receiveDamage() equal to the strength of a Saxon", () => { let oldHealth = viking.health; war.saxonAttack(); expect(viking.health).toEqual(oldHealth - saxon.strength); }); - it('should remove dead vikings from the army', () => { + it("should remove dead vikings from the army", () => { for (let i = 0; i < 12; i++) { war.saxonAttack(); } expect(war.vikingArmy.length).toEqual(0); }); - it('should return result of calling receiveDamage() of a Viking with the strength of a Saxon', () => { + it("should return result of calling receiveDamage() of a Viking with the strength of a Saxon", () => { expect(war.saxonAttack()).toEqual( `${viking.name} has received ${saxon.strength} points of damage` ); }); }); - describe('showStatus() method', () => { - it('should be a declared', () => { - expect(typeof war.showStatus).toBe('function'); + describe("showStatus() method", () => { + it("should be a declared", () => { + expect(typeof war.showStatus).toBe("function"); }); - it('should receive 0 arguments', () => { + it("should receive 0 arguments", () => { expect(war.showStatus.length).toEqual(0); }); it('should return "Vikings have won the war of the century!", if the Saxons array is empty', () => { war.vikingAttack(); expect(war.showStatus()).toEqual( - 'Vikings have won the war of the century!' + "Vikings have won the war of the century!" ); }); @@ -369,13 +368,13 @@ describe('War', () => { war.saxonAttack(); } expect(war.showStatus()).toEqual( - 'Saxons have fought for their lives and survived another day...' + "Saxons have fought for their lives and survived another day..." ); }); it('should return "Vikings and Saxons are still in the thick of battle.", if there are still both Vikings and Saxons', () => { expect(war.showStatus()).toEqual( - 'Vikings and Saxons are still in the thick of battle.' + "Vikings and Saxons are still in the thick of battle." ); }); });