Refactoring & comments
This commit is contained in:
@@ -50,20 +50,20 @@ class GameSession {
|
|||||||
|
|
||||||
/// Sets the scores of the players for a specific round.
|
/// Sets the scores of the players for a specific round.
|
||||||
/// This method takes a list of round scores and a round number as parameters.
|
/// This method takes a list of round scores and a round number as parameters.
|
||||||
/// It then replaces the values for the given [roundNumber] in the playerScores.
|
/// It then replaces the values for the given [roundNumber] in the
|
||||||
/// Its important that each index of the [roundScores] list corresponds to the
|
/// playerScores. Its important that each index of the [roundScores] list
|
||||||
/// index of the player in the [playerScores] list.
|
/// corresponds to the index of the player in the [playerScores] list.
|
||||||
void addRoundScoresToScoreList(List<int> roundScores, int roundNumber) {
|
void addRoundScoresToScoreList(List<int> roundScores, int roundNumber) {
|
||||||
for (int i = 0; i < roundScores.length; i++) {
|
for (int i = 0; i < roundScores.length; i++) {
|
||||||
playerScores[i][roundNumber] = (roundScores[i]);
|
playerScores[i][roundNumber] = (roundScores[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Summarizes the points of all players in the first index of their score list.
|
/// Summarizes the points of all players in the first index of their
|
||||||
/// The method clears the first index of each player score list and then sums up
|
/// score list. The method clears the first index of each player score
|
||||||
/// the points from the second index to the last index. It then stores the
|
/// list and then sums up the points from the second index to the last
|
||||||
/// result in the first index.
|
/// index. It then stores the result in the first index. This method is
|
||||||
/// This method is used to update the total points of each player after a round.
|
/// used to update the total points of each player after a round.
|
||||||
void sumPoints() {
|
void sumPoints() {
|
||||||
for (int i = 0; i < playerScores.length; i++) {
|
for (int i = 0; i < playerScores.length; i++) {
|
||||||
playerScores[i][0] = 0;
|
playerScores[i][0] = 0;
|
||||||
|
|||||||
@@ -21,17 +21,18 @@ class _RoundViewState extends State<RoundView> {
|
|||||||
/// Index of the player who said CABO.
|
/// Index of the player who said CABO.
|
||||||
int _caboPlayerIndex = 0;
|
int _caboPlayerIndex = 0;
|
||||||
|
|
||||||
/// Index of the player who has Kamikaze. Default is null (no kamikaze player).
|
/// Index of the player who has Kamikaze.
|
||||||
|
/// Default is null (no Kamikaze player).
|
||||||
int? _kamikazePlayerIndex;
|
int? _kamikazePlayerIndex;
|
||||||
|
|
||||||
/// List of text controllers for the point text fields.
|
/// List of text controllers for the score text fields.
|
||||||
late final List<TextEditingController> _pointControllers = List.generate(
|
late final List<TextEditingController> _scoreControllerList = List.generate(
|
||||||
widget.gameSession.players.length,
|
widget.gameSession.players.length,
|
||||||
(index) => TextEditingController(),
|
(index) => TextEditingController(),
|
||||||
);
|
);
|
||||||
|
|
||||||
/// List of focus nodes for the point text fields.
|
/// List of focus nodes for the score text fields.
|
||||||
late final List<FocusNode> _focusNodes = List.generate(
|
late final List<FocusNode> _focusNodeList = List.generate(
|
||||||
widget.gameSession.players.length,
|
widget.gameSession.players.length,
|
||||||
(index) => FocusNode(),
|
(index) => FocusNode(),
|
||||||
);
|
);
|
||||||
@@ -39,18 +40,17 @@ class _RoundViewState extends State<RoundView> {
|
|||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
print('Runde ${widget.roundNumber} geöffnet');
|
print('Runde ${widget.roundNumber} geöffnet');
|
||||||
print(
|
print('Abgeschlossene Runden: '
|
||||||
'Abgeschlossene Runden: ${widget.gameSession.playerScores[0].length - 1}');
|
'${widget.gameSession.playerScores[0].length - 1}');
|
||||||
print('Aktuelle Runde: ${widget.gameSession.round}');
|
print('Aktuelle Runde: ${widget.gameSession.round}');
|
||||||
if (widget.gameSession.round < widget.gameSession.playerScores[0].length) {
|
if (widget.gameSession.round < widget.gameSession.playerScores[0].length) {
|
||||||
print(
|
print('Die Länge ist ${widget.gameSession.playerScores[0].length} und '
|
||||||
'Die Länge ist ${widget.gameSession.playerScores[0].length} und somit kleiner als '
|
'somit kleiner als die Runde ${widget.gameSession.round}');
|
||||||
'die Runde ${widget.gameSession.round}');
|
|
||||||
|
|
||||||
// If the current round has already been played, the text fields are filled
|
// If the current round has already been played, the text fields
|
||||||
// with the scores from this round
|
// are filled with the scores from this round
|
||||||
for (int i = 0; i < _pointControllers.length; i++) {
|
for (int i = 0; i < _scoreControllerList.length; i++) {
|
||||||
_pointControllers[i].text =
|
_scoreControllerList[i].text =
|
||||||
gameSession.playerScores[i][widget.roundNumber].toString();
|
gameSession.playerScores[i][widget.roundNumber].toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -176,7 +176,7 @@ class _RoundViewState extends State<RoundView> {
|
|||||||
width: 100,
|
width: 100,
|
||||||
child: CupertinoTextField(
|
child: CupertinoTextField(
|
||||||
maxLength: 3,
|
maxLength: 3,
|
||||||
focusNode: _focusNodes[index],
|
focusNode: _focusNodeList[index],
|
||||||
keyboardType:
|
keyboardType:
|
||||||
TextInputType.numberWithOptions(
|
TextInputType.numberWithOptions(
|
||||||
signed: true,
|
signed: true,
|
||||||
@@ -192,7 +192,7 @@ class _RoundViewState extends State<RoundView> {
|
|||||||
1
|
1
|
||||||
? TextInputAction.done
|
? TextInputAction.done
|
||||||
: TextInputAction.next,
|
: TextInputAction.next,
|
||||||
controller: _pointControllers[index],
|
controller: _scoreControllerList[index],
|
||||||
placeholder: 'Punkte',
|
placeholder: 'Punkte',
|
||||||
textAlign: TextAlign.center,
|
textAlign: TextAlign.center,
|
||||||
onSubmitted: (_) =>
|
onSubmitted: (_) =>
|
||||||
@@ -286,9 +286,9 @@ class _RoundViewState extends State<RoundView> {
|
|||||||
/// [index] is the index of the current text field.
|
/// [index] is the index of the current text field.
|
||||||
void _focusNextTextfield(int index) {
|
void _focusNextTextfield(int index) {
|
||||||
if (index < widget.gameSession.players.length - 1) {
|
if (index < widget.gameSession.players.length - 1) {
|
||||||
FocusScope.of(context).requestFocus(_focusNodes[index + 1]);
|
FocusScope.of(context).requestFocus(_focusNodeList[index + 1]);
|
||||||
} else {
|
} else {
|
||||||
_focusNodes[index].unfocus();
|
_focusNodeList[index].unfocus();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -304,7 +304,7 @@ class _RoundViewState extends State<RoundView> {
|
|||||||
/// Checks if any of the text fields for the players points are empty.
|
/// Checks if any of the text fields for the players points are empty.
|
||||||
/// Returns true if any of the text fields is empty, false otherwise.
|
/// Returns true if any of the text fields is empty, false otherwise.
|
||||||
bool _areTextFieldsEmpty() {
|
bool _areTextFieldsEmpty() {
|
||||||
for (TextEditingController t in _pointControllers) {
|
for (TextEditingController t in _scoreControllerList) {
|
||||||
if (t.text.isEmpty) {
|
if (t.text.isEmpty) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -355,7 +355,7 @@ class _RoundViewState extends State<RoundView> {
|
|||||||
void _calculateScoredPoints() {
|
void _calculateScoredPoints() {
|
||||||
// List of the scores of the current round
|
// List of the scores of the current round
|
||||||
List<int> roundScores = [];
|
List<int> roundScores = [];
|
||||||
for (TextEditingController c in _pointControllers) {
|
for (TextEditingController c in _scoreControllerList) {
|
||||||
roundScores.add(int.parse(c.text));
|
roundScores.add(int.parse(c.text));
|
||||||
}
|
}
|
||||||
print('Spieler: ${gameSession.players}');
|
print('Spieler: ${gameSession.players}');
|
||||||
@@ -366,22 +366,24 @@ class _RoundViewState extends State<RoundView> {
|
|||||||
|
|
||||||
/// List of the index of the player(s) with the lowest score
|
/// List of the index of the player(s) with the lowest score
|
||||||
List<int> lowestScoreIndex = _getLowestScoreIndex(roundScores);
|
List<int> lowestScoreIndex = _getLowestScoreIndex(roundScores);
|
||||||
// Spieler der CABO gesagt hat, hat am wenigsten Punkte
|
// A player has Kamikaze
|
||||||
if (_kamikazePlayerIndex != null) {
|
if (_kamikazePlayerIndex != null) {
|
||||||
print('${widget.gameSession.players[_kamikazePlayerIndex!]} hat Kamikaze '
|
print('${widget.gameSession.players[_kamikazePlayerIndex!]} hat Kamikaze '
|
||||||
'und bekommt 0 Punkte');
|
'und bekommt 0 Punkte');
|
||||||
print('Alle anderen Spieler bekommen 50 Punkte');
|
print('Alle anderen Spieler bekommen 50 Punkte');
|
||||||
_applyKamikaze(_kamikazePlayerIndex!, roundScores);
|
_applyKamikaze(_kamikazePlayerIndex!, roundScores);
|
||||||
} else if (lowestScoreIndex.contains(_caboPlayerIndex)) {
|
}
|
||||||
|
// The player who said CABO is one of the players which have the
|
||||||
|
// fewest points.
|
||||||
|
else if (lowestScoreIndex.contains(_caboPlayerIndex)) {
|
||||||
print('${widget.gameSession.players[_caboPlayerIndex]} hat CABO gesagt '
|
print('${widget.gameSession.players[_caboPlayerIndex]} hat CABO gesagt '
|
||||||
'und bekommt 0 Punkte');
|
'und bekommt 0 Punkte');
|
||||||
print('Alle anderen Spieler bekommen ihre Punkte');
|
print('Alle anderen Spieler bekommen ihre Punkte');
|
||||||
_assignPoints([_caboPlayerIndex], -1, roundScores);
|
_assignPoints([_caboPlayerIndex], -1, roundScores);
|
||||||
} else {
|
} else {
|
||||||
// Ein anderer Spieler hat weniger Punkte
|
// A player other than the one who said CABO has the fewest points.
|
||||||
print('${widget.gameSession.players[_caboPlayerIndex]} hat CABO gesagt, '
|
print('${widget.gameSession.players[_caboPlayerIndex]} hat CABO gesagt, '
|
||||||
'jedoch nicht die wenigsten Punkte.');
|
'jedoch nicht die wenigsten Punkte.');
|
||||||
|
|
||||||
print('Folgende:r Spieler haben die wenigsten Punkte:');
|
print('Folgende:r Spieler haben die wenigsten Punkte:');
|
||||||
for (int i in lowestScoreIndex) {
|
for (int i in lowestScoreIndex) {
|
||||||
print('${widget.gameSession.players[i]}: ${roundScores[i]} Punkte');
|
print('${widget.gameSession.players[i]}: ${roundScores[i]} Punkte');
|
||||||
@@ -459,10 +461,10 @@ class _RoundViewState extends State<RoundView> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
for (final controller in _pointControllers) {
|
for (final controller in _scoreControllerList) {
|
||||||
controller.dispose();
|
controller.dispose();
|
||||||
}
|
}
|
||||||
for (final focusNode in _focusNodes) {
|
for (final focusNode in _focusNodeList) {
|
||||||
focusNode.dispose();
|
focusNode.dispose();
|
||||||
}
|
}
|
||||||
super.dispose();
|
super.dispose();
|
||||||
|
|||||||
Reference in New Issue
Block a user