Implemented new class structure

This commit is contained in:
Felix Kirchner
2025-04-30 00:06:13 +02:00
parent 18606508dc
commit 9827a983e7
2 changed files with 200 additions and 200 deletions

View File

@@ -43,15 +43,17 @@ class _RoundViewState extends State<RoundView> {
print('=== Runde ${widget.roundNumber} geöffnet ===');
if (widget.roundNumber < widget.gameSession.roundNumber ||
widget.gameSession.isGameFinished == true) {
print('Die Runde ${widget.roundNumber} wurde bereits gespielt, deshalb '
'werden die alten Punktestaende angezeigt');
print(
'Diese wurde bereits gespielt, deshalb werden die alten Punktestaende angezeigt');
// If the current round has already been played, the text fields
// are filled with the scores from this round
for (int i = 0; i < _scoreControllerList.length; i++) {
_scoreControllerList[i].text =
gameSession.playerScores[i][widget.roundNumber].toString();
gameSession.roundList[widget.roundNumber - 1].scores[i].toString();
}
_kamikazePlayerIndex =
gameSession.roundList[widget.roundNumber - 1].kamikaze;
}
super.initState();
}
@@ -179,7 +181,7 @@ class _RoundViewState extends State<RoundView> {
backgroundColor: CupertinoColors.secondaryLabel,
title: Row(children: [Text(name)]),
subtitle: Text(
'${widget.gameSession.playerScores[index][0]}'
'${widget.gameSession.playerScores[index]}'
' Punkte'),
trailing: Row(
children: [
@@ -343,7 +345,7 @@ class _RoundViewState extends State<RoundView> {
}
/// Finishes the current round.
/// Calls the [_calculateScoredPoints()] method to calculate the points for
/// It first determines, ifCalls the [_calculateScoredPoints()] method to calculate the points for
/// every player. If the round is the highest round played in this game,
/// it expands the player score lists. At the end it updates the score
/// array for the game.
@@ -351,150 +353,30 @@ class _RoundViewState extends State<RoundView> {
print('====================================');
print('Runde ${widget.roundNumber} beendet');
// The shown round is smaller than the newest round
if (widget.gameSession.roundNumber <
widget.gameSession.playerScores[0].length) {
if (widget.roundNumber < widget.gameSession.roundNumber) {
print('Da diese Runde bereits gespielt wurde, werden die alten '
'Punktestaende ueberschrieben');
print('Alte Punktestaende:');
print(gameSession.printRoundScores(widget.roundNumber));
}
_calculateScoredPoints();
widget.gameSession.sumPoints();
if (widget.gameSession.isGameFinished == true) {
print('Das Spiel ist beendet');
} else {
if (widget.roundNumber >= widget.gameSession.playerScores[0].length - 1) {
gameSession.expandPlayerScoreLists();
print('Das Punkte-Array wurde erweitert');
}
widget.gameSession.increaseRound();
}
print('Die Punktesummen wurden aktualisiert');
}
/// Checks the scores of the current round and assigns points to the players.
/// There are three possible outcomes of a round:
///
/// **Case 1**<br>
/// One player has Kamikaze. This player receives 0 points. Every other player
/// receives 50 points.
///
/// **Case 2**<br>
/// The player who said CABO has the lowest score. They receive 0 points.
/// Every other player gets their round score.
///
/// **Case 3**<br>
/// The player who said CABO does not have the lowest score.
/// They receive 5 extra points added to their round score.
/// Every player with the lowest score gets 0 points.
/// Every other player gets their round score.
void _calculateScoredPoints() {
print('Spieler: ${gameSession.players}');
// A player has Kamikaze
if (_kamikazePlayerIndex != null) {
print('${widget.gameSession.players[_kamikazePlayerIndex!]} hat Kamikaze '
'und bekommt 0 Punkte');
print('Alle anderen Spieler bekommen 50 Punkte');
_applyKamikaze(_kamikazePlayerIndex!,
List.generate(widget.gameSession.players.length, (index) => 0));
widget.gameSession
.applyKamikaze(widget.roundNumber, _kamikazePlayerIndex!);
} else {
// List of the scores of the current round
List<int> roundScores = [];
for (TextEditingController c in _scoreControllerList) {
if (c.text.isNotEmpty) roundScores.add(int.parse(c.text));
}
print('Punkte: $roundScores');
print('${gameSession.players[_caboPlayerIndex]} hat CABO gesagt');
print('${gameSession.players[_caboPlayerIndex]} hat '
'${roundScores[_caboPlayerIndex]} Punkte');
/// List of the index of the player(s) with the lowest score
List<int> lowestScoreIndex = _getLowestScoreIndex(roundScores);
print('Folgende Spieler haben die niedrigsten Punte:');
for (int i in lowestScoreIndex) {
print('${widget.gameSession.players[i]} (${roundScores[i]} Punkte)');
}
// The player who said CABO is one of the players which have the
// fewest points.
if (lowestScoreIndex.contains(_caboPlayerIndex)) {
print('${widget.gameSession.players[_caboPlayerIndex]} hat CABO gesagt '
'und bekommt 0 Punkte');
print('Alle anderen Spieler bekommen ihre Punkte');
_assignPoints([_caboPlayerIndex], -1, roundScores);
} else {
// A player other than the one who said CABO has the fewest points.
print(
'${widget.gameSession.players[_caboPlayerIndex]} hat CABO gesagt, '
'jedoch nicht die wenigsten Punkte.');
print('Folgende:r Spieler haben die wenigsten Punkte:');
for (int i in lowestScoreIndex) {
print('${widget.gameSession.players[i]}: ${roundScores[i]} Punkte');
}
_assignPoints(lowestScoreIndex, _caboPlayerIndex, roundScores);
}
widget.gameSession.calculateScoredPoints(
widget.roundNumber, roundScores, _caboPlayerIndex);
}
}
/// Returns the index of the player with the lowest score. If there are
/// multiple players with the same lowest score, all of them are returned.
/// [roundScores] is a list of the scores of all players in the current round.
List<int> _getLowestScoreIndex(List<int> roundScores) {
int lowestScore = roundScores[0];
List<int> lowestScoreIndex = [0];
for (int i = 1; i < roundScores.length; i++) {
if (roundScores[i] < lowestScore) {
lowestScore = roundScores[i];
lowestScoreIndex = [i];
} else if (roundScores[i] == lowestScore) {
lowestScoreIndex.add(i);
}
widget.gameSession.updatePoints();
if (widget.gameSession.isGameFinished == true) {
print('Das Spiel ist beendet');
} else if (widget.roundNumber == widget.gameSession.roundNumber) {
widget.gameSession.increaseRound();
}
return lowestScoreIndex;
}
/// Assigns 50 points to all players except the kamikaze player.
/// [kamikazePlayerIndex] is the index of the kamikaze player.
/// [roundScores] is the list of the scores of all players in the
/// current round.
void _applyKamikaze(int kamikazePlayerIndex, List<int> roundScores) {
for (int i = 0; i < widget.gameSession.players.length; i++) {
if (i != kamikazePlayerIndex) {
roundScores[i] += 50;
}
}
gameSession.addRoundScoresToScoreList(roundScores, widget.roundNumber);
}
/// Assigns points to the players based on the scores of the current round.
/// [winnerIndex] is the index of the player(s) who receive 0 points
/// [loserIndex] is the index of the player who receives 5 extra points
/// [roundScores] is the raw list of the scores of all players in the
/// current round.
void _assignPoints(
List<int> winnnerIndex, int loserIndex, List<int> roundScores) {
print('Folgende Punkte wurden aus der Runde übernommen:');
for (int i = 0; i < roundScores.length; i++) {
print('${widget.gameSession.players[i]}: ${roundScores[i]}');
}
for (int i in winnnerIndex) {
print(
'${widget.gameSession.players[i]} hat gewonnen und bekommt 0 Punkte');
roundScores[i] = 0;
}
if (loserIndex != -1) {
print('${widget.gameSession.players[loserIndex]} bekommt 5 Fehlerpunkte');
roundScores[loserIndex] += 5;
}
print('Aktualisierte Punkte:');
for (int i = 0; i < roundScores.length; i++) {
print('${widget.gameSession.players[i]}: ${roundScores[i]}');
}
gameSession.addRoundScoresToScoreList(roundScores, widget.roundNumber);
}
@override