generated from CodersCamp2020/CodersCamp2020.Project.TypeScript.Chess
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add GameHandler class * Fix GameHandler class and add tests * Fix tests
- Loading branch information
1 parent
b0b3a0f
commit 3a67f72
Showing
3 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters