Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 33x 33x 11x 11x 104x 104x 104x 11x 11x 11x 11x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 1x 22x 22x 35x 35x 35x 22x 1x 1x 1x 11x 11x 1x 1x 1x 9x 9x 1x 1x 1x 21x 21x 21x 21x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 4x 4x 4x 4x 1x 1x 1x 21x 21x 21x 21x 21x 145x 145x 119x 119x 119x 119x 119x 119x 145x 21x 21x 21x 21x 21x 39x 39x 39x 10x 10x 39x 10x 10x 39x 11x 11x 39x 39x 8x 8x 39x 21x 11x 11x 21x 10x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 10x 8x 8x 8x 8x 8x 8x 10x 21x 21x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 39x 39x 39x 39x 82x 43x 43x 82x 4x 4x 82x 39x 39x 39x 36x 39x 3x 3x 3x 39x 1x 1x 1x 21x 21x 21x 21x 1x 21x 2x 2x 21x 1x 1x 1x 25x 25x 1x 1x 1x 4x 4x 1x 1x 1x 2x 2x 1x 1x 1x 3x 3x 1x 1x 1x 1x 1x | import {Team} from "./team"; import {Phase} from "./phase"; import {Player} from "./player"; import {Role} from "./role"; import {Action} from "./action"; import {Chat} from "./chat"; /** * Represents the state of a game, including the players in it, roles, phases, etc. * The game setup is passed on the constructor. It then internally * assigns roles to the players and starts in day 1 with the voting phase. * * The method advance() simulates the actions of all players for the day, and * advances the state to the next phase. * * After each advance, the new state can be accessed through getters. */ export class Game { private _phase: Phase = Phase.SIGN_UP; private _players: Player[] = []; private _winner?: Team; private _day = 0; private _initialRoleCounts; constructor( totalPlayers: number, roleCounts: {role: Role, count: number}[], ){ this._initialRoleCounts = roleCounts; const roles: Role[] = []; for (const {role, count} of roleCounts) { roles.push(...Array(count).fill(role)); } for (let i = 0; i<totalPlayers; i++) { const role = roles[i] || Role.VILLAGER; this._players.push(new Player(role)); } this.teamUp(this._players.filter(p => p.team == Team.WOLVES)); this.teamUp(this._players.filter(p => p.role == Role.GUARD)); this.seerEachother(this._players.filter(p => p.role == Role.MASON)); } /** * Returns a new game that uses the same setup as this one * @deprecated in favor of the constructor */ clone(): Game { return new Game(this.players, this._initialRoleCounts); } /** Adds players to a group chat so they can coordinate */ private teamUp(players: Player[]) { const chat = new Chat(); for (const player of players) { player.friends(players); player.chat(chat); } } /** Reveals roles to eachother */ private seerEachother(players: Player[]) { for (const player of players) { player.friends(players); } } /** @readonly The initial number of players */ get players(): number { return this._players.length; } /** The number of players with a certain role */ roleCount(role: Role): number { return this._players .filter(p => p.role == role) .length; } /** The number of players in a team */ teamCount(team : Team): number { return this._players .filter(p => p.team == team) .length; } /** @readonly Indicates if villagers have special roles */ get hasSpecialVillagers(): boolean { return this._players .filter(p => p.team == Team.VILLAGE && p.role != Role.VILLAGER) .length > 0; } /** Advances the game to the next phase, simulating all actions from players */ advance() { const playersAlive = this._players.filter(p => p.alive); this._phase = this._phase == Phase.DAY ? Phase.NIGHT : Phase.DAY; if (this._phase == Phase.DAY) this._day++; const tallies = new Map<Action, Map<Player, number>>(); for (const player of playersAlive) { const actions = player.act(this._phase, playersAlive); for (const [action,target] of actions) { const tallyForAction = tallies.get(action) || new Map<Player, number>(); const count = tallyForAction.get(target) || 0; tallyForAction.set(target, count + 1); tallies.set(action, tallyForAction); if (action == Action.SCOUT) { player.scoutResult(target, this.nightActivity(target.role)); } } } const guarded: Player[] = []; const attacked: Player[] = []; const hanged: Player[] = []; const healed: Player[] = []; for (const [action, tally] of tallies) { const target = this.tieBreak(tally); if (target) { if (action == Action.GUARD) { guarded.push(target); } if (action == Action.ATTACK) { attacked.push(target); } if (action == Action.VOTE) { hanged.push(target); } } if (action == Action.HEAL) { healed.push(...tally.keys()); } } for (const player of hanged) { player.die(); } for (const player of attacked) { if (guarded.includes(player)) { const wolves = this._players.filter(p => p.team == Team.WOLVES); const guards = this._players.filter(p => p.role == Role.GUARD); const deadWolf = wolves[Math.floor(Math.random() * wolves.length)]; const guard = guards[Math.floor(Math.random() * guards.length)]; if (healed.includes(guard)) { this.revealGood(guard); } else { guard.die(); } deadWolf.die(); this.revealGood(player); } else { if (healed.includes(player)) { this.revealGood(player); } else if (player.role == Role.LYCAN) { player.role = Role.WEREWOLF; const wolves = this._players.filter(p => p.team == Team.WOLVES); this.teamUp(wolves); } else { player.die(); } } } this.updateWinCondition(); } /** Lets all good players know that this player is good */ private revealGood(player: Player) { this._players .filter(p => p.team == Team.VILLAGE) .forEach(p => p.friends([player])); } /** Determines which player is the target after votes have been casted */ private tieBreak(tally: Map<Player,number>): Player|null { let highestScorePlayers: Player[] = []; let highestScore = -Infinity; for (const [player, score] of tally.entries()) { if (score > highestScore) { highestScore = score; highestScorePlayers = [player]; } else if (score === highestScore) { highestScorePlayers.push(player); } } if (highestScorePlayers.length === 0) { return null; } else if (highestScorePlayers.length === 1) { return highestScorePlayers[0]; } else { const randomIndex = Math.floor(Math.random() * highestScorePlayers.length); return highestScorePlayers[randomIndex]; } } /** Verifies if one of the teams has won the game */ private updateWinCondition() { const alive = this._players.filter(p => p.alive); const evilCount = alive.filter(p => p.team == Team.WOLVES).length; const goodCount = alive.length - evilCount; if (evilCount == 0) { this._winner = Team.VILLAGE; } else if (evilCount >= goodCount) { this._winner = Team.WOLVES; } } /** @readonly Indicates that the game has ended */ get ended():boolean { return this._winner !== undefined; } /** @readonly Returns the team that won, if any */ get winner(): Team|undefined { return this._winner; } /** @readonly The number of days that passed (minimum 1) */ get days(): number { return this._day; } /** @readonly The list of roles in a team, with duplicates */ get roles(): Role[] { return this._players.map(player => player.role); } /** Determines if a role has night activity */ nightActivity(role: Role) { return role == Role.WEREWOLF || role == Role.GUARD || role == Role.HEALER || role == Role.SCOUT; } } |