-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
0aa2d9c
3a76c3e
e6fec66
ca6b13c
45ba47a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that factory can be global, like |
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}); | ||
}); | ||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||
} | ||||||||
|
||||||||
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; | ||||||||
} | ||||||||
} |
There was a problem hiding this comment.
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