added ending game to mancala

This commit is contained in:
jhalitaksoy 2021-06-30 19:27:38 +03:00
parent 30953e3464
commit a5b9ec2513
6 changed files with 82 additions and 26 deletions

View File

@ -53,8 +53,9 @@ matchmaker.onPlayersPaired = (userKey1: string, userKey2: string) => {
gameStore.set(userKey1, game)
gameStore.set(userKey2, game)
rtmt.sendMessage(userKey1, channel_on_game_start, encodeText(JSON.stringify(game)))
rtmt.sendMessage(userKey2, channel_on_game_start, encodeText(JSON.stringify(game)))
const data = encodeText(JSON.stringify(game))
rtmt.sendMessage(userKey1, channel_on_game_start, data)
rtmt.sendMessage(userKey2, channel_on_game_start, data)
}
rtmt.listenMessage(channel_game_move, (userKey: string, message: Bytes) => {
@ -62,9 +63,14 @@ rtmt.listenMessage(channel_game_move, (userKey: string, message: Bytes) => {
const game = gameStore.get(userKey)
if (game) {
game.moveByIndex(gameMove.index, game.getPlayerNameByKey(userKey))
const data = encodeText(JSON.stringify(game))
rtmt.sendMessage(game.player1, channel_on_game_update, data)
rtmt.sendMessage(game.player2, channel_on_game_update, data)
rtmt.sendMessage(game.player1, channel_on_game_update, encodeText(JSON.stringify(game)))
rtmt.sendMessage(game.player2, channel_on_game_update, encodeText(JSON.stringify(game)))
if (game.state == "ended") {
gameStore.delete(game.player1)
gameStore.delete(game.player2)
}
} else {
console.log("Game not found!");
}
@ -75,5 +81,8 @@ rtmt.listenMessage(channel_leave_game, (userKey: string, message: Bytes) => {
if (game) {
gameStore.delete(game.player1)
gameStore.delete(game.player2)
const data = encodeText(JSON.stringify(game))
rtmt.sendMessage(game.player1, channel_on_game_update, data)
rtmt.sendMessage(game.player2, channel_on_game_update, data)
}
})

View File

@ -3,12 +3,14 @@ export class Game {
player2: string
board: Board
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.player2 = player2
this.board = board
this.turn = turn
this.state = state
}
public moveByIndex(holeIndex: number, player: "player1" | "player2") {
@ -73,6 +75,7 @@ export class Game {
const ownStore = getOwnStore(circle)
const opponentHole = getOpponentHoleByIndex(stopIndex, circle)
if (opponentHole.ballCount > 0) {
ownStore.ballCount++
stopHole.ballCount = 0;
@ -80,10 +83,27 @@ export class Game {
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,7 +114,7 @@ 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" {
@ -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 {

View File

@ -6,6 +6,7 @@ export class MatchMaker {
public onPlayersPaired!: (userKey1: string, userKey2: string) => void
public find(userKey : string) : void{
if(this.waitingUserKey == userKey) return;
if(this.waitingUserKey){
const user1 = this.waitingUserKey as string
this.waitingUserKey = undefined

View File

@ -12,11 +12,20 @@ describe('mancala', function () {
});
it('zero hole test 1', function () {
var board = new mancala_1.Board([new mancala_1.Hole(0), new mancala_1.Hole(1), new mancala_1.Hole(2), new mancala_1.Hole(3), new mancala_1.Hole(4), new mancala_1.Hole(5)], [new mancala_1.Hole(1), new mancala_1.Hole(0), new mancala_1.Hole(4), new mancala_1.Hole(4), new mancala_1.Hole(4), new mancala_1.Hole(4)], new mancala_1.Store(0), new mancala_1.Store(0));
var game = new mancala_1.Game(player1, player2, board, "player2");
var game = new mancala_1.Game(player1, player2, board, "player2", "playing");
game.moveByIndex(0, "player2");
expect(game.turn).toBe("player1");
expect(game.board.player2Store.ballCount).toBe(5);
expect(game.board.player1Holes[game.board.player1Holes.length - 1 - 1].ballCount).toBe(0);
});
it('game end test 1', function () {
var board = new mancala_1.Board([new mancala_1.Hole(0), new mancala_1.Hole(0), new mancala_1.Hole(0), new mancala_1.Hole(0), new mancala_1.Hole(4), new mancala_1.Hole(4)], [new mancala_1.Hole(0), new mancala_1.Hole(0), new mancala_1.Hole(0), new mancala_1.Hole(0), new mancala_1.Hole(0), new mancala_1.Hole(1)], new mancala_1.Store(0), new mancala_1.Store(0));
var game = new mancala_1.Game(player1, player2, board, "player2", "playing");
game.moveByIndex(5, "player2");
expect(game.turn).toBe("player2");
expect(game.board.player2Store.ballCount).toBe(9);
expect(game.board.player1Holes[game.board.player1Holes.length / 2].ballCount).toBe(0);
expect(game.state).toBe("ended");
});
});
//# sourceMappingURL=mancala.test.js.map

View File

@ -1 +1 @@
{"version":3,"file":"mancala.test.js","sourceRoot":"","sources":["mancala.test.ts"],"names":[],"mappings":";;AAAA,0CAAqE;AAErE,QAAQ,CAAC,SAAS,EAAE;IAClB,IAAM,OAAO,GAAG,GAAG,CAAA;IACnB,IAAM,OAAO,GAAG,GAAG,CAAA;IACnB,EAAE,CAAC,WAAW,EAAE;QACd,IAAM,IAAI,GAAG,oBAAU,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;QACzC,IAAI,CAAC,IAAI,GAAG,SAA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E,SAAS,EAAE,SAAS,CAAC,CAAA;QACpE,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,SAAS,CAAC,CAAA;QAC9B,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;QACjC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QACjD,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QACrF,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IAClC,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}

View File

@ -14,10 +14,23 @@ describe('mancala', function () {
[new Hole(0), new Hole(1), new Hole(2), new Hole(3), new Hole(4), new Hole(5)],
[new Hole(1), new Hole(0), new Hole(4), new Hole(4), new Hole(4), new Hole(4)],
new Store(0), new Store(0))
const game = new Game(player1, player2, board, "player2")
const game = new Game(player1, player2, board, "player2", "playing")
game.moveByIndex(0, "player2")
expect(game.turn).toBe("player1")
expect(game.board.player2Store.ballCount).toBe(5)
expect(game.board.player1Holes[game.board.player1Holes.length - 1 - 1].ballCount).toBe(0)
})
it('game end test 1', function () {
const board = new Board(
[new Hole(0), new Hole(0), new Hole(0), new Hole(0), new Hole(4), new Hole(4)],
[new Hole(0), new Hole(0), new Hole(0), new Hole(0), new Hole(0), new Hole(1)],
new Store(0), new Store(0))
const game = new Game(player1, player2, board, "player2", "playing")
game.moveByIndex(5, "player2")
expect(game.turn).toBe("player2")
expect(game.board.player2Store.ballCount).toBe(9)
expect(game.board.player1Holes[game.board.player1Holes.length / 2].ballCount).toBe(0)
expect(game.state).toBe("ended")
})
})