Skip to content

Commit

Permalink
Should have fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Oskar Baumann committed Dec 28, 2023
1 parent 7da9a2b commit 9cb37e9
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 25 deletions.
25 changes: 13 additions & 12 deletions server/src/bot/bots/Futuro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export class Futuro implements AiInterface {
const moves = getMovesFromCards(data.cardsWithMoves, data.gamePlayer)
return moves[Math.floor(Math.random() * moves.length)]
}
return nodes.sort((p1, p2) => calculateScoreOfNode(p2) - calculateScoreOfNode(p1))[0].movesToGetThere[0]
const sortedNodes = nodes.sort((p1, p2) => calculateScoreOfNode(p2) - calculateScoreOfNode(p1))

return sortedNodes[0].movesToGetThere[0]
} catch (e) {
console.error('Could not calculate paths', e)
}
Expand All @@ -26,7 +28,7 @@ export class Futuro implements AiInterface {
function calculatePaths(data: AiData): EndNode[] {
let nodes: EndNode[] = [{ state: data, movesToGetThere: [], scoresPerState: [] }]

for (let i = 0; i < 1; i++) {
for (let i = 0; i < 3; i++) {
const newNodes: EndNode[] = []
for (let node of nodes) {

Check failure on line 33 in server/src/bot/bots/Futuro.ts

View workflow job for this annotation

GitHub Actions / build

'node' is never reassigned. Use 'const' instead
newNodes.push(...expandNode(node))
Expand Down Expand Up @@ -54,23 +56,22 @@ function expandNode(node: EndNode): EndNode[] {
return [node]
}

return moves.map((m) => {
return {
state: previewMove(node.state, m),
const result = moves.map((m) => {

Check failure on line 59 in server/src/bot/bots/Futuro.ts

View workflow job for this annotation

GitHub Actions / build

Immediately return this expression instead of assigning it to the temporary variable "result"
const dataAfterMove = previewMove(node.state, m)
return structuredClone({
state: dataAfterMove,
movesToGetThere: [...node?.movesToGetThere, m],

Check failure on line 63 in server/src/bot/bots/Futuro.ts

View workflow job for this annotation

GitHub Actions / build

Unsafe usage of optional chaining. If it short-circuits with 'undefined' the evaluation will throw TypeError
scoresPerState: [...node?.scoresPerState, calculateScoreOfState(node.state)],
}
scoresPerState: [...node?.scoresPerState, calculateScoreOfState(dataAfterMove)],

Check failure on line 64 in server/src/bot/bots/Futuro.ts

View workflow job for this annotation

GitHub Actions / build

Unsafe usage of optional chaining. If it short-circuits with 'undefined' the evaluation will throw TypeError
})
})
return result
}

function calculateScoreOfState(data: AiData): number {
// Sum of all teams for coop, difference of own team and enemy teams for non-coop
/*return data.coop
return data.coop
? data.teams.reduce((sum, team) => sum + calculatePointsOfTeamFromBalls(data.balls, team), 0)
: data.teams.reduce((sum, team, i) => sum + (i === 0 ? 1 : -1) * calculatePointsOfTeamFromBalls(data.balls, team), 0)
*/
// TODO: DEBUG
return calculatePointsOfTeamFromBalls(data.balls, data.teams[0])
}

function calculatePointsOfTeamFromBalls(balls: BallsType, team: number[]): number {
Expand All @@ -93,5 +94,5 @@ function calculatePointsOfTeamFromBalls(balls: BallsType, team: number[]): numbe
type EndNode = { state: AiData; movesToGetThere: MoveTextOrBall[]; scoresPerState: number[] }

function calculateScoreOfNode(node: EndNode) {
return node.scoresPerState.map((n, i) => n * (1 / 2) ** (node.movesToGetThere.length - i - 1)).reduce((sum, score) => sum + score, 0)
return node.scoresPerState.map((n, i) => n * Math.pow(0.5, node.movesToGetThere.length - i - 1)).reduce((sum, score) => sum + score, 0)
}
26 changes: 26 additions & 0 deletions server/src/bot/bots/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { initializeBalls } from 'src/game/ballUtils'
import { normalizedNecessaryForwardMovesToEndOfGoal } from './utils'

describe('Test Bot Utils', () => {
test('normalizedNecessaryForwardMovesToEndOfGoal', () => {
const balls = initializeBalls(4)

expect(normalizedNecessaryForwardMovesToEndOfGoal(0, 0, balls)).toBe(0)
expect(normalizedNecessaryForwardMovesToEndOfGoal(17, 0, balls)).toBe(1 - 67 / 68)
expect(normalizedNecessaryForwardMovesToEndOfGoal(79, 0, balls)).toBe(1 - 5 / 68)
expect(normalizedNecessaryForwardMovesToEndOfGoal(80, 0, balls)).toBe(1 - 3 / 68)
expect(normalizedNecessaryForwardMovesToEndOfGoal(83, 0, balls)).toBe(1)

expect(normalizedNecessaryForwardMovesToEndOfGoal(4, 4, balls)).toBe(0)
expect(normalizedNecessaryForwardMovesToEndOfGoal(33, 4, balls)).toBe(1 - 67 / 68)
expect(normalizedNecessaryForwardMovesToEndOfGoal(31, 4, balls)).toBe(1 - 5 / 68)
expect(normalizedNecessaryForwardMovesToEndOfGoal(84, 4, balls)).toBe(1 - 3 / 68)
expect(normalizedNecessaryForwardMovesToEndOfGoal(87, 4, balls)).toBe(1)

expect(normalizedNecessaryForwardMovesToEndOfGoal(8, 8, balls)).toBe(0)
expect(normalizedNecessaryForwardMovesToEndOfGoal(49, 8, balls)).toBe(1 - 67 / 68)
expect(normalizedNecessaryForwardMovesToEndOfGoal(47, 8, balls)).toBe(1 - 5 / 68)
expect(normalizedNecessaryForwardMovesToEndOfGoal(88, 8, balls)).toBe(1 - 3 / 68)
expect(normalizedNecessaryForwardMovesToEndOfGoal(91, 8, balls)).toBe(1)
})
})
10 changes: 6 additions & 4 deletions server/src/bot/bots/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { ballStart, getPositionsBetweenStarts } from '../../game/ballUtils'
import { ballGoal, ballStart, getPositionsBetweenStarts } from '../../game/ballUtils'
import { BallsType } from 'src/sharedTypes/typesBall'

function necessaryForwardMovesToEndOfGoal(position: number, ballIndex: number, balls: BallsType): number {
if (position < ballStart(0, balls)) return maxMovesToEndOfGoal(balls)
return ballStart(ballIndex, balls) > position
? ballStart(ballIndex, balls) - position + 4
: ballStart(ballIndex, balls) + (balls.length / 4) * getPositionsBetweenStarts(balls) - position + 4
if (position < ballGoal(0, balls))
return ballStart(ballIndex, balls) > position

Check failure on line 7 in server/src/bot/bots/utils.ts

View workflow job for this annotation

GitHub Actions / build

Expected { after 'if' condition
? ballStart(ballIndex, balls) - position + 4
: ballStart(ballIndex, balls) + (balls.length / 4) * getPositionsBetweenStarts(balls) - position + 4
return ballGoal(ballIndex, balls) + 3 - position
}

function maxMovesToEndOfGoal(balls: BallsType): number {
Expand Down
6 changes: 3 additions & 3 deletions server/src/bot/simulation/dev.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Futuro } from '../bots/Futuro'
//import { Ruby } from '../bots/Ruby'
import { Ruby } from '../bots/Ruby'
//import { Greedy } from '../bots/Greedy'
import { Raindom } from '../bots/Raindom'
//import { Raindom } from '../bots/Raindom'
import { runSimulation } from './simulation'

runSimulation(1, [new Futuro(), new Raindom(), new Futuro(), new Raindom()])
runSimulation(1, [new Futuro(), new Ruby(), new Futuro(), new Ruby()])
14 changes: 8 additions & 6 deletions server/src/bot/simulation/previewMove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ export function previewMove(data: AiData, move: MoveTextOrBall): AiData {
if (!game.gameEnded && game.cardsWithMoves.length > 0 && game.cardsWithMoves.every((c) => !c.possible)) throw new Error('No possible moves found')

// convert back to data
return getAiData(game, 0, {
hadOneOrThirteen: data.hadOneOrThirteen,
tradedCards: Array.from({ length: data.nPlayers }, (_, i) => (i === 0 ? data.tradedCard : null)),
narrTradedCards: Array.from({ length: data.nPlayers }, (_, i) => (i === 0 ? data.narrTradedCards : null)),
previouslyUsedCards: data.previouslyUsedCards,
})
return structuredClone(
getAiData(game, 0, {
hadOneOrThirteen: data.hadOneOrThirteen,
tradedCards: Array.from({ length: data.nPlayers }, (_, i) => (i === 0 ? data.tradedCard : null)),
narrTradedCards: Array.from({ length: data.nPlayers }, (_, i) => (i === 0 ? data.narrTradedCards : null)),
previouslyUsedCards: data.previouslyUsedCards,
})
)
}

0 comments on commit 9cb37e9

Please sign in to comment.