Skip to content

Commit

Permalink
Track consequent days without kills to detect game tie
Browse files Browse the repository at this point in the history
  • Loading branch information
evgfilim1 committed Jun 1, 2023
1 parent eff948c commit 5d680a1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
- Все оставшиеся голоса уходят в последнего выставленного игрока
- Присутствует возможность "попила" стола
- [x] Завершение игры при кол-ве мафий = кол-ву мирных или кол-ве мафий = 0
- [ ] Отслеживает ничейный исход игры
- [x] Отслеживает ничейный исход игры
- [x] Таймер
- [ ] Отслеживает "Лучший ход"
- [ ] Ведёт лог игры
Expand Down
16 changes: 16 additions & 0 deletions lib/game/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,12 @@ class Game {
player: thisNightKilledPlayer,
);
}
if (_consequentDaysWithoutKills >= 3) {
return GameStateFinish(
day: state.day,
winner: null,
);
}
return GameStateSpeaking(
day: state.day + 1,
player: players.getByNumber(_firstSpeakingPlayerNumber),
Expand Down Expand Up @@ -529,5 +535,15 @@ class Game {
}
return result.number;
}

int get _consequentDaysWithoutKills {
final lastKillDay = _history
.where((e) =>
(e is GameStateWithPlayer && e.stage == GameStage.nightLastWords) ||
(e is GameStateWithCurrentPlayer && e.stage == GameStage.dayLastWords))
.lastOrNull
?.day;
return _state.day - (lastKillDay ?? 0);
}
// endregion
}
7 changes: 6 additions & 1 deletion lib/game/states.dart
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,12 @@ const validTransitions = {
GameStage.dropTableVoting: [GameStage.dayLastWords, GameStage.nightKill],
GameStage.dayLastWords: [GameStage.dayLastWords, GameStage.nightKill, GameStage.finish],
GameStage.nightKill: [GameStage.nightCheck],
GameStage.nightCheck: [GameStage.nightCheck, GameStage.nightLastWords, GameStage.speaking],
GameStage.nightCheck: [
GameStage.nightCheck,
GameStage.nightLastWords,
GameStage.speaking,
GameStage.finish,
],
GameStage.nightLastWords: [GameStage.speaking, GameStage.finish],
GameStage.finish: [],
};
12 changes: 8 additions & 4 deletions lib/screens/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,17 @@ class _MainScreenState extends State<MainScreen> {
value: onlyOneSelected ? aliveCount : currentPlayerVotes,
);
}
if (gameState.stage == GameStage.finish) {
final winRole =
controller.winTeamAssumption! == PlayerRole.citizen ? "мирных жителей" : "мафии";
if (gameState case GameStateFinish(winner: final winner)) {
final resultText = switch (winner) {
PlayerRole.citizen => "Победа команды мирных жителей",
PlayerRole.mafia => "Победа команды мафии",
null => "Ничья",
_ => throw AssertionError(),
};
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Text("Победа команды $winRole", style: const TextStyle(fontSize: 20)),
Text(resultText, style: const TextStyle(fontSize: 20)),
TextButton(
onPressed: () async {
if (await _showRestartGameDialog(context)) {
Expand Down

0 comments on commit 5d680a1

Please sign in to comment.