From 09adc10a76c8ba989986f7a0d524e7d87f684fe6 Mon Sep 17 00:00:00 2001 From: "arthur.valin" Date: Mon, 13 Mar 2023 09:58:24 +0100 Subject: [PATCH] Starting implementation of the bowling game algorithm --- .../bowlin_project/lib/config/app_router.dart | 7 ++ .../lib/model/AbstractRound.dart | 74 +++++++++++++++++++ .../bowlin_project/lib/model/GameDetail.dart | 4 +- .../bowlin_project/lib/model/GamePlayer.dart | 30 ++++++++ .../bowlin_project/lib/model/IManager.dart | 9 +++ .../bowlin_project/lib/model/LastRound.dart | 48 ++++++++++++ Sources/bowlin_project/lib/model/Round.dart | 62 +++++++++------- .../lib/model/StubManager/StubData.dart | 52 ++++++------- Sources/bowlin_project/pubspec.lock | 3 +- 9 files changed, 232 insertions(+), 57 deletions(-) create mode 100644 Sources/bowlin_project/lib/model/AbstractRound.dart create mode 100644 Sources/bowlin_project/lib/model/GamePlayer.dart create mode 100644 Sources/bowlin_project/lib/model/LastRound.dart diff --git a/Sources/bowlin_project/lib/config/app_router.dart b/Sources/bowlin_project/lib/config/app_router.dart index a443419..08f332e 100644 --- a/Sources/bowlin_project/lib/config/app_router.dart +++ b/Sources/bowlin_project/lib/config/app_router.dart @@ -1,3 +1,4 @@ +import 'package:bowl_in/widgets/button_new_party.dart'; import 'package:flutter/cupertino.dart'; import 'package:go_router/go_router.dart'; @@ -40,6 +41,12 @@ final GoRouter router = GoRouter( return InGameScreen(); }, ), + GoRoute( + path: 'scoreboard', + builder: (BuildContext context, GoRouterState state) { + return FinalScoreBoard(); + }, + ), ], ), ], diff --git a/Sources/bowlin_project/lib/model/AbstractRound.dart b/Sources/bowlin_project/lib/model/AbstractRound.dart new file mode 100644 index 0000000..f91756c --- /dev/null +++ b/Sources/bowlin_project/lib/model/AbstractRound.dart @@ -0,0 +1,74 @@ +import 'Player.dart'; + +abstract class AbstractRound { + int? _firstThrow; + int? _secondThrow; + int? _points; + Player _player; + + AbstractRound? _previousRound; + + // Constructor + AbstractRound(this._firstThrow, this._secondThrow, this._points, this._player); + + // Getters and setters + int? get firstThrow => _firstThrow; + + set firstThrow(int? value) { + _firstThrow = value; + } + + int? get secondThrow => _secondThrow; + + set secondThrow(int? value) { + _secondThrow = value; + } + + int? get points => _points; + + set points(int? value) { + _points = value; + } + + Player get player => _player; + + set player(Player value) { + _player = value; + } + + AbstractRound? get previousRound => _previousRound; + + set previousRound(AbstractRound? value) { + _previousRound = value; + } + + bool computeNext(int val); + void computePoints(); + + bool isStrike() { + return firstThrow==10; + } + + bool isSpare(){ + return firstThrow!=10 && (firstThrow ?? 0)+(secondThrow ?? 0)==10; + } + + bool isSpareOrStrike() { + return (firstThrow ?? 0)+(secondThrow ?? 0)==10; + } + + void subscribe(AbstractRound nextRound){ + nextRound.previousRound=this; + } + + void update(int val){ + points = (points ?? 0) + val; + previousRound?.update(val); + } + + void unsubscribe(){ + previousRound?.unsubscribe(); + previousRound=null; + } + +} diff --git a/Sources/bowlin_project/lib/model/GameDetail.dart b/Sources/bowlin_project/lib/model/GameDetail.dart index 41134e8..afee8e4 100644 --- a/Sources/bowlin_project/lib/model/GameDetail.dart +++ b/Sources/bowlin_project/lib/model/GameDetail.dart @@ -85,7 +85,7 @@ class GameDetail { if (player.id == id) { for (var element in rounds) { if (element.player == player) { - pointPlayer += element.points; + pointPlayer += element.points ?? 0; } } return pointPlayer; @@ -98,7 +98,7 @@ class GameDetail { Map rank = {}; for (var player in players) { - rank.addAll({player.id: this.getPointByPlayerId(player.id)}); + rank.addAll({player.id: getPointByPlayerId(player.id)}); } var sortedByKeyMap = Map.fromEntries( rank.entries.toList()..sort((e1, e2) => e2.value.compareTo(e1.value))); diff --git a/Sources/bowlin_project/lib/model/GamePlayer.dart b/Sources/bowlin_project/lib/model/GamePlayer.dart new file mode 100644 index 0000000..7ca30de --- /dev/null +++ b/Sources/bowlin_project/lib/model/GamePlayer.dart @@ -0,0 +1,30 @@ +import 'package:flutter/cupertino.dart'; + +import 'GameDetail.dart'; +import 'package:go_router/go_router.dart'; + +class GamePlayer{ + + final GameDetail game; + int currentRoundIndex = 0; + final BuildContext context; + + GamePlayer(this.game, this.context); + + void onNext(bool isRoundFinished){ + if(isRoundFinished){ + currentRoundIndex++; + } + if(currentRoundIndex>game.rounds.length){ + context.go("/scoreboard"); + }else{ + context.go("/in-game", extra: game.rounds[currentRoundIndex]); + } + } + + void onSpareOrStrike(){ + if(currentRoundIndex>=game.rounds.length-game.players.length){ + game.rounds[currentRoundIndex].subscribe(game.rounds[currentRoundIndex+game.players.length]); + } + } +} \ No newline at end of file diff --git a/Sources/bowlin_project/lib/model/IManager.dart b/Sources/bowlin_project/lib/model/IManager.dart index f30f996..0ef1191 100644 --- a/Sources/bowlin_project/lib/model/IManager.dart +++ b/Sources/bowlin_project/lib/model/IManager.dart @@ -1,3 +1,5 @@ +import 'package:bowl_in/model/GamePlayer.dart'; + import 'User.dart'; import 'Game.dart'; import 'IUserManager.dart'; @@ -7,6 +9,7 @@ import 'Game.dart'; abstract class IManager { late User _userCurrent; late Game _gameCurrent; + late GamePlayer _gamePlayer; late IUserManager _userMgr; late IGameManager _gameMgr; @@ -17,6 +20,12 @@ abstract class IManager { _userCurrent = user; } + GamePlayer get gamePlayer => _gamePlayer; + + set gamePlayer(GamePlayer value) { + _gamePlayer = value; + } + Game get gameCurrent => _gameCurrent; IUserManager get userMgr => _userMgr; IGameManager get gameMgr => _gameMgr; diff --git a/Sources/bowlin_project/lib/model/LastRound.dart b/Sources/bowlin_project/lib/model/LastRound.dart new file mode 100644 index 0000000..018aae7 --- /dev/null +++ b/Sources/bowlin_project/lib/model/LastRound.dart @@ -0,0 +1,48 @@ +import 'package:bowl_in/model/AbstractRound.dart'; + +import 'Player.dart'; + +class LastRound extends AbstractRound{ + int? _thirdThrow; + + LastRound(super.firstThrow, super.secondThrow, super.points, super.player, this._thirdThrow); + + int? get thirdThrow => _thirdThrow; + + set thirdThrow(int? value) { + _thirdThrow = value; + } + + @override + bool computeNext(int val) { + if(firstThrow==null){ + firstThrow=val; + if(previousRound?.isSpare() ?? false){ + previousRound?.update(val); + } + return false; + }else if(secondThrow!=null){ + secondThrow=val; + return false; + + }else if((firstThrow??0)+(secondThrow??0)>=10){ + thirdThrow=val; + return false; + + } + computePoints(); + return true; + } + + @override + void computePoints() { + points = (firstThrow??0)+(secondThrow??0)+(thirdThrow??0); + + if(previousRound?.isStrike()??false){ + update(points??0); + } + unsubscribe(); + } + + +} diff --git a/Sources/bowlin_project/lib/model/Round.dart b/Sources/bowlin_project/lib/model/Round.dart index 3e21c60..1ef4406 100644 --- a/Sources/bowlin_project/lib/model/Round.dart +++ b/Sources/bowlin_project/lib/model/Round.dart @@ -1,36 +1,42 @@ -import 'Player.dart'; - -class Round { - int _firstThrow; - int? _secondThrow; - int _points; - Player _player; - - // Constructor - Round(this._firstThrow, this._secondThrow, this._points, this._player); - - // Getters and setters - int get firstThrow => _firstThrow; - - set firstThrow(int value) { - _firstThrow = value; +import 'package:bowl_in/model/AbstractRound.dart'; +import 'GamePlayer.dart'; + +class Round extends AbstractRound{ + + GamePlayer gamePlayer; + + Round(super.firstThrow, super.secondThrow, super.points, super.player, this.gamePlayer); + + @override + bool computeNext(int val) { + if(firstThrow==null){ + firstThrow=val; + if(previousRound?.isSpare() ?? false){ + previousRound?.update(val); + unsubscribe(); + } + return false; //Le round n'est pas fini + }else if(firstThrow==10){ + secondThrow=val; + return false; //Le round n'est pas fini + } + computePoints(); + return true; //Le round est fini } - int? get secondThrow => _secondThrow; + @override + void computePoints() { + points = (firstThrow ?? 0)+(secondThrow ?? 0); - set secondThrow(int? value) { - _secondThrow = value; - } - - int get points => _points; + if(previousRound?.isStrike() ?? false){ + previousRound?.update(points ?? 0); + } - set points(int value) { - _points = value; + if(isSpareOrStrike()){ + gamePlayer.onSpareOrStrike(); + } + unsubscribe(); } - Player get player => _player; - set player(Player value) { - _player = value; - } } diff --git a/Sources/bowlin_project/lib/model/StubManager/StubData.dart b/Sources/bowlin_project/lib/model/StubManager/StubData.dart index f5c2c70..2029842 100644 --- a/Sources/bowlin_project/lib/model/StubManager/StubData.dart +++ b/Sources/bowlin_project/lib/model/StubManager/StubData.dart @@ -142,32 +142,32 @@ class StubData extends IManager { List _rounds = []; void _initRounds() { - rounds.add(Round(4, 5, 9, players[1])); - rounds.add(Round(4, 1, 5, players[8])); - rounds.add(Round(4, 5, 9, players[7])); - rounds.add(Round(4, 1, 5, players[1])); - rounds.add(Round(4, 5, 9, players[7])); - rounds.add(Round(1, 5, 6, players[8])); - rounds.add(Round(4, 1, 5, players[7])); - rounds.add(Round(1, 5, 6, players[8])); - rounds.add(Round(4, 5, 9, players[7])); - rounds.add(Round(9, 0, 9, players[8])); - rounds.add(Round(4, 5, 9, players[7])); - rounds.add(Round(4, 5, 9, players[8])); - rounds.add(Round(4, 2, 6, players[0])); - rounds.add(Round(3, 5, 8, players[1])); - rounds.add(Round(4, 5, 9, players[2])); - rounds.add(Round(4, 5, 9, players[4])); - rounds.add(Round(1, 5, 6, players[6])); - rounds.add(Round(4, 5, 9, players[8])); - rounds.add(Round(4, 2, 6, players[7])); - rounds.add(Round(3, 5, 8, players[8])); - rounds.add(Round(4, 5, 9, players[7])); - rounds.add(Round(4, 5, 9, players[8])); - rounds.add(Round(1, 5, 6, players[7])); - rounds.add(Round(4, 5, 9, players[8])); - rounds.add(Round(4, 5, 9, players[7])); - rounds.add(Round(4, 1, 5, players[8])); + rounds.add(Round(4, 5, 9, players[1], gamePlayer)); + rounds.add(Round(4, 1, 5, players[8], gamePlayer)); + rounds.add(Round(4, 5, 9, players[7], gamePlayer)); + rounds.add(Round(4, 1, 5, players[1], gamePlayer)); + rounds.add(Round(4, 5, 9, players[7], gamePlayer)); + rounds.add(Round(1, 5, 6, players[8], gamePlayer)); + rounds.add(Round(4, 1, 5, players[7], gamePlayer)); + rounds.add(Round(1, 5, 6, players[8], gamePlayer)); + rounds.add(Round(4, 5, 9, players[7], gamePlayer)); + rounds.add(Round(9, 0, 9, players[8], gamePlayer)); + rounds.add(Round(4, 5, 9, players[7], gamePlayer)); + rounds.add(Round(4, 5, 9, players[8], gamePlayer)); + rounds.add(Round(4, 2, 6, players[0], gamePlayer)); + rounds.add(Round(3, 5, 8, players[1], gamePlayer)); + rounds.add(Round(4, 5, 9, players[2], gamePlayer)); + rounds.add(Round(4, 5, 9, players[4], gamePlayer)); + rounds.add(Round(1, 5, 6, players[6], gamePlayer)); + rounds.add(Round(4, 5, 9, players[8], gamePlayer)); + rounds.add(Round(4, 2, 6, players[7], gamePlayer)); + rounds.add(Round(3, 5, 8, players[8], gamePlayer)); + rounds.add(Round(4, 5, 9, players[7], gamePlayer)); + rounds.add(Round(4, 5, 9, players[8], gamePlayer)); + rounds.add(Round(1, 5, 6, players[7], gamePlayer)); + rounds.add(Round(4, 5, 9, players[8], gamePlayer)); + rounds.add(Round(4, 5, 9, players[7], gamePlayer)); + rounds.add(Round(4, 1, 5, players[8], gamePlayer)); } List get rounds => _rounds; diff --git a/Sources/bowlin_project/pubspec.lock b/Sources/bowlin_project/pubspec.lock index 67e2bc8..ee6d354 100644 --- a/Sources/bowlin_project/pubspec.lock +++ b/Sources/bowlin_project/pubspec.lock @@ -196,7 +196,8 @@ packages: dependency: "direct main" description: name: intl - url: "https://pub.dartlang.org" + sha256: a3715e3bc90294e971cb7dc063fbf3cd9ee0ebf8604ffeafabd9e6f16abbdbe6 + url: "https://pub.dev" source: hosted version: "0.18.0" js: