Starting implementation of the bowling game algorithm
continuous-integration/drone/push Build is failing Details

Arthur_BowlingAlgo
Arthur VALIN 2 years ago
parent f56e66708b
commit 09adc10a76

@ -1,3 +1,4 @@
import 'package:bowl_in/widgets/button_new_party.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
@ -40,6 +41,12 @@ final GoRouter router = GoRouter(
return InGameScreen(); return InGameScreen();
}, },
), ),
GoRoute(
path: 'scoreboard',
builder: (BuildContext context, GoRouterState state) {
return FinalScoreBoard();
},
),
], ],
), ),
], ],

@ -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;
}
}

@ -85,7 +85,7 @@ class GameDetail {
if (player.id == id) { if (player.id == id) {
for (var element in rounds) { for (var element in rounds) {
if (element.player == player) { if (element.player == player) {
pointPlayer += element.points; pointPlayer += element.points ?? 0;
} }
} }
return pointPlayer; return pointPlayer;
@ -98,7 +98,7 @@ class GameDetail {
Map<int, int> rank = {}; Map<int, int> rank = {};
for (var player in players) { for (var player in players) {
rank.addAll({player.id: this.getPointByPlayerId(player.id)}); rank.addAll({player.id: getPointByPlayerId(player.id)});
} }
var sortedByKeyMap = Map.fromEntries( var sortedByKeyMap = Map.fromEntries(
rank.entries.toList()..sort((e1, e2) => e2.value.compareTo(e1.value))); rank.entries.toList()..sort((e1, e2) => e2.value.compareTo(e1.value)));

@ -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]);
}
}
}

@ -1,3 +1,5 @@
import 'package:bowl_in/model/GamePlayer.dart';
import 'User.dart'; import 'User.dart';
import 'Game.dart'; import 'Game.dart';
import 'IUserManager.dart'; import 'IUserManager.dart';
@ -7,6 +9,7 @@ import 'Game.dart';
abstract class IManager { abstract class IManager {
late User _userCurrent; late User _userCurrent;
late Game _gameCurrent; late Game _gameCurrent;
late GamePlayer _gamePlayer;
late IUserManager _userMgr; late IUserManager _userMgr;
late IGameManager _gameMgr; late IGameManager _gameMgr;
@ -17,6 +20,12 @@ abstract class IManager {
_userCurrent = user; _userCurrent = user;
} }
GamePlayer get gamePlayer => _gamePlayer;
set gamePlayer(GamePlayer value) {
_gamePlayer = value;
}
Game get gameCurrent => _gameCurrent; Game get gameCurrent => _gameCurrent;
IUserManager get userMgr => _userMgr; IUserManager get userMgr => _userMgr;
IGameManager get gameMgr => _gameMgr; IGameManager get gameMgr => _gameMgr;

@ -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();
}
}

@ -1,36 +1,42 @@
import 'Player.dart'; import 'package:bowl_in/model/AbstractRound.dart';
import 'GamePlayer.dart';
class Round { class Round extends AbstractRound{
int _firstThrow;
int? _secondThrow;
int _points;
Player _player;
// Constructor GamePlayer gamePlayer;
Round(this._firstThrow, this._secondThrow, this._points, this._player);
// Getters and setters Round(super.firstThrow, super.secondThrow, super.points, super.player, this.gamePlayer);
int get firstThrow => _firstThrow;
set firstThrow(int value) { @override
_firstThrow = value; 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) { if(previousRound?.isStrike() ?? false){
_secondThrow = value; previousRound?.update(points ?? 0);
} }
int get points => _points; if(isSpareOrStrike()){
gamePlayer.onSpareOrStrike();
set points(int value) { }
_points = value; unsubscribe();
} }
Player get player => _player;
set player(Player value) {
_player = value;
}
} }

@ -142,32 +142,32 @@ class StubData extends IManager {
List<Round> _rounds = []; List<Round> _rounds = [];
void _initRounds() { void _initRounds() {
rounds.add(Round(4, 5, 9, players[1])); rounds.add(Round(4, 5, 9, players[1], gamePlayer));
rounds.add(Round(4, 1, 5, players[8])); rounds.add(Round(4, 1, 5, players[8], gamePlayer));
rounds.add(Round(4, 5, 9, players[7])); rounds.add(Round(4, 5, 9, players[7], gamePlayer));
rounds.add(Round(4, 1, 5, players[1])); rounds.add(Round(4, 1, 5, players[1], gamePlayer));
rounds.add(Round(4, 5, 9, players[7])); rounds.add(Round(4, 5, 9, players[7], gamePlayer));
rounds.add(Round(1, 5, 6, players[8])); rounds.add(Round(1, 5, 6, players[8], gamePlayer));
rounds.add(Round(4, 1, 5, players[7])); rounds.add(Round(4, 1, 5, players[7], gamePlayer));
rounds.add(Round(1, 5, 6, players[8])); rounds.add(Round(1, 5, 6, players[8], gamePlayer));
rounds.add(Round(4, 5, 9, players[7])); rounds.add(Round(4, 5, 9, players[7], gamePlayer));
rounds.add(Round(9, 0, 9, players[8])); rounds.add(Round(9, 0, 9, players[8], gamePlayer));
rounds.add(Round(4, 5, 9, players[7])); rounds.add(Round(4, 5, 9, players[7], gamePlayer));
rounds.add(Round(4, 5, 9, players[8])); rounds.add(Round(4, 5, 9, players[8], gamePlayer));
rounds.add(Round(4, 2, 6, players[0])); rounds.add(Round(4, 2, 6, players[0], gamePlayer));
rounds.add(Round(3, 5, 8, players[1])); rounds.add(Round(3, 5, 8, players[1], gamePlayer));
rounds.add(Round(4, 5, 9, players[2])); rounds.add(Round(4, 5, 9, players[2], gamePlayer));
rounds.add(Round(4, 5, 9, players[4])); rounds.add(Round(4, 5, 9, players[4], gamePlayer));
rounds.add(Round(1, 5, 6, players[6])); rounds.add(Round(1, 5, 6, players[6], gamePlayer));
rounds.add(Round(4, 5, 9, players[8])); rounds.add(Round(4, 5, 9, players[8], gamePlayer));
rounds.add(Round(4, 2, 6, players[7])); rounds.add(Round(4, 2, 6, players[7], gamePlayer));
rounds.add(Round(3, 5, 8, players[8])); rounds.add(Round(3, 5, 8, players[8], gamePlayer));
rounds.add(Round(4, 5, 9, players[7])); rounds.add(Round(4, 5, 9, players[7], gamePlayer));
rounds.add(Round(4, 5, 9, players[8])); rounds.add(Round(4, 5, 9, players[8], gamePlayer));
rounds.add(Round(1, 5, 6, players[7])); rounds.add(Round(1, 5, 6, players[7], gamePlayer));
rounds.add(Round(4, 5, 9, players[8])); rounds.add(Round(4, 5, 9, players[8], gamePlayer));
rounds.add(Round(4, 5, 9, players[7])); rounds.add(Round(4, 5, 9, players[7], gamePlayer));
rounds.add(Round(4, 1, 5, players[8])); rounds.add(Round(4, 1, 5, players[8], gamePlayer));
} }
List<Round> get rounds => _rounds; List<Round> get rounds => _rounds;

@ -196,7 +196,8 @@ packages:
dependency: "direct main" dependency: "direct main"
description: description:
name: intl name: intl
url: "https://pub.dartlang.org" sha256: a3715e3bc90294e971cb7dc063fbf3cd9ee0ebf8604ffeafabd9e6f16abbdbe6
url: "https://pub.dev"
source: hosted source: hosted
version: "0.18.0" version: "0.18.0"
js: js:

Loading…
Cancel
Save