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

Max players #1

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion src/Players.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PlayerLimitReached } from './errors/PlayerLimitReached';
import { Player, PlayerName } from './Player';

export class Players {

public readonly players: Array<Player> = [];
private currentPlayerIndex: number = 0;

Expand All @@ -13,7 +13,13 @@ export class Players {
return this.players.length;
}

constructor(private readonly limit?: number) {}

public add(name: PlayerName): void {
if (this.limit && this.howMany === this.limit) {
throw new PlayerLimitReached();
}

this.players.push(new Player(name));
}

Expand Down
5 changes: 5 additions & 0 deletions src/errors/PlayerLimitReached.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class PlayerLimitReached extends Error {
constructor() {
super('The maximum number of players has been reached');
}
}
13 changes: 13 additions & 0 deletions src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,28 @@ import { Questions } from './Questions';

type ShouldProceed = boolean;

interface GameRules {
maxPlayers?: number;
}

export class Game implements AnyGame {

private rules: GameRules;
private players: Players = new Players();
private questions = new Questions();

private get currentPlayer(): Player {
return this.players.currentPlayer;
}

constructor(rules?: Partial<GameRules>) {
this.rules = {
...rules,
};

this.players = new Players(this.rules.maxPlayers);
}

public add(name: PlayerName): boolean {
this.players.add(name);

Expand Down
29 changes: 29 additions & 0 deletions tests/game.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { Game } from '../src/game';
import { PlayerName } from '../src/Player';
import { PlayerLimitReached } from '../src/errors/PlayerLimitReached';

describe("Game", () => {
describe("The maximum number of players", () => {
it("can be limited", () => {
const MAX_PLAYERS = 6;

const game = new Game({ maxPlayers: MAX_PLAYERS });

for (let i = 0; i < MAX_PLAYERS; i++) {
game.add(<PlayerName>`PLAYER ${i}`);
}

expect(() => game.add(<PlayerName>'EXTRA PLAYER')).to.throw(PlayerLimitReached);
})

it("can be unlimited", () => {
const game = new Game();

for (let i = 0; i < 1000; i++) {
expect(() => game.add(<PlayerName>`PLAYER ${i}`)).not.to.throw();
}
})
})
});