added ending game
This commit is contained in:
parent
c4c0044985
commit
5b147a1aa7
27
src/Home.tsx
27
src/Home.tsx
@ -17,14 +17,17 @@ const testBoard = (): Board => {
|
|||||||
return 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 Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
|
||||||
const [userKey, setUserKey] = useState(undefined);
|
const [userKey, setUserKey] = useState(undefined);
|
||||||
|
|
||||||
const [game, setGame] = useState<Game>(undefined)
|
const [game, setGame] = useState<Game>(undefined)
|
||||||
|
|
||||||
|
const [connectionState, setConnetionState] = useState<"connecting" | "error" | "connected">("connecting")
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
|
|
||||||
context.userKeyStore.getUserKey((userKey: string) => {
|
context.userKeyStore.getUserKey((userKey: string) => {
|
||||||
setUserKey(userKey)
|
setUserKey(userKey)
|
||||||
const rtmtws = context.rtmt as RTMTWS
|
const rtmtws = context.rtmt as RTMTWS
|
||||||
@ -39,19 +42,19 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
|
|||||||
const newGame: Game = JSON.parse(decodeText(message))
|
const newGame: Game = JSON.parse(decodeText(message))
|
||||||
console.log("on game update");
|
console.log("on game update");
|
||||||
console.log(newGame);
|
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) => {
|
context.rtmt.listenMessage(channel_on_game_update, (message: Bytes) => {
|
||||||
const newGame: Game = JSON.parse(decodeText(message))
|
const newGame: Game = JSON.parse(decodeText(message))
|
||||||
console.log("on game update");
|
console.log("on game update");
|
||||||
console.log(newGame);
|
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 = () => {
|
const onConnectionDone = () => {
|
||||||
|
setConnetionState("connected")
|
||||||
}
|
}
|
||||||
|
|
||||||
const newGameClick = () => {
|
const newGameClick = () => {
|
||||||
@ -60,7 +63,7 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
|
|||||||
const newGame: Game = JSON.parse(decodeText(message))
|
const newGame: Game = JSON.parse(decodeText(message))
|
||||||
console.log("on_game_start");
|
console.log("on_game_start");
|
||||||
console.log(newGame);
|
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 }) => {
|
|||||||
}}>
|
}}>
|
||||||
<div style={{
|
<div style={{
|
||||||
padding: "0px 50px",
|
padding: "0px 50px",
|
||||||
background : "rgb(228, 228, 228)",
|
background: "rgb(228, 228, 228)",
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
flexDirection: "row",
|
flexDirection: "row",
|
||||||
alignItems: "center",
|
alignItems: "center",
|
||||||
@ -90,13 +93,21 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
|
|||||||
alignSelf: "stretch"
|
alignSelf: "stretch"
|
||||||
}}>
|
}}>
|
||||||
<h1>Mancala</h1>
|
<h1>Mancala</h1>
|
||||||
{game && <button onClick={leaveGame}>Leave</button>}
|
{
|
||||||
</div>
|
game && (
|
||||||
|
<div>
|
||||||
|
{game.state == "ended" && <button onClick={newGameClick}>New Game</button>}
|
||||||
|
<button onClick={leaveGame}>Leave</button>
|
||||||
|
</div>)
|
||||||
|
}
|
||||||
{!game && <button onClick={newGameClick}>New Game</button>}
|
{!game && <button onClick={newGameClick}>New Game</button>}
|
||||||
|
</div>
|
||||||
|
|
||||||
{game && (
|
{game && (
|
||||||
<>
|
<>
|
||||||
|
{game.state == "ended" ? <h4>Game ended</h4> :
|
||||||
<h4>{game.checkGameTurn(userKey) ? "Your Turn" : "Opponent Turn"}</h4>
|
<h4>{game.checkGameTurn(userKey) ? "Your Turn" : "Opponent Turn"}</h4>
|
||||||
|
}
|
||||||
<BoardView userKey={userKey} game={game} onHoleSelect={onHoleSelect} />
|
<BoardView userKey={userKey} game={game} onHoleSelect={onHoleSelect} />
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@ -96,7 +96,7 @@ const BoardView: FunctionComponent<{ game?: Game, userKey: string, onHoleSelect:
|
|||||||
return (
|
return (
|
||||||
<div style={{
|
<div style={{
|
||||||
margin: '10px',
|
margin: '10px',
|
||||||
padding: '10px',
|
padding: '20px',
|
||||||
display: 'grid',
|
display: 'grid',
|
||||||
gridTemplateColumns: 'repeat(8, 11vw)',
|
gridTemplateColumns: 'repeat(8, 11vw)',
|
||||||
gridTemplateRows: 'repeat(2, 11vw)',
|
gridTemplateRows: 'repeat(2, 11vw)',
|
||||||
|
|||||||
@ -3,18 +3,20 @@ export class Game {
|
|||||||
player2: string
|
player2: string
|
||||||
board: Board
|
board: Board
|
||||||
turn: "player1" | "player2"
|
turn: "player1" | "player2"
|
||||||
|
state: "playing" | "ended"
|
||||||
|
|
||||||
constructor(player1: string, player2: string, board: Board, turn: "player1" | "player2") {
|
constructor(player1: string, player2: string, board: Board, turn: "player1" | "player2", state: "playing" | "ended") {
|
||||||
this.player1 = player1
|
this.player1 = player1
|
||||||
this.player2 = player2
|
this.player2 = player2
|
||||||
this.board = board
|
this.board = board
|
||||||
this.turn = turn
|
this.turn = turn
|
||||||
|
this.state = state
|
||||||
}
|
}
|
||||||
|
|
||||||
public moveByIndex(holeIndex: number, player: "player1" | "player2") {
|
public moveByIndex(holeIndex: number, player: "player1" | "player2") {
|
||||||
let hole = this.board.player1Holes[holeIndex]
|
let hole = this.board.player1Holes[holeIndex]
|
||||||
|
|
||||||
if(player == "player2"){
|
if (player == "player2") {
|
||||||
hole = this.board.player2Holes[holeIndex]
|
hole = this.board.player2Holes[holeIndex]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,7 +66,7 @@ export class Game {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
hole.ballCount = holeEndBallCount
|
hole.ballCount = holeEndBallCount
|
||||||
console.log((stopIndex) );
|
console.log((stopIndex));
|
||||||
console.log(checkIsOwnHole(stopIndex, circleSize));
|
console.log(checkIsOwnHole(stopIndex, circleSize));
|
||||||
|
|
||||||
if (checkIsOwnHole(stopIndex, circleSize)) {
|
if (checkIsOwnHole(stopIndex, circleSize)) {
|
||||||
@ -73,6 +75,7 @@ export class Game {
|
|||||||
const ownStore = getOwnStore(circle)
|
const ownStore = getOwnStore(circle)
|
||||||
const opponentHole = getOpponentHoleByIndex(stopIndex, circle)
|
const opponentHole = getOpponentHoleByIndex(stopIndex, circle)
|
||||||
|
|
||||||
|
if (opponentHole.ballCount > 0) {
|
||||||
ownStore.ballCount++
|
ownStore.ballCount++
|
||||||
stopHole.ballCount = 0;
|
stopHole.ballCount = 0;
|
||||||
|
|
||||||
@ -80,10 +83,27 @@ export class Game {
|
|||||||
opponentHole.ballCount = 0
|
opponentHole.ballCount = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!checkIsOwnStore(stopIndex, circleSize)) {
|
if (!checkIsOwnStore(stopIndex, circleSize)) {
|
||||||
this.changeTurn()
|
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() {
|
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
|
//todo throw expection
|
||||||
public getPlayerNameByKey(key : string) : "player1" | "player2" {
|
public getPlayerNameByKey(key: string): "player1" | "player2" {
|
||||||
if(this.player1 == key){
|
if (this.player1 == key) {
|
||||||
return "player1"
|
return "player1"
|
||||||
}else{
|
} else {
|
||||||
return "player2"
|
return "player2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public checkGameTurn(user : string) : boolean{
|
public checkGameTurn(user: string): boolean {
|
||||||
return this.getPlayerNameByKey(user) == this.turn
|
return this.getPlayerNameByKey(user) == this.turn
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createGame( player1: string, player2: string){
|
export function createGame(player1: string, player2: string) {
|
||||||
// const board = new Board(
|
// 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)],
|
||||||
// [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 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))
|
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 {
|
export class Hole {
|
||||||
@ -191,5 +215,5 @@ function getOpponentHoleByIndex(index: number, circle: Array<Hole>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface GameMove {
|
export interface GameMove {
|
||||||
index : number,
|
index: number,
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user