Starting implementation of the bowling game algorithm
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
parent
f56e66708b
commit
09adc10a76
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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 {
|
|
||||||
int _firstThrow;
|
class Round extends AbstractRound{
|
||||||
int? _secondThrow;
|
|
||||||
int _points;
|
GamePlayer gamePlayer;
|
||||||
Player _player;
|
|
||||||
|
Round(super.firstThrow, super.secondThrow, super.points, super.player, this.gamePlayer);
|
||||||
// Constructor
|
|
||||||
Round(this._firstThrow, this._secondThrow, this._points, this._player);
|
@override
|
||||||
|
bool computeNext(int val) {
|
||||||
// Getters and setters
|
if(firstThrow==null){
|
||||||
int get firstThrow => _firstThrow;
|
firstThrow=val;
|
||||||
|
if(previousRound?.isSpare() ?? false){
|
||||||
set firstThrow(int value) {
|
previousRound?.update(val);
|
||||||
_firstThrow = value;
|
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;
|
|
||||||
|
|
||||||
set points(int value) {
|
if(isSpareOrStrike()){
|
||||||
_points = value;
|
gamePlayer.onSpareOrStrike();
|
||||||
|
}
|
||||||
|
unsubscribe();
|
||||||
}
|
}
|
||||||
|
|
||||||
Player get player => _player;
|
|
||||||
|
|
||||||
set player(Player value) {
|
|
||||||
_player = value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue