2021-06-29 03:27:18 +03:00
|
|
|
export class Game {
|
|
|
|
|
player1: string
|
|
|
|
|
player2: string
|
|
|
|
|
board: Board
|
|
|
|
|
turn: "player1" | "player2"
|
2021-06-30 19:41:53 +03:00
|
|
|
state: "playing" | "ended"
|
2021-06-29 03:27:18 +03:00
|
|
|
|
2021-06-30 19:41:53 +03:00
|
|
|
constructor(player1: string, player2: string, board: Board, turn: "player1" | "player2", state: "playing" | "ended") {
|
2021-06-29 03:27:18 +03:00
|
|
|
this.player1 = player1
|
|
|
|
|
this.player2 = player2
|
|
|
|
|
this.board = board
|
|
|
|
|
this.turn = turn
|
2021-06-30 19:41:53 +03:00
|
|
|
this.state = state
|
2021-06-29 03:27:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public moveByIndex(holeIndex: number, player: "player1" | "player2") {
|
|
|
|
|
let hole = this.board.player1Holes[holeIndex]
|
|
|
|
|
|
2021-06-30 19:41:53 +03:00
|
|
|
if (player == "player2") {
|
2021-06-29 03:27:18 +03:00
|
|
|
hole = this.board.player2Holes[holeIndex]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.move(hole, player)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public move(hole: Hole, player: "player1" | "player2") {
|
|
|
|
|
|
|
|
|
|
if (this.turn != player) return
|
|
|
|
|
if (hole.ballCount == 0) return
|
|
|
|
|
|
|
|
|
|
let holeIndex = -1
|
|
|
|
|
let circle: Array<Hole>
|
|
|
|
|
if (player == "player1") {
|
|
|
|
|
holeIndex = this.board.player1Holes.indexOf(hole)
|
|
|
|
|
circle = this.board.player1Circle()
|
|
|
|
|
} else {
|
|
|
|
|
holeIndex = this.board.player2Holes.indexOf(hole)
|
|
|
|
|
circle = this.board.player2Circle()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if can't find hole in own list
|
|
|
|
|
if (holeIndex < 0) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const circleSize = circle.length
|
|
|
|
|
const stepCount = hole.ballCount
|
|
|
|
|
|
|
|
|
|
let startIndex = holeIndex
|
|
|
|
|
|
|
|
|
|
let holeEndBallCount = 1
|
|
|
|
|
|
|
|
|
|
if (hole.ballCount == 1) {
|
|
|
|
|
startIndex = holeIndex + 1
|
|
|
|
|
holeEndBallCount = 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let stopIndex = startIndex + stepCount - 1
|
|
|
|
|
stopIndex += (stopIndex / circleSize) | 0 // add the number of jump over opponent store
|
|
|
|
|
|
|
|
|
|
const stopHole = circle[stopIndex % circle.length]
|
|
|
|
|
|
|
|
|
|
iterateHoles(circle, startIndex, stopIndex, (eachHole: Hole, index: number) => {
|
|
|
|
|
if (!checkIsOpponentStore(index, circleSize)) {
|
|
|
|
|
eachHole.ballCount++
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
hole.ballCount = holeEndBallCount
|
2021-06-30 19:41:53 +03:00
|
|
|
console.log((stopIndex));
|
2021-06-29 03:27:18 +03:00
|
|
|
console.log(checkIsOwnHole(stopIndex, circleSize));
|
|
|
|
|
|
|
|
|
|
if (checkIsOwnHole(stopIndex, circleSize)) {
|
|
|
|
|
//check if it was 0 before
|
|
|
|
|
if (stopHole.ballCount == 1) {
|
|
|
|
|
const ownStore = getOwnStore(circle)
|
|
|
|
|
const opponentHole = getOpponentHoleByIndex(stopIndex, circle)
|
|
|
|
|
|
2021-06-30 19:41:53 +03:00
|
|
|
if (opponentHole.ballCount > 0) {
|
|
|
|
|
ownStore.ballCount++
|
|
|
|
|
stopHole.ballCount = 0;
|
2021-06-29 03:27:18 +03:00
|
|
|
|
2021-06-30 19:41:53 +03:00
|
|
|
ownStore.ballCount += opponentHole.ballCount
|
|
|
|
|
opponentHole.ballCount = 0
|
|
|
|
|
}
|
2021-06-29 03:27:18 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!checkIsOwnStore(stopIndex, circleSize)) {
|
|
|
|
|
this.changeTurn()
|
|
|
|
|
}
|
2021-06-30 19:41:53 +03:00
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
}
|
2021-06-29 03:27:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public changeTurn() {
|
|
|
|
|
if (this.turn == "player1") {
|
|
|
|
|
this.turn = "player2"
|
|
|
|
|
} else {
|
|
|
|
|
this.turn = "player1"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-30 19:41:53 +03:00
|
|
|
public clone() { return new Game(this.player1, this.player2, this.board, this.turn, this.state) }
|
2021-06-29 03:27:18 +03:00
|
|
|
|
|
|
|
|
//todo throw expection
|
2021-06-30 19:41:53 +03:00
|
|
|
public getPlayerNameByKey(key: string): "player1" | "player2" {
|
|
|
|
|
if (this.player1 == key) {
|
2021-06-29 03:27:18 +03:00
|
|
|
return "player1"
|
2021-06-30 19:41:53 +03:00
|
|
|
} else {
|
2021-06-29 03:27:18 +03:00
|
|
|
return "player2"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-30 19:41:53 +03:00
|
|
|
public checkGameTurn(user: string): boolean {
|
2021-06-29 03:27:18 +03:00
|
|
|
return this.getPlayerNameByKey(user) == this.turn
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-30 19:41:53 +03:00
|
|
|
export function createGame(player1: string, player2: string) {
|
2021-06-29 03:27:18 +03:00
|
|
|
// 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 Store(0), new Store(0))
|
|
|
|
|
const board = new Board(
|
|
|
|
|
[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))
|
2021-06-30 19:41:53 +03:00
|
|
|
// 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")
|
2021-06-29 03:27:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class Hole {
|
|
|
|
|
public ballCount: number
|
|
|
|
|
|
|
|
|
|
constructor(ballCount: number) {
|
|
|
|
|
this.ballCount = ballCount
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class Store extends Hole {
|
|
|
|
|
constructor(ballCount: number) {
|
|
|
|
|
super(ballCount)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class Board {
|
|
|
|
|
public player1Holes: Array<Hole>
|
|
|
|
|
public player2Holes: Array<Hole>
|
|
|
|
|
public player1Store: Store
|
|
|
|
|
public player2Store: Store
|
|
|
|
|
|
|
|
|
|
constructor(player1Holes: Array<Hole>, player2Holes: Array<Hole>, player1Store: Store, player2Store: Store) {
|
|
|
|
|
this.player1Holes = player1Holes
|
|
|
|
|
this.player2Holes = player2Holes
|
|
|
|
|
this.player1Store = player1Store
|
|
|
|
|
this.player2Store = player2Store
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// array.reverse didn't work
|
|
|
|
|
public player1Circle(): Array<Hole> {
|
|
|
|
|
return [...this.player1Holes, this.player1Store, ...this.player2Holes, this.player2Store]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// array.reverse didn't work
|
|
|
|
|
public player2Circle(): Array<Hole> {
|
|
|
|
|
return [...this.player2Holes, this.player2Store, ...this.player1Holes, this.player1Store]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function iterateHoles(holes: Array<Hole>, start: number, stop: number, callback: (hole: Hole, index: number) => void) {
|
|
|
|
|
for (let index = start; index <= stop; index++) {
|
|
|
|
|
callback(holes[index % holes.length], index)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function reverse(holes: Array<Hole>) {
|
|
|
|
|
const array = new Array<Hole>()
|
|
|
|
|
for (let index = holes.length - 1; index > -1; index--) {
|
|
|
|
|
array.push(holes[index])
|
|
|
|
|
}
|
|
|
|
|
return array
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function checkIsOpponentStore(index: number, size: number) { return (index % size) === size - 1 }
|
|
|
|
|
|
|
|
|
|
function checkIsOpponentHole(index: number, size: number) { return (index % size) >= size / 2 }
|
|
|
|
|
|
|
|
|
|
function checkIsOwnHole(index: number, size: number) { return (index % size) < size / 2 - 1 }
|
|
|
|
|
|
|
|
|
|
function checkIsOwnStore(index: number, size: number) { return (index % size) == size / 2 - 1 }
|
|
|
|
|
|
|
|
|
|
function getOwnStore(circle: Array<Hole>) {
|
|
|
|
|
return circle[circle.length / 2 - 1]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getOpponentHoleByIndex(index: number, circle: Array<Hole>) {
|
|
|
|
|
return circle[circle.length - 2 - index]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface GameMove {
|
2021-06-30 19:41:53 +03:00
|
|
|
index: number,
|
2021-06-29 03:27:18 +03:00
|
|
|
}
|