Commit 85a332db authored by Timothé KOBAK's avatar Timothé KOBAK

AHHHHHHHHHHHHHHHHHHHHHHHH

parent 8c7e59ae
...@@ -3,6 +3,7 @@ import 'exo7_taquin.dart'; ...@@ -3,6 +3,7 @@ import 'exo7_taquin.dart';
class Exo7 extends StatelessWidget { class Exo7 extends StatelessWidget {
const Exo7({super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
...@@ -18,42 +19,48 @@ class Exo7 extends StatelessWidget { ...@@ -18,42 +19,48 @@ class Exo7 extends StatelessWidget {
style: TextStyle() style: TextStyle()
) )
), ),
TextButton( Center(
child: TextButton(
style: TextButton.styleFrom( style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 20), textStyle: const TextStyle(fontSize: 20),
), ),
onPressed: () { onPressed: () {
Navigator.push( Navigator.push(
context, context,
MaterialPageRoute(builder: (context) => const SizeChoose(difficulty: 1)), MaterialPageRoute(builder: (context) => const SizeChoose(difficulty: 10)),
); );
}, },
child: const Text('Easy'), child: const Text('Easy'),
), ),
TextButton( ),
Center(
child: TextButton(
style: TextButton.styleFrom( style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 20), textStyle: const TextStyle(fontSize: 20),
), ),
onPressed: () { onPressed: () {
Navigator.push( Navigator.push(
context, context,
MaterialPageRoute(builder: (context) => const SizeChoose(difficulty: 2)), MaterialPageRoute(builder: (context) => const SizeChoose(difficulty: 20)),
); );
}, },
child: const Text('Medium'), child: const Text('Medium'),
), ),
TextButton( ),
Center(
child: TextButton(
style: TextButton.styleFrom( style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 20), textStyle: const TextStyle(fontSize: 20),
), ),
onPressed: () { onPressed: () {
Navigator.push( Navigator.push(
context, context,
MaterialPageRoute(builder: (context) => const SizeChoose(difficulty: 3)), MaterialPageRoute(builder: (context) => const SizeChoose(difficulty: 30)),
); );
}, },
child: const Text('Hard'), child: const Text('Hard'),
), ),
),
]) ])
); );
} }
...@@ -79,7 +86,8 @@ class SizeChoose extends StatelessWidget { ...@@ -79,7 +86,8 @@ class SizeChoose extends StatelessWidget {
style: TextStyle() style: TextStyle()
) )
), ),
TextButton( Center(
child: TextButton(
style: TextButton.styleFrom( style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 20), textStyle: const TextStyle(fontSize: 20),
), ),
...@@ -91,7 +99,9 @@ class SizeChoose extends StatelessWidget { ...@@ -91,7 +99,9 @@ class SizeChoose extends StatelessWidget {
}, },
child: const Text('2 par 2'), child: const Text('2 par 2'),
), ),
TextButton( ),
Center(
child: TextButton(
style: TextButton.styleFrom( style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 20), textStyle: const TextStyle(fontSize: 20),
), ),
...@@ -103,7 +113,9 @@ class SizeChoose extends StatelessWidget { ...@@ -103,7 +113,9 @@ class SizeChoose extends StatelessWidget {
}, },
child: const Text('3 par 3'), child: const Text('3 par 3'),
), ),
TextButton( ),
Center(
child: TextButton(
style: TextButton.styleFrom( style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 20), textStyle: const TextStyle(fontSize: 20),
), ),
...@@ -115,7 +127,9 @@ class SizeChoose extends StatelessWidget { ...@@ -115,7 +127,9 @@ class SizeChoose extends StatelessWidget {
}, },
child: const Text('4 par 4'), child: const Text('4 par 4'),
), ),
TextButton( ),
Center(
child: TextButton(
style: TextButton.styleFrom( style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 20), textStyle: const TextStyle(fontSize: 20),
), ),
...@@ -126,6 +140,7 @@ class SizeChoose extends StatelessWidget { ...@@ -126,6 +140,7 @@ class SizeChoose extends StatelessWidget {
); );
}, },
child: const Text('5 par 5'), child: const Text('5 par 5'),
),
) )
]) ])
); );
......
...@@ -23,8 +23,8 @@ class _Taquin extends State<Taquin> { ...@@ -23,8 +23,8 @@ class _Taquin extends State<Taquin> {
List<Key> initialOrder = <Key>[]; List<Key> initialOrder = <Key>[];
double? size = 300; double? size = 300;
int emptyTileIndex = 0; int emptyTileIndex = 0;
int score=0;
late int score;
late String imageUrl; late String imageUrl;
late Image image; late Image image;
late int colNb; late int colNb;
...@@ -41,6 +41,7 @@ class _Taquin extends State<Taquin> { ...@@ -41,6 +41,7 @@ class _Taquin extends State<Taquin> {
colNb = widget.colNb; colNb = widget.colNb;
difficulty = widget.difficulty; difficulty = widget.difficulty;
score = difficulty * colNb ;
tiles = buildImage(); tiles = buildImage();
moveTiles(); moveTiles();
...@@ -71,9 +72,9 @@ class _Taquin extends State<Taquin> { ...@@ -71,9 +72,9 @@ class _Taquin extends State<Taquin> {
onTap: () { onTap: () {
setState(() { setState(() {
swapTiles(k); swapTiles(k);
win(); isWon();
if(isValidTile(tiles.indexOf(tiles.singleWhere((element) => element.key == k)))){ if(isValidTile(tiles.indexOf(tiles.singleWhere((element) => element.key == k)))){
score++; score--;
} }
}); });
} }
...@@ -90,7 +91,7 @@ class _Taquin extends State<Taquin> { ...@@ -90,7 +91,7 @@ class _Taquin extends State<Taquin> {
} }
void moveTiles(){ void moveTiles(){
for(int i=0; i < math.pow(colNb, difficulty + 1); i++) { for(int i=0; i < colNb * difficulty; i++) {
var validTiles = getValidTiles(); var validTiles = getValidTiles();
swapTiles( swapTiles(
...@@ -99,31 +100,78 @@ class _Taquin extends State<Taquin> { ...@@ -99,31 +100,78 @@ class _Taquin extends State<Taquin> {
} }
} }
void _showWinDialog(BuildContext context){ void _showWinDialog(BuildContext context, int winscore){
showDialog( showDialog(
context: context, context: context,
builder: (BuildContext context){ builder: (BuildContext context){
return AlertDialog( return AlertDialog(
title: const Text('Congratulations !'), title: const Text('Congratulations !'),
content: const Text('You won !'), content: Column(
actions: <Widget> [ mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('You won ! With a score of $winscore'),
SizedBox(height: 20), // Add some space between text and image
image,
],
),
actions: <Widget>[
TextButton( TextButton(
onPressed: () { onPressed: () {
Navigator.pop(context); Navigator.pop(context);
}, },
child: const Text('Close'), child: const Text('Close'),
),TextButton(
onPressed: () { setState(() {
stopwatch = const Duration();
stopwatchTimer?.cancel();
stopwatchTimer = null;
score = difficulty * colNb;
image.image.evict();
image = new Image.network('https://picsum.photos/512');
tiles = buildImage();
moveTiles();
});
Navigator.pop(context);
), },
child: const Text('Restart'),
)
], ],
); );
},
);
} }
void _showHintDialog(BuildContext context){
showDialog(
context: context,
builder: (BuildContext context){
return AlertDialog(
title: const Text('Good luck !'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('This is the image you are trying to recompose :'),
SizedBox(height: 20), // Add some space between text and image
image,
],
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Close'),
),
],
);
},
); );
} }
bool win(){
bool isWon(){
for(int i = 1; i < tiles.length; i++){ for(int i = 1; i < tiles.length; i++){
if (tiles[i].key != initialOrder[i]){ if (tiles[i].key != initialOrder[i]){
...@@ -132,7 +180,13 @@ class _Taquin extends State<Taquin> { ...@@ -132,7 +180,13 @@ class _Taquin extends State<Taquin> {
} }
//print("It's a win"); //print("It's a win");
_showWinDialog(context); stopwatch = const Duration();
stopwatchTimer?.cancel();
stopwatchTimer = null;
final winscore = score - 1;
_showWinDialog(context, winscore);
return true; return true;
} }
...@@ -190,7 +244,7 @@ class _Taquin extends State<Taquin> { ...@@ -190,7 +244,7 @@ class _Taquin extends State<Taquin> {
timeWidget = stopwatchTimer != null ? Center( timeWidget = stopwatchTimer != null ? Center(
child: Text.rich( child: Text.rich(
TextSpan( TextSpan(
text: stopwatch.toString(), // default text style text: stopwatch.toString().substring(0, 9), // default text style
style: const TextStyle() style: const TextStyle()
) )
) )
...@@ -206,45 +260,54 @@ class _Taquin extends State<Taquin> { ...@@ -206,45 +260,54 @@ class _Taquin extends State<Taquin> {
'Score: $score', 'Score: $score',
style: const TextStyle(fontSize: 20), style: const TextStyle(fontSize: 20),
), ),
const SizedBox(height: 5),
Row( Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly, mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [ children: [
ElevatedButton( ElevatedButton(
onPressed: () { onPressed: () {
setState(() { setState(() {
stopwatch = const Duration();
stopwatchTimer?.cancel();
stopwatchTimer = null;
score = difficulty * colNb;
image.image.evict();
image = new Image.network('https://picsum.photos/512');
colNb = widget.colNb;
difficulty = widget.difficulty;
image = Image.network('https://picsum.photos/512');
tiles = buildImage(); tiles = buildImage();
moveTiles(); moveTiles();
}); });
// Add functionality for button 1
}, },
child: const Text('Restart'), child: const Text('Restart'),
), ),
ElevatedButton( ElevatedButton(
onPressed: () { onPressed: () {
// Add functionality for button 1
setState(() { setState(() {
stopwatchTimer = Timer.periodic(const Duration(milliseconds: 100), (timer) {
_showHintDialog(context);
});
},
child: const Text('HINT'),
),
ElevatedButton(
onPressed: () {
const d = Duration(milliseconds: 100);
setState(() {
stopwatchTimer = Timer.periodic(d, (timer) {
setState(() { setState(() {
stopwatch = stopwatch + const Duration(milliseconds: 100); stopwatch = stopwatch + d;
}); });
}); });
}); });
}, },
child: const Text('Stopwatch'), child: const Text('Stopwatch'),
), ),
// TODO : PLUS DE BOUTO
], ],
), ),
const SizedBox(height: 5),
Row( Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly, mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [ children: [
...@@ -253,7 +316,7 @@ class _Taquin extends State<Taquin> { ...@@ -253,7 +316,7 @@ class _Taquin extends State<Taquin> {
getImage().then((_) { getImage().then((_) {
setState(() {}); setState(() {});
}); });
score=0; score = difficulty * colNb;
}, },
child: const Text('Image From Galeria'), child: const Text('Image From Galeria'),
), ),
...@@ -262,7 +325,7 @@ class _Taquin extends State<Taquin> { ...@@ -262,7 +325,7 @@ class _Taquin extends State<Taquin> {
getImageFromCamera().then((_) { getImageFromCamera().then((_) {
setState(() {}); setState(() {});
}); });
score=0; score = difficulty * colNb;
}, },
child: const Text('Image from camera'), child: const Text('Image from camera'),
), ),
...@@ -271,11 +334,12 @@ class _Taquin extends State<Taquin> { ...@@ -271,11 +334,12 @@ class _Taquin extends State<Taquin> {
], ],
), ),
const SizedBox(height: 5),
Container( Container(
margin: const EdgeInsets.all(10.0), margin: const EdgeInsets.all(10.0),
child: SizedBox( child: SizedBox(
height: MediaQuery.of(context).size.height * 0.7, height: MediaQuery.of(context).size.height * 0.5,
width: MediaQuery.of(context).size.height * 0.7, width: MediaQuery.of(context).size.height * 0.5,
child: GridView.builder( child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: colNb, crossAxisCount: colNb,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment