diff --git a/android/gradle.properties b/android/gradle.properties index 9a7fb493bbdeb67a67f6271e0418d25f7e2c9668..4818be8ca52a6781003f81fbeb0e73520e42eba3 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -1,5 +1,5 @@ org.gradle.jvmargs=-Xmx1536M android.useAndroidX=true android.enableJetifier=true -app.versionName=1.0.10 -app.versionCode=11 +app.versionName=1.0.11 +app.versionCode=12 diff --git a/lib/board.dart b/lib/board.dart index 36eaa4f657c56d47f71ffdf7d20129cf8399e1e5..4502f08a237b134104ce4b0f304935b75c221405 100644 --- a/lib/board.dart +++ b/lib/board.dart @@ -3,7 +3,7 @@ import 'package:puissance4/coordinate.dart'; import 'match_page.dart'; class Board { - List<List<Color>> _boxes = List.generate( + List<List<Color?>> _boxes = List.generate( 7, (i) => List.generate( 7, @@ -13,33 +13,34 @@ class Board { Board(); - Board.from(List<List<Color>> boxes) { + Board.from(List<List<Color?>> boxes) { _boxes = boxes; } - Color getBox(Coordinate coordinate) => _boxes[coordinate.col][coordinate.row]; + Color? getBox(Coordinate coordinate) => + _boxes[coordinate.col ?? 0][coordinate.row ?? 0]; - int getColumnTarget(int col) => _boxes[col].lastIndexOf(null); + int getColumnTarget(int? col) => _boxes[col ?? 0].lastIndexOf(null); - void setBox(Coordinate coordinate, Color player) => - _boxes[coordinate.col][coordinate.row] = player; + void setBox(Coordinate coordinate, Color? player) => + _boxes[coordinate.col ?? 0][coordinate.row ?? 0] = player; void reset() { _boxes.forEach((r) => r.forEach((p) => p = null)); } - bool checkWinner(Coordinate coordinate, Color player) { + bool checkWinner(Coordinate coordinate, Color? player) { return checkHorizontally(coordinate, player) || checkVertically(coordinate, player) || checkDiagonally(coordinate, player); } - bool checkHorizontally(Coordinate coordinate, Color player) { + bool checkHorizontally(Coordinate coordinate, Color? player) { var r = 0; for (; - coordinate.col + r < 7 && + (coordinate.col ?? 0) + r < 7 && r < 4 && - getBox(coordinate.copyWith(col: coordinate.col + r)) == player; + getBox(coordinate.copyWith(col: (coordinate.col ?? 0) + r)) == player; ++r) {} if (r >= 4) { return true; @@ -47,9 +48,9 @@ class Board { var l = 0; for (; - coordinate.col - l >= 0 && + (coordinate.col ?? 0) - l >= 0 && l < 4 && - getBox(coordinate.copyWith(col: coordinate.col - l)) == player; + getBox(coordinate.copyWith(col: (coordinate.col ?? 0) - l)) == player; ++l) {} if (l >= 4 || l + r >= 5) { return true; @@ -58,15 +59,15 @@ class Board { return false; } - bool checkDiagonally(Coordinate coordinate, Color player) { + bool checkDiagonally(Coordinate coordinate, Color? player) { var ur = 0; for (; - coordinate.col + ur < 7 && - coordinate.row + ur < 7 && + (coordinate.col ?? 0) + ur < 7 && + (coordinate.row ?? 0) + ur < 7 && ur < 4 && getBox(coordinate.copyWith( - col: coordinate.col + ur, - row: coordinate.row + ur, + col: (coordinate.col ?? 0) + ur, + row: (coordinate.row ?? 0) + ur, )) == player; ++ur) {} @@ -75,12 +76,12 @@ class Board { } var dl = 0; for (; - coordinate.col - dl >= 0 && - coordinate.row - dl >= 0 && + (coordinate.col ?? 0) - dl >= 0 && + (coordinate.row ?? 0) - dl >= 0 && dl < 4 && getBox(coordinate.copyWith( - col: coordinate.col - dl, - row: coordinate.row - dl, + col: (coordinate.col ?? 0) - dl, + row: (coordinate.row ?? 0) - dl, )) == player; ++dl) {} @@ -90,12 +91,12 @@ class Board { var dr = 0; for (; - coordinate.col + dr < 7 && - coordinate.row - dr >= 0 && + (coordinate.col ?? 0) + dr < 7 && + (coordinate.row ?? 0) - dr >= 0 && dr < 4 && getBox(coordinate.copyWith( - col: coordinate.col + dr, - row: coordinate.row - dr, + col: (coordinate.col ?? 0) + dr, + row: (coordinate.row ?? 0) - dr, )) == player; ++dr) {} @@ -105,12 +106,12 @@ class Board { var ul = 0; for (; - coordinate.col - ul >= 0 && - coordinate.row + ul < 7 && + (coordinate.col ?? 0) - ul >= 0 && + (coordinate.row ?? 0) + ul < 7 && ul < 4 && getBox(coordinate.copyWith( - col: coordinate.col - ul, - row: coordinate.row + ul, + col: (coordinate.col ?? 0) - ul, + row: (coordinate.row ?? 0) + ul, )) == player; ++ul) {} @@ -120,13 +121,13 @@ class Board { return false; } - bool checkVertically(Coordinate coordinate, Color player) { + bool checkVertically(Coordinate coordinate, Color? player) { var u = 0; for (; - coordinate.row + u < 7 && + (coordinate.row ?? 0) + u < 7 && u < 4 && getBox(coordinate.copyWith( - row: coordinate.row + u, + row: (coordinate.row ?? 0) + u, )) == player; ++u) {} @@ -135,10 +136,10 @@ class Board { } var d = 0; for (; - coordinate.row - d >= 0 && + (coordinate.row ?? 0) - d >= 0 && d < 4 && getBox(coordinate.copyWith( - row: coordinate.row - d, + row: (coordinate.row ?? 0) - d, )) == player; ++d) {} diff --git a/lib/coordinate.dart b/lib/coordinate.dart index c8cbca43fa33b8bcc32daca8e61f42f5975621b0..3d137c74442be8a8baf6d4d9c26e06edb46d6040 100644 --- a/lib/coordinate.dart +++ b/lib/coordinate.dart @@ -1,5 +1,5 @@ class Coordinate { - final int row, col; + final int? row, col; Coordinate( this.col, @@ -7,8 +7,8 @@ class Coordinate { ); Coordinate copyWith({ - int col, - int row, + int? col, + int? row, }) => Coordinate( col ?? this.col, diff --git a/lib/cpu.dart b/lib/cpu.dart index cd5af2c2b18ec5dc9e8e0fe9d9be9feef94a26ce..7f91cf9445106aba094d3c6c0c14df873a0f3075 100644 --- a/lib/cpu.dart +++ b/lib/cpu.dart @@ -37,13 +37,13 @@ class HarderCpu extends Cpu { @override Future<int> chooseCol(Board board) async { - final List<double> scores = List.filled(7, 0); + final List<double?> scores = List.filled(7, 0.0); await Future.delayed(Duration(seconds: 1 + _random.nextInt(2))); return _compute(board, 0, 1, scores); } - int _compute(Board board, int step, int deepness, List<double> scores) { + int _compute(Board board, int step, int deepness, List<double?> scores) { for (var i = 0; i < 7; ++i) { final boardCopy = board.clone(); @@ -57,7 +57,7 @@ class HarderCpu extends Cpu { boardCopy.setBox(coordinate, color); if (boardCopy.checkWinner(coordinate, color)) { - scores[i] += deepness / (step + 1); + scores[i] = (scores[i] ?? 0) + deepness / (step + 1); continue; } @@ -71,7 +71,7 @@ class HarderCpu extends Cpu { boardCopy.setBox(coordinate, otherPlayer); if (boardCopy.checkWinner(coordinate, otherPlayer)) { - scores[i] -= deepness / (step + 1); + scores[i] = (scores[i] ?? 0) - deepness / (step + 1); continue; } @@ -84,11 +84,11 @@ class HarderCpu extends Cpu { return _getBestScoreIndex(scores); } - int _getBestScoreIndex(List<double> scores) { + int _getBestScoreIndex(List<double?> scores) { int bestScoreIndex = scores.indexWhere((s) => s != null); scores.asMap().forEach((index, score) { if (score != null && - (score > scores[bestScoreIndex] || + (score > (scores[bestScoreIndex] ?? 0) || (score == scores[bestScoreIndex] && _random.nextBool()))) { bestScoreIndex = index; } @@ -105,7 +105,7 @@ class HardestCpu extends HarderCpu { @override Future<int> chooseCol(Board board) async { - final List<double> scores = List.filled(7, 0); + final List<double?> scores = List.filled(7, 0); await Future.delayed(Duration(seconds: 2 + _random.nextInt(2))); return _compute(board, 0, 4, scores); diff --git a/lib/cpu_level_page.dart b/lib/cpu_level_page.dart index 0554af0e844af689ca8be72e65dcd761287bc14e..579158f7fbbef265c5f315fb364e47ce9cdfd0cf 100644 --- a/lib/cpu_level_page.dart +++ b/lib/cpu_level_page.dart @@ -19,15 +19,15 @@ class CpuLevelPage extends StatelessWidget { crossAxisAlignment: CrossAxisAlignment.center, mainAxisSize: MainAxisSize.max, children: <Widget>[ - FlatButton( - color: Colors.yellow, - padding: EdgeInsets.all(15), + TextButton( + style: TextButton.styleFrom( + backgroundColor: Colors.yellow, + padding: EdgeInsets.all(15), + ), child: Text( 'βΊοΈ FACILE', - style: Theme.of(context) - .textTheme - .headline4 - .copyWith(color: Colors.black), + style: + Theme.of(context).textTheme.headline4?.copyWith(color: Colors.black), ), onPressed: () { Navigator.pushNamed( @@ -35,21 +35,20 @@ class CpuLevelPage extends StatelessWidget { '/match', arguments: { 'mode': Mode.PVC, - 'cpu': - DumbCpu(Random().nextBool() ? Color.RED : Color.YELLOW), + 'cpu': DumbCpu(Random().nextBool() ? Color.RED : Color.YELLOW), }, ); }, ), - FlatButton( - color: Colors.red, - padding: EdgeInsets.all(15), + TextButton( + style: TextButton.styleFrom( + backgroundColor: Colors.red, + padding: EdgeInsets.all(15), + ), child: Text( 'π€ DIFFICILE', - style: Theme.of(context) - .textTheme - .headline4 - .copyWith(color: Colors.white), + style: + Theme.of(context).textTheme.headline4?.copyWith(color: Colors.white), ), onPressed: () { Navigator.pushNamed( @@ -57,21 +56,20 @@ class CpuLevelPage extends StatelessWidget { '/match', arguments: { 'mode': Mode.PVC, - 'cpu': HarderCpu( - Random().nextBool() ? Color.RED : Color.YELLOW), + 'cpu': HarderCpu(Random().nextBool() ? Color.RED : Color.YELLOW), }, ); }, ), - FlatButton( - color: Colors.deepPurpleAccent, - padding: EdgeInsets.all(15), + TextButton( + style: TextButton.styleFrom( + backgroundColor: Colors.deepPurpleAccent, + padding: EdgeInsets.all(15), + ), child: Text( 'π€― TRES DIFFICILE', - style: Theme.of(context) - .textTheme - .headline4 - .copyWith(color: Colors.white), + style: + Theme.of(context).textTheme.headline4?.copyWith(color: Colors.white), ), onPressed: () { Navigator.pushNamed( @@ -79,8 +77,7 @@ class CpuLevelPage extends StatelessWidget { '/match', arguments: { 'mode': Mode.PVC, - 'cpu': HardestCpu( - Random().nextBool() ? Color.RED : Color.YELLOW), + 'cpu': HardestCpu(Random().nextBool() ? Color.RED : Color.YELLOW), }, ); }, diff --git a/lib/game_chip.dart b/lib/game_chip.dart index 1c737c3dd63e62cec0fa5a704d352deab644830f..65178cfa51503d63248f354e71b60dc589d42121 100644 --- a/lib/game_chip.dart +++ b/lib/game_chip.dart @@ -4,13 +4,13 @@ import 'match_page.dart'; class GameChip extends StatelessWidget { const GameChip({ - Key key, + Key? key, this.translation, - @required this.color, + this.color, }) : super(key: key); - final Animation<double> translation; - final Color color; + final Animation<double>? translation; + final Color? color; @override Widget build(BuildContext context) { diff --git a/lib/home_page.dart b/lib/home_page.dart index 7676f7eda26246c55bd6700b9395a76b648669c2..1db42a57d616fa9a285e63e2cfcf8e4c5603828b 100644 --- a/lib/home_page.dart +++ b/lib/home_page.dart @@ -16,15 +16,15 @@ class HomePage extends StatelessWidget { crossAxisAlignment: CrossAxisAlignment.center, mainAxisSize: MainAxisSize.max, children: <Widget>[ - FlatButton( - color: Colors.green, - padding: EdgeInsets.all(15), + TextButton( + style: TextButton.styleFrom( + backgroundColor: Colors.green, + padding: EdgeInsets.all(15), + ), child: Text( 'π§ 2 JOUEURS π§', - style: Theme.of(context) - .textTheme - .headline4 - .copyWith(color: Colors.white), + style: + Theme.of(context).textTheme.headline4?.copyWith(color: Colors.white), ), onPressed: () { Navigator.pushNamed( @@ -36,15 +36,15 @@ class HomePage extends StatelessWidget { ); }, ), - FlatButton( - color: Colors.orange, - padding: EdgeInsets.all(15), + TextButton( + style: TextButton.styleFrom( + backgroundColor: Colors.orange, + padding: EdgeInsets.all(15), + ), child: Text( 'π§ 1 JOUEUR π€', - style: Theme.of(context) - .textTheme - .headline4 - .copyWith(color: Colors.white), + style: + Theme.of(context).textTheme.headline4?.copyWith(color: Colors.white), ), onPressed: () { Navigator.pushNamed( @@ -56,15 +56,15 @@ class HomePage extends StatelessWidget { ); }, ), - FlatButton( - color: Colors.white, - padding: EdgeInsets.all(15), + TextButton( + style: TextButton.styleFrom( + backgroundColor: Colors.white, + padding: EdgeInsets.all(15), + ), child: Text( 'π€ DEMO π€', - style: Theme.of(context) - .textTheme - .headline4 - .copyWith(color: Colors.black), + style: + Theme.of(context).textTheme.headline4?.copyWith(color: Colors.black), ), onPressed: () { final harderCpu = diff --git a/lib/main.dart b/lib/main.dart index 19115a1ab134e8abfe9b50d25aa53f337d4720b2..09f7a1e6585f2a03598dbf558883a6d0a31760fe 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -14,7 +14,7 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( - title: 'Flutter fiar', + title: 'Puissance 4', theme: ThemeData( primarySwatch: Colors.blue, ), diff --git a/lib/match_page.dart b/lib/match_page.dart index db231b42baaf1b45a852dfb9c1d9562b6a6362c0..e18dbae318a0962d18a5c2902075f97394ae6545 100644 --- a/lib/match_page.dart +++ b/lib/match_page.dart @@ -21,14 +21,14 @@ enum Mode { class MatchPage extends StatefulWidget { final Mode mode; - final Cpu cpu; - final Cpu cpu2; + final Cpu? cpu; + final Cpu? cpu2; const MatchPage({ - Key key, - this.mode, - this.cpu, - this.cpu2, + Key? key, + required this.mode, + required this.cpu, + required this.cpu2, }) : super(key: key); @override @@ -37,10 +37,10 @@ class MatchPage extends StatefulWidget { class _MatchPageState extends State<MatchPage> with TickerProviderStateMixin { final board = Board(); - Color turn; - Color winner; + Color? turn; + Color? winner; - List<List<Animation<double>>> translations = List.generate( + List<List<Animation<double>?>> translations = List.generate( 7, (i) => List.generate( 7, @@ -73,7 +73,6 @@ class _MatchPageState extends State<MatchPage> with TickerProviderStateMixin { child: Padding( padding: const EdgeInsets.only(top: 32.0), child: Stack( - overflow: Overflow.clip, fit: StackFit.loose, children: <Widget>[ Positioned.fill( @@ -99,7 +98,7 @@ class _MatchPageState extends State<MatchPage> with TickerProviderStateMixin { style: Theme.of(context) .textTheme .headline6 - .copyWith(color: Colors.white), + ?.copyWith(color: Colors.white), ) : Column( children: <Widget>[ @@ -109,11 +108,13 @@ class _MatchPageState extends State<MatchPage> with TickerProviderStateMixin { style: Theme.of(context) .textTheme .headline5 - .copyWith(color: Colors.white), + ?.copyWith(color: Colors.white), ), Padding( padding: const EdgeInsets.all(8.0), - child: GameChip(color: turn), + child: GameChip( + color: turn, + ), ), _buildPlayerName(context), ], @@ -130,7 +131,7 @@ class _MatchPageState extends State<MatchPage> with TickerProviderStateMixin { String name; if (widget.mode == Mode.PVC) { - if (turn == widget.cpu.color) { + if (turn == widget.cpu?.color) { name = 'CPU - ${widget.cpu.toString()}'; } else { name = 'USER'; @@ -142,7 +143,7 @@ class _MatchPageState extends State<MatchPage> with TickerProviderStateMixin { name = 'PLAYER2'; } } else { - if (turn == widget.cpu.color) { + if (turn == widget.cpu?.color) { name = 'CPU1 - ${widget.cpu.toString()}'; } else { name = 'CPU2 - ${widget.cpu2.toString()}'; @@ -151,22 +152,18 @@ class _MatchPageState extends State<MatchPage> with TickerProviderStateMixin { return Text( name, textAlign: TextAlign.center, - style: Theme.of(context) - .textTheme - .headline5 - .copyWith(color: Colors.white), + style: Theme.of(context).textTheme.headline5?.copyWith(color: Colors.white), ); } @override void initState() { super.initState(); - turn = widget.cpu?.otherPlayer ?? - (Random().nextBool() ? Color.RED : Color.YELLOW); - if (widget.mode == Mode.PVC && turn == widget.cpu.color) { + turn = Random().nextBool() ? Color.RED : Color.YELLOW; + if (widget.mode == Mode.PVC && turn == widget.cpu?.color) { cpuMove(widget.cpu); } else if (widget.mode == Mode.DEMO) { - if (turn == widget.cpu.color) { + if (turn == widget.cpu?.color) { cpuMove(widget.cpu); } else { cpuMove(widget.cpu2); @@ -179,8 +176,7 @@ class _MatchPageState extends State<MatchPage> with TickerProviderStateMixin { padding: const EdgeInsets.all(0), shrinkWrap: true, physics: NeverScrollableScrollPhysics(), - gridDelegate: - SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 7), + gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 7), childrenDelegate: SliverChildBuilderDelegate( (context, i) { final col = i % 7; @@ -204,8 +200,7 @@ class _MatchPageState extends State<MatchPage> with TickerProviderStateMixin { return GridView.custom( padding: const EdgeInsets.all(0), physics: NeverScrollableScrollPhysics(), - gridDelegate: - SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 7), + gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 7), shrinkWrap: true, childrenDelegate: SliverChildBuilderDelegate( (context, i) { @@ -236,12 +231,12 @@ class _MatchPageState extends State<MatchPage> with TickerProviderStateMixin { } } - void cpuMove(Cpu cpu) async { - int col = await cpu.chooseCol(board); + void cpuMove(Cpu? cpu) async { + int? col = await cpu?.chooseCol(board); putChip(col); if (winner == null && widget.mode == Mode.DEMO) { - if (turn == widget.cpu.color) { + if (turn == widget.cpu?.color) { cpuMove(widget.cpu); } else { cpuMove(widget.cpu2); @@ -249,7 +244,7 @@ class _MatchPageState extends State<MatchPage> with TickerProviderStateMixin { } } - void putChip(int col) { + void putChip(int? col) { final target = board.getColumnTarget(col); final player = turn; @@ -273,7 +268,7 @@ class _MatchPageState extends State<MatchPage> with TickerProviderStateMixin { }); } - translations[col][target] = Tween( + translations[col ?? 0][target] = Tween( begin: 0.0, end: 1.0, ).animate(CurvedAnimation( @@ -293,7 +288,7 @@ class _MatchPageState extends State<MatchPage> with TickerProviderStateMixin { } } - void showWinnerDialog(BuildContext context, Color player) { + void showWinnerDialog(BuildContext context, Color? player) { setState(() { winner = player; }); diff --git a/pubspec.lock b/pubspec.lock index 3317f5f8760ca76e88858ef443c38d580faf416e..7d27b51980f8e290e295bf9d2a7e3802ccf92fb8 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -42,14 +42,14 @@ packages: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.15.0" + version: "1.16.0" fake_async: dependency: transitive description: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.0" flutter: dependency: "direct main" description: flutter @@ -73,7 +73,7 @@ packages: name: material_color_utilities url: "https://pub.dartlang.org" source: hosted - version: "0.1.3" + version: "0.1.4" meta: dependency: transitive description: @@ -87,7 +87,7 @@ packages: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.0" + version: "1.8.1" sky_engine: dependency: transitive description: flutter @@ -99,7 +99,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.1" + version: "1.8.2" stack_trace: dependency: transitive description: @@ -134,20 +134,13 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.4.8" - typed_data: - dependency: transitive - description: - name: typed_data - url: "https://pub.dartlang.org" - source: hosted - version: "1.3.0" + version: "0.4.9" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.1" + version: "2.1.2" sdks: - dart: ">=2.14.0 <3.0.0" + dart: ">=2.17.0-0 <3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 1867edeee01aba780127508b152e6a4feb936cb1..d6521c6afefc496d365d0cde9d1cbf64f0e91ee7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ publish_to: 'none' version: 1.0.0+1 environment: - sdk: ">=2.7.0 <3.0.0" + sdk: ">=2.12.0 <3.0.0" dependencies: flutter: