From 502687615e11a1547c1ade0ac545aa8d8fc164f1 Mon Sep 17 00:00:00 2001 From: jhalitaksoy Date: Sat, 3 Jul 2021 15:43:51 +0300 Subject: [PATCH] added you won/lost --- src/Home.tsx | 4 ++-- src/mancala.ts | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/Home.tsx b/src/Home.tsx index 9fe794c..30c180e 100644 --- a/src/Home.tsx +++ b/src/Home.tsx @@ -145,7 +145,7 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => { { game && (
- {game.state == "ended" && } + {game.state == "ended" &&
) } @@ -154,7 +154,7 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => { {game && ( <> - {game.state == "ended" ?

{context.texts.GameEnded}

: + {game.state == "ended" ?

{context.texts.GameEnded + " " + game.getWonPlayer() == userKey ? context.texts.YouWon : context.texts.YouLost}

:

{game.checkGameTurn(userKey) ? context.texts.YourTurn : context.texts.OpponentTurn}

} diff --git a/src/mancala.ts b/src/mancala.ts index 5a8ea23..7eea963 100644 --- a/src/mancala.ts +++ b/src/mancala.ts @@ -65,6 +65,7 @@ export class Game { eachHole.ballCount++ } }) + hole.ballCount = holeEndBallCount console.log((stopIndex)); console.log(checkIsOwnHole(stopIndex, circleSize)); @@ -73,7 +74,7 @@ export class Game { //check if it was 0 before if (stopHole.ballCount == 1) { const ownStore = getOwnStore(circle) - const opponentHole = getOpponentHoleByIndex(stopIndex, circle) + const opponentHole = getOpponentHoleByIndex(stopIndex%16, circle) if (opponentHole.ballCount > 0) { ownStore.ballCount++ @@ -125,9 +126,31 @@ export class Game { } } + //todo throw expection + public getPlayerKeyByName(key: "player1" | "player2"): string { + if (this.player1 == key) { + return "player1" + } else { + return "player2" + } + } + public checkGameTurn(user: string): boolean { return this.getPlayerNameByKey(user) == this.turn } + + public getWonPlayer() : string | undefined { + const player1BallCount = this.board.player1Store.ballCount + const player2BallCount = this.board.player2Store.ballCount + + if(player1BallCount < player2BallCount){ + return this.getPlayerKeyByName("player1") + }else if(player1BallCount > player2BallCount) { + return this.getPlayerKeyByName("player2") + }else{ + return undefined + } + } } export function createGame(player1: string, player2: string) { @@ -182,6 +205,14 @@ export class Board { public player2Circle(): Array { return [...this.player2Holes, this.player2Store, ...this.player1Holes, this.player1Store] } + + public getBallCount(holes : Array ) : number { + let ballCount = 0 + for (let hole of holes) { + ballCount += hole.ballCount; + } + return ballCount + } } function iterateHoles(holes: Array, start: number, stop: number, callback: (hole: Hole, index: number) => void) {