Skip to content

Commit 193520e

Browse files
authored
better color separation (#452)
1 parent 1f2bb54 commit 193520e

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

server/src/game/logic.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,34 @@ export const processPendingAction = (state: GameState) => {
123123

124124
const getEvenlySpacedHueColors = (n: number) =>
125125
Array.from({ length: n }, (_, i) => {
126-
const angle = (i / n) * 2 * Math.PI
127-
return `hsl(${angle * (180 / Math.PI)}, 50%, 50%)`
126+
const angle = ((i + 0.5) / n) * 2 * Math.PI
127+
return `hsl(${Math.round(angle * (180 / Math.PI))}, 35%, 50%)`
128128
})
129129

130+
const distributeColorsWithFixedStep = (colors: string[]): string[] => {
131+
const n = colors.length
132+
const step = 3
133+
134+
if (n > 1 && n % step === 0) {
135+
throw new Error(
136+
`Step size of ${step} is not coprime to array length ${n}. The output array may not contain all original elements if a random start is used.`
137+
)
138+
}
139+
140+
const startIndex = Math.floor(Math.random() * n)
141+
const result: string[] = []
142+
let currentIndex = startIndex
143+
for (let i = 0; i < n; i++) {
144+
result.push(colors[currentIndex])
145+
currentIndex = (currentIndex + step) % n
146+
}
147+
148+
return result
149+
}
150+
130151
const getNewGameState = (roomId: string, settings: GameSettings): GameState => ({
131152
roomId,
132-
availablePlayerColors: shuffle(getEvenlySpacedHueColors(MAX_PLAYER_COUNT)),
153+
availablePlayerColors: distributeColorsWithFixedStep(getEvenlySpacedHueColors(MAX_PLAYER_COUNT)),
133154
players: [],
134155
deck: shuffle(createDeckForPlayerCount(0)),
135156
pendingInfluenceLoss: {},

0 commit comments

Comments
 (0)