diff --git a/lib/data/game_session.dart b/lib/data/game_session.dart index 36629f8..1523469 100644 --- a/lib/data/game_session.dart +++ b/lib/data/game_session.dart @@ -82,7 +82,7 @@ class GameSession { } } addRoundScoresToList( - roundNum, roundScores, scoreUpdates, kamikazePlayerIndex); + roundNum, roundScores, scoreUpdates, 0, kamikazePlayerIndex); } /// Checks the scores of the current round and assigns points to the players. @@ -116,7 +116,7 @@ class GameSession { print('${players[caboPlayerIndex]} hat CABO gesagt ' 'und bekommt 0 Punkte'); print('Alle anderen Spieler bekommen ihre Punkte'); - _assignPoints(roundNum, roundScores, [caboPlayerIndex]); + _assignPoints(roundNum, roundScores, caboPlayerIndex, [caboPlayerIndex]); } else { // A player other than the one who said CABO has the fewest points. print('${players[caboPlayerIndex]} hat CABO gesagt, ' @@ -125,7 +125,8 @@ class GameSession { for (int i in lowestScoreIndex) { print('${players[i]}: ${roundScores[i]} Punkte'); } - _assignPoints(roundNum, roundScores, lowestScoreIndex, caboPlayerIndex); + _assignPoints(roundNum, roundScores, caboPlayerIndex, lowestScoreIndex, + caboPlayerIndex); } } @@ -151,8 +152,9 @@ class GameSession { /// [roundNum] is the number of the current round. /// [roundScores] is the raw list of the scores of all players in the current round. /// [winnerIndex] is the index of the player who receives 5 extra points - void _assignPoints(int roundNum, List roundScores, List winnerIndex, - [int loserIndex = -1]) { + void _assignPoints(int roundNum, List roundScores, int caboPlayerIndex, + List winnerIndex, + [int? loserIndex]) { /// List of the updates for every player score List scoreUpdates = [...roundScores]; print('Folgende Punkte wurden aus der Runde übernommen:'); @@ -163,7 +165,7 @@ class GameSession { print('${players[i]} hat gewonnen und bekommt 0 Punkte'); scoreUpdates[i] = 0; } - if (loserIndex != -1) { + if (loserIndex != null) { print('${players[loserIndex]} bekommt 5 Fehlerpunkte'); scoreUpdates[loserIndex] += 5; } @@ -172,7 +174,7 @@ class GameSession { print('${players[i]}: ${scoreUpdates[i]}'); } print('scoreUpdates: $scoreUpdates, roundScores: $roundScores'); - addRoundScoresToList(roundNum, roundScores, scoreUpdates); + addRoundScoresToList(roundNum, roundScores, scoreUpdates, caboPlayerIndex); } /// Sets the scores of the players for a specific round. @@ -181,13 +183,18 @@ class GameSession { /// playerScores. Its important that each index of the [roundScores] list /// corresponds to the index of the player in the [playerScores] list. void addRoundScoresToList( - int roundNum, List roundScores, List scoreUpdates, - [int? kamikazePlayerIndex]) { + int roundNum, + List roundScores, + List scoreUpdates, + int caboPlayerIndex, [ + int? kamikazePlayerIndex, + ]) { Round newRound = Round( roundNum: roundNum, + caboPlayerIndex: caboPlayerIndex, + kamikazePlayerIndex: kamikazePlayerIndex, scores: roundScores, scoreUpdates: scoreUpdates, - kamikazePlayerIndex: kamikazePlayerIndex, ); if (roundNum > roundList.length) { roundList.add(newRound); diff --git a/lib/data/round.dart b/lib/data/round.dart index 0c5b456..dcc5d9f 100644 --- a/lib/data/round.dart +++ b/lib/data/round.dart @@ -9,34 +9,40 @@ import 'package:cabo_counter/data/game_session.dart'; /// kamikaze, this value is null. class Round { final int roundNum; + final int caboPlayerIndex; + final int? kamikazePlayerIndex; final List scores; final List scoreUpdates; - final int? kamikazePlayerIndex; - Round( - {required this.roundNum, - required this.scores, - required this.scoreUpdates, - this.kamikazePlayerIndex}); + Round({ + required this.roundNum, + required this.caboPlayerIndex, + this.kamikazePlayerIndex, + required this.scores, + required this.scoreUpdates, + }); @override toString() { - return 'Round $roundNum: scores: $scores, scoreUpdates: $scoreUpdates, ' - 'kamikazePlayerIndex: $kamikazePlayerIndex'; + return 'Round $roundNum, caboPlayerIndex: $caboPlayerIndex, ' + 'kamikazePlayerIndex: $kamikazePlayerIndex, scores: $scores, ' + 'scoreUpdates: $scoreUpdates, '; } /// Converts the Round object to a JSON map. Map toJson() => { 'roundNum': roundNum, + 'caboPlayerIndex': caboPlayerIndex, + 'kamikazePlayerIndex': kamikazePlayerIndex, 'scores': scores, 'scoreUpdates': scoreUpdates, - 'kamikazePlayerIndex': kamikazePlayerIndex, }; /// Creates a Round object from a JSON map. Round.fromJson(Map json) : roundNum = json['roundNum'], + caboPlayerIndex = json['caboPlayerIndex'], + kamikazePlayerIndex = json['kamikazePlayerIndex'], scores = List.from(json['scores']), - scoreUpdates = List.from(json['scoreUpdates']), - kamikazePlayerIndex = json['kamikazePlayerIndex']; + scoreUpdates = List.from(json['scoreUpdates']); } diff --git a/lib/views/round_view.dart b/lib/views/round_view.dart index 7e5ea96..f22b8c1 100644 --- a/lib/views/round_view.dart +++ b/lib/views/round_view.dart @@ -53,6 +53,8 @@ class _RoundViewState extends State { _scoreControllerList[i].text = gameSession.roundList[widget.roundNumber - 1].scores[i].toString(); } + _caboPlayerIndex = + gameSession.roundList[widget.roundNumber - 1].caboPlayerIndex; _kamikazePlayerIndex = gameSession.roundList[widget.roundNumber - 1].kamikazePlayerIndex; }