From 5b147a1aa70e46298d2533cd314cb9d5f5d11694 Mon Sep 17 00:00:00 2001 From: jhalitaksoy Date: Wed, 30 Jun 2021 19:41:53 +0300 Subject: [PATCH] added ending game --- src/Home.tsx | 29 +++++++++++++------ src/components/BoardView.tsx | 2 +- src/mancala.ts | 54 ++++++++++++++++++++++++++---------- 3 files changed, 60 insertions(+), 25 deletions(-) diff --git a/src/Home.tsx b/src/Home.tsx index 73c642f..2ef6238 100644 --- a/src/Home.tsx +++ b/src/Home.tsx @@ -17,14 +17,17 @@ const testBoard = (): Board => { return board } -const testGame = new Game("0", "1", testBoard(), "player2") +const testGame = new Game("0", "1", testBoard(), "player2", "playing") const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => { const [userKey, setUserKey] = useState(undefined); const [game, setGame] = useState(undefined) + const [connectionState, setConnetionState] = useState<"connecting" | "error" | "connected">("connecting") + React.useEffect(() => { + context.userKeyStore.getUserKey((userKey: string) => { setUserKey(userKey) const rtmtws = context.rtmt as RTMTWS @@ -39,19 +42,19 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => { const newGame: Game = JSON.parse(decodeText(message)) console.log("on game update"); console.log(newGame); - setGame(new Game(newGame.player1, newGame.player2, newGame.board, newGame.turn)) + setGame(new Game(newGame.player1, newGame.player2, newGame.board, newGame.turn, newGame.state)) }) context.rtmt.listenMessage(channel_on_game_update, (message: Bytes) => { const newGame: Game = JSON.parse(decodeText(message)) console.log("on game update"); console.log(newGame); - setGame(new Game(newGame.player1, newGame.player2, newGame.board, newGame.turn)) + setGame(new Game(newGame.player1, newGame.player2, newGame.board, newGame.turn, newGame.state)) }) }, []) const onConnectionDone = () => { - + setConnetionState("connected") } const newGameClick = () => { @@ -60,7 +63,7 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => { const newGame: Game = JSON.parse(decodeText(message)) console.log("on_game_start"); console.log(newGame); - setGame(new Game(newGame.player1, newGame.player2, newGame.board, newGame.turn)) + setGame(new Game(newGame.player1, newGame.player2, newGame.board, newGame.turn, newGame.state)) }) } @@ -82,7 +85,7 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => { }}>
= ({ initial = 0 }) => { alignSelf: "stretch" }}>

Mancala

- {game && } + { + game && ( +
+ {game.state == "ended" && } + +
) + } + {!game && }
- {!game && } {game && ( <> -

{game.checkGameTurn(userKey) ? "Your Turn" : "Opponent Turn"}

+ {game.state == "ended" ?

Game ended

: +

{game.checkGameTurn(userKey) ? "Your Turn" : "Opponent Turn"}

+ } )} diff --git a/src/components/BoardView.tsx b/src/components/BoardView.tsx index 1f2abf4..866ff92 100644 --- a/src/components/BoardView.tsx +++ b/src/components/BoardView.tsx @@ -96,7 +96,7 @@ const BoardView: FunctionComponent<{ game?: Game, userKey: string, onHoleSelect: return (
0) { + ownStore.ballCount++ + stopHole.ballCount = 0; - ownStore.ballCount += opponentHole.ballCount - opponentHole.ballCount = 0 + ownStore.ballCount += opponentHole.ballCount + opponentHole.ballCount = 0 + } } } if (!checkIsOwnStore(stopIndex, circleSize)) { this.changeTurn() } + + const ownHoles = circle.slice(0, circle.length / 2 - 1) + + let sumBallCount = 0; + for (const hole of ownHoles) sumBallCount += hole.ballCount + + if (sumBallCount == 0) { + const opppentHoles = circle.slice(circle.length / 2, circle.length - 1) + let sumOppenentBallCount = 0; + for (const hole of opppentHoles) { + sumOppenentBallCount += hole.ballCount + hole.ballCount = 0 + } + getOwnStore(circle).ballCount += sumOppenentBallCount + this.state = "ended" + } } public changeTurn() { @@ -94,23 +114,23 @@ export class Game { } } - public clone() { return new Game(this.player1, this.player2, this.board, this.turn) } + public clone() { return new Game(this.player1, this.player2, this.board, this.turn, this.state) } //todo throw expection - public getPlayerNameByKey(key : string) : "player1" | "player2" { - if(this.player1 == key){ + public getPlayerNameByKey(key: string): "player1" | "player2" { + if (this.player1 == key) { return "player1" - }else{ + } else { return "player2" } } - public checkGameTurn(user : string) : boolean{ + public checkGameTurn(user: string): boolean { return this.getPlayerNameByKey(user) == this.turn } } -export function createGame( player1: string, player2: string){ +export function createGame(player1: string, player2: string) { // const board = new Board( // [new Hole(0), new Hole(1), new Hole(2), new Hole(3), new Hole(4), new Hole(5)], // [new Hole(0), new Hole(1), new Hole(2), new Hole(3), new Hole(4), new Hole(5)], @@ -119,7 +139,11 @@ export function createGame( player1: string, player2: string){ [new Hole(4), new Hole(4), new Hole(4), new Hole(4), new Hole(4), new Hole(4)], [new Hole(4), new Hole(4), new Hole(4), new Hole(4), new Hole(4), new Hole(4)], new Store(0), new Store(0)) - return new Game(player1, player2, board, "player1") + // const board = new Board( + // [new Hole(4), new Hole(4), new Hole(4), new Hole(4), new Hole(4), new Hole(1)], + // [new Hole(1), new Hole(0), new Hole(0), new Hole(0), new Hole(1), new Hole(0)], + // new Store(0), new Store(0)) + return new Game(player1, player2, board, "player1", "playing") } export class Hole { @@ -191,5 +215,5 @@ function getOpponentHoleByIndex(index: number, circle: Array) { } export interface GameMove { - index : number, + index: number, } \ No newline at end of file