cpd_David_und_Yusuf/lib/models/game.dart

88 lines
2.3 KiB
Dart
Raw Permalink Normal View History

import 'package:werwolf/models/role.dart';
import 'player.dart';
class Game {
2024-06-18 01:46:57 +02:00
// List to store player objects
List<Player> players = [];
2024-06-18 01:46:57 +02:00
// List to store player names
List playernames = [];
2024-06-18 01:46:57 +02:00
// Initial number of werewolves
int numWolves = 1;
2024-06-18 01:46:57 +02:00
// Map to store special roles and their activation status
Map specialRoles = <Role, bool>{};
2024-06-18 01:46:57 +02:00
// Constructor to initialize the game with player names
Game({required this.playernames}) {
2024-06-18 01:46:57 +02:00
// Initialize specialRoles map, setting all special roles to false
for (Role role in Role.values) {
if (role != Role.dorfbewohner && role != Role.werwolf) {
specialRoles[role] = false;
}
}
}
2024-06-18 01:46:57 +02:00
// Method to increment the number of werewolves
void incrementWolves() {
2024-06-18 01:46:57 +02:00
// Increment only if conditions are met: enough players and balance maintained
if (numWolves < playernames.length - 1 &&
(playernames.length) >= ((numWolves + 1) * 3)) {
numWolves++;
}
}
2024-06-18 01:46:57 +02:00
// Method to decrement the number of werewolves
void decrementWolves() {
2024-06-18 01:46:57 +02:00
// Decrement only if there's more than one werewolf
if (numWolves > 1) {
numWolves--;
}
}
2024-06-18 01:46:57 +02:00
// Method to get the current number of werewolves
int getWolves() {
return numWolves;
}
2024-05-10 23:21:21 +02:00
2024-06-18 01:46:57 +02:00
// Method to get all players with their assigned roles
2024-05-10 23:21:21 +02:00
List<Player> getAllPlayers() {
2024-06-18 01:46:57 +02:00
// Clear the players list to start fresh
2024-05-10 23:21:21 +02:00
players.clear();
2024-06-18 01:46:57 +02:00
// List to hold roles randomly assigned to players
2024-05-10 23:21:21 +02:00
List<Role> randomRoles = [];
2024-06-18 01:46:57 +02:00
// Add werewolf roles to the list
2024-05-10 23:21:21 +02:00
for (var i = 0; i < numWolves; i++) {
randomRoles.add(Role.werwolf);
}
2024-06-18 01:46:57 +02:00
// Add active special roles to the list
2024-05-10 23:21:21 +02:00
for (var specialRole in specialRoles.keys) {
if (specialRoles[specialRole]) {
randomRoles.add(specialRole);
}
}
2024-06-18 01:46:57 +02:00
// Fill the remaining roles with dorfbewohner
2024-05-10 23:21:21 +02:00
for (var i = randomRoles.length; i < playernames.length; i++) {
randomRoles.add(Role.dorfbewohner);
}
2024-06-18 01:46:57 +02:00
// Shuffle the roles to ensure randomness
2024-05-10 23:21:21 +02:00
randomRoles.shuffle();
2024-06-18 01:46:57 +02:00
// Assign roles to players and create Player objects
2024-05-10 23:21:21 +02:00
for (var playerName in playernames) {
players
.add(Player(name: playerName, role: randomRoles.last, isDead: false));
2024-05-10 23:21:21 +02:00
randomRoles.removeLast();
}
2024-06-18 01:46:57 +02:00
// Return the list of players with their roles
2024-05-10 23:21:21 +02:00
return players;
}
}