Skip to content

Commit

Permalink
MNG-95 Add GameHandler class (#14)
Browse files Browse the repository at this point in the history
* Add GameHandler class

* Fix GameHandler class and add tests

* Fix tests
  • Loading branch information
AleksandraCyp authored Feb 8, 2021
1 parent b0b3a0f commit 3a67f72
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
102 changes: 102 additions & 0 deletions src/__tests__/gameHandlerClass.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { GameHandler } from "../gameHandlerClass";
import { Player } from "../playerClass";
import { PokemonFactory } from "../pokemonFactory";
import { PokemonType } from "../pokemonClass";
import * as data from "../../assets/poke_data.json";

describe("Test Game Handler class", () => {

const pokeData = data.pokemons;
const getPlayerOne = (factory: PokemonFactory) => {
return new Player("Wojtek", [
factory.getPokemonByName("bulbasaur"),
factory.getPokemonByName("charmander"),
factory.getPokemonByName("pikachu"),
]);
}

const getPlayerTwo = (factory: PokemonFactory) => {
return new Player("Wojtek", [
factory.getPokemonByName("bulbasaur"),
factory.getPokemonByName("charmander"),
factory.getPokemonByName("pikachu"),
]);
}

it("GameHandler.isGameFinished should return false if every player has at least one pokemon", () => {
// Given
const factory = new PokemonFactory(pokeData);
const playerOne = getPlayerOne(factory);
const playerTwo = getPlayerTwo(factory);
const gameHandler = new GameHandler(playerOne, playerTwo);

// When
const isGameFinished = gameHandler.isGameFinished();

// Then
expect(isGameFinished).toBe(false);
});

it("GameHandler.isGameFinished should return true if all pokemons of one player are dead", () => {
// Given
const factory = new PokemonFactory(pokeData);
const playerOne = getPlayerOne(factory);
const playerTwo = getPlayerTwo(factory)
const gameHandler = new GameHandler(playerOne, playerTwo);

// When
playerOne.pokemons.forEach((pokemon) =>
pokemon.subtractHP(pokemon.maxHP));
const isGameFinished = gameHandler.isGameFinished();

// Then
expect(isGameFinished).toBe(true);
});
it("GameHandler.isGameFinished should return false if only some pokemons of a player are dead", () => {
// Given
const factory = new PokemonFactory(pokeData);
const playerOne = getPlayerOne(factory);
const playerTwo = getPlayerTwo(factory)
const gameHandler = new GameHandler(playerOne, playerTwo);

// When
playerTwo.pokemons[1].subtractHP(playerTwo.pokemons[1].maxHP)
const isGameFinished = gameHandler.isGameFinished();

// Then
expect(isGameFinished).toBe(false);
});
it("GameHandler.getWinner should return an error if the game is not finished", () => {
// Given
const factory = new PokemonFactory(pokeData);
const playerOne = getPlayerOne(factory);
const playerTwo = getPlayerTwo(factory)
const gameHandler = new GameHandler(playerOne, playerTwo);

// When
const winner = () =>
gameHandler.getWinner();

// Then
expect(winner).toThrowError(
"You cannot get the winner. The game is not over."
);
});
it("GameHandler.getWinner should return the winner if the game is finished", () => {
// Given
const factory = new PokemonFactory(pokeData);
const playerOne = getPlayerOne(factory);
const playerTwo = getPlayerTwo(factory)
const gameHandler = new GameHandler(playerOne, playerTwo);


// When
playerTwo.pokemons.forEach((pokemon) =>
pokemon.subtractHP(pokemon.maxHP));
const winner = gameHandler.getWinner();

// Then
expect(winner).toStrictEqual(playerOne);
});
});

38 changes: 38 additions & 0 deletions src/gameHandlerClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Player } from "./playerClass";

export class GameHandler {
constructor(
private readonly _playerOne: Player,
private readonly _playerTwo: Player
) {}

_currentPlayer: Player = this._playerOne;

getCurrentPlayer(): Player {
return this._currentPlayer;
}

getOpponentPlayer(): Player {
if (this._currentPlayer === this._playerOne) return this._playerTwo;
return this._playerOne;
}

private didPlayerLoose(player: Player): boolean {
return player.alivePokemons.length === 0;
}

isGameFinished(): boolean {
return (
this.didPlayerLoose(this._playerOne) ||
this.didPlayerLoose(this._playerTwo)
);
}

getWinner(): Player | Error {
if (!this.isGameFinished())
throw new Error("You cannot get the winner. The game is not over.");
return this.didPlayerLoose(this._playerOne) === true
? this._playerTwo
: this._playerOne;
}
}
4 changes: 4 additions & 0 deletions src/pokemonClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export class Pokemon {
return this._currentHP;
}

get maxHP() {
return this._maxHP;
}

subtractHP(pointsToSubtract: number): void {
const newHP = this._currentHP - pointsToSubtract;
if (newHP < 0) {
Expand Down

0 comments on commit 3a67f72

Please sign in to comment.