Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GameHandler class #14

Merged
merged 5 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"),
]);
}
Comment on lines +10 to +24
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if both players are the same - why bother to have two identical functions? :P


it("GameHandler.isGameFinished should return false if every player has at least one pokemon", () => {
// Given
const factory = new PokemonFactory(pokeData);
Copy link
Owner

@lukaszdutka lukaszdutka Feb 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that factory can be global, like getPlayerOne function.
Because using factory should be pure, factory.getPokemonByName should be (and is) pure function - it always returns the same (the same pokemon with same stats) for the same input (pokemon name)

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));
Comment on lines +48 to +49
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be in one line, but whatever :P

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();
Comment on lines +77 to +78
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it also can be in one line


// Then
expect(winner).toThrowError(
"You cannot get the winner. The game is not over."
);
Comment on lines +81 to +83
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it also can be in one line

});
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));
Comment on lines +94 to +95
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it also can be in one line

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;
Comment on lines +16 to +17
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can flex your programming skills and astonish your peers by using ternary operator:

Suggested change
if (this._currentPlayer === this._playerOne) return this._playerTwo;
return this._playerOne;
return this._currentPlayer === this._playerOne ? this._playerTwo : 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