Commit 936f6a4b authored by Benjamin LEROUX's avatar Benjamin LEROUX

compteur et retour arriere

parent db74f242
import 'package:flutter/material.dart';
import 'Tile.dart';
import 'dart:math';
class TileWidget extends StatelessWidget {
Tile tile;
bool isUsable=false;
TextStyle textStyle = TextStyle(fontSize: 10, fontWeight: FontWeight.bold,color: Colors.red,);
String nbrText="";
TileWidget(this.tile,this.isUsable);
TileWidget(this.tile,this.isUsable,this.nbrText);
@override
Widget build(BuildContext context) {
return Container(
return Stack(
children: <Widget>[
Container(
child: Align(
child: tile.croppedImageTile(),
alignment: Alignment.center,
)
),
Center(child: Text(nbrText,style: textStyle,)),
]
);
}
}
......@@ -44,17 +52,27 @@ List<Tile> createListTile(double size){
return res;
}
List<TileWidget>createTileWidgets(double slide, int emptyIndex, List<Tile> listTiles){
String getRightText(bool play, int i){
String res="";
if(play)res= i.toString();
return res;
}
List<TileWidget>createTileWidgets(double slide, int emptyIndex, List<Tile> listTiles,bool play, bool nombres){
List<TileWidget> tiles = <TileWidget>[];
int max=slide.toInt()*slide.toInt();
int ligne=slide.toInt();
for(int i=0;i<max;i++){
if(i==emptyIndex){
if(i==emptyIndex && play){
tiles.add(TileWidget(Tile(imageURL:"https://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Fond_blanc.svg/1200px-Fond_blanc.svg.png",
alignment: Alignment(-1,-1),size: slide),false));
}else if(i==emptyIndex+1 || i==emptyIndex+slide.toInt()){
tiles.add(TileWidget(listTiles[i],true));
alignment: Alignment(-1,-1),size: slide),false,""));
}else if(((i==emptyIndex+1 && i%ligne!=0) ||
i==emptyIndex+ligne ||
(i==emptyIndex-1 && (i+1)%ligne!=0)||
i==emptyIndex-ligne) && play){
tiles.add(TileWidget(listTiles[i],true,getRightText(nombres, i)));
}
else tiles.add(TileWidget(listTiles[i],false));
else tiles.add(TileWidget(listTiles[i],false,getRightText(nombres, i)));
}
return tiles;
}
......@@ -69,9 +87,15 @@ class JeuDuTaquin extends StatefulWidget {
class JeuDuTaquinState extends State<JeuDuTaquin> {
TextStyle textStyle = TextStyle(fontSize: 16, fontWeight: FontWeight.bold,);
var appBar= AppBar(title: Text('Jeu Du Taquin'),);
double _currentSlideValue=4;
List<TileWidget> tiles=createTileWidgets(4, 0,createListTile(4));
int emptyIndex=0;
List<TileWidget> tiles=createTileWidgets(3, 4,createListTile(3),false,false);
double _currentSlideValue=3;
double _currentDifficultyValue=100;
bool isPlaying=false;
bool _numbers=false;
int emptyIndex=4;
int previousEmptyIndex=-1;
int count=0;
Random random = new Random();
@override
Widget build(BuildContext context) {
......@@ -80,7 +104,7 @@ class JeuDuTaquinState extends State<JeuDuTaquin> {
body: ListView(children: [
SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height*0.7,
height: MediaQuery.of(context).size.height*0.6,
child: Container(
child:GridView.builder(
padding: const EdgeInsets.all(10),
......@@ -96,6 +120,8 @@ class JeuDuTaquinState extends State<JeuDuTaquin> {
child:InkWell(
child: tiles[index],
onTap: (){
previousEmptyIndex=emptyIndex; //on garde le coup précédent
count++; //on incremente le compteur de coup
swapTiles(index,emptyIndex);
},
),
......@@ -104,7 +130,39 @@ class JeuDuTaquinState extends State<JeuDuTaquin> {
),
),
),
Row(children: <Widget>[
isPlaying? Container(height: 40,child:Align(
child:Text("Compteur de coups: "+count.toString(),style: textStyle,),
alignment: Alignment.center,
)): new Container(),
isPlaying? Align(
child:RaisedButton(
onPressed: () {
if(previousEmptyIndex!=-1){
count--;
swapTiles(previousEmptyIndex, emptyIndex);
}
},
child: const Text('Annuler Action', style: TextStyle(fontSize: 20)),
),
alignment: Alignment.center,
): new Container(),
!isPlaying? Row(children: <Widget>[
Container(width: 90,child:Text('Mélanges:', style: textStyle,textAlign: TextAlign.center,),padding: EdgeInsets.fromLTRB(8, 0, 0, 0),),
Container(height:((MediaQuery.of(context).size.height)/7)-appBar.preferredSize.height,width: MediaQuery.of(context).size.width-90,child: Slider(
value: _currentDifficultyValue,
min: 20, max: 220, divisions: 10,
activeColor: Colors.blue,
inactiveColor: Colors.grey,
label: _currentDifficultyValue.round().toString(),
onChanged: (double value) {
setState(() {
if(!isPlaying)_currentDifficultyValue = value;
});
}),
),
]
): new Container(),
!isPlaying? Row(children: <Widget>[
Container(width: 90,child:Text('Taille:', style: textStyle,textAlign: TextAlign.center,),padding: EdgeInsets.fromLTRB(8, 0, 0, 0),),
Container(height:((MediaQuery.of(context).size.height)/7)-appBar.preferredSize.height,width: MediaQuery.of(context).size.width-90,child: Slider(
value: _currentSlideValue,
......@@ -112,16 +170,47 @@ class JeuDuTaquinState extends State<JeuDuTaquin> {
label: _currentSlideValue.round().toString(),
onChanged: (double value) {
setState(() {
if(!isPlaying){
_currentSlideValue = value;
tiles=createTileWidgets(_currentSlideValue,emptyIndex,createListTile(_currentSlideValue));
if(_currentSlideValue%2==1)emptyIndex=((_currentSlideValue*_currentSlideValue)-1)~/2; //si impair alors milieu
else emptyIndex=random.nextInt((_currentSlideValue*_currentSlideValue).toInt()); //sinon aleatoire
tiles=createTileWidgets(_currentSlideValue,emptyIndex,createListTile(_currentSlideValue),isPlaying,_numbers);
}
});
}),
),
]
): new Container(),
!isPlaying? Row(mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(width: 100,child:Text('Afficher les nombres:', style: textStyle, textAlign: TextAlign.center,),padding: EdgeInsets.fromLTRB(8, 0, 0, 0),),
Container(height:((MediaQuery.of(context).size.height)/7)-appBar.preferredSize.height,width: 100,child: Checkbox(
value: _numbers,
onChanged: (value) {
setState(() {
if(!isPlaying)_numbers = !_numbers;
});
}
),
),
]
): new Container(),
],),
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
floatingActionButton: new FloatingActionButton(
onPressed:(){
setState(() {
if(!isPlaying){ //lancement du jeu
isPlaying=true;
tiles=createTileWidgets(_currentSlideValue,emptyIndex,createListTile(_currentSlideValue),isPlaying,_numbers);
shuffle(_currentDifficultyValue.toInt());
}else reset();
});
},
tooltip: 'Play',
child: new Icon(isPlaying? Icons.cancel:Icons.play_arrow),
),
],
)
);
}
......@@ -151,11 +240,60 @@ class JeuDuTaquinState extends State<JeuDuTaquin> {
}else print("Can't move this");
//ajout des bordures rouges
if(emptyIndex>=_currentSlideValue) tiles[emptyIndex-_currentSlideValue.toInt()].isUsable=true;
if(emptyIndex<(_currentSlideValue*(_currentSlideValue-1))) tiles[emptyIndex+_currentSlideValue.toInt()].isUsable=true;
if((emptyIndex+1)%_currentSlideValue!=0)tiles[emptyIndex+1].isUsable=true;
if(emptyIndex%_currentSlideValue!=0)tiles[emptyIndex-1].isUsable=true;
if(emptyIndex>=_currentSlideValue) tiles[emptyIndex-_currentSlideValue.toInt()].isUsable=true; //haut
if(emptyIndex<(_currentSlideValue*(_currentSlideValue-1))) tiles[emptyIndex+_currentSlideValue.toInt()].isUsable=true; //bas
if((emptyIndex+1)%_currentSlideValue!=0)tiles[emptyIndex+1].isUsable=true; //droite
if(emptyIndex%_currentSlideValue!=0)tiles[emptyIndex-1].isUsable=true; //gauche
});
}
shuffle(int difficulty){
Random random = new Random();
for (int i=0;i<difficulty;i++){
switch (random.nextInt(4)){
case 0: {
if(emptyIndex>=_currentSlideValue) {
swapTiles(emptyIndex - _currentSlideValue.toInt(), emptyIndex);
}
else i--;
}
break;
case 1: {
if((emptyIndex+1)%_currentSlideValue!=0) {
swapTiles(emptyIndex + 1, emptyIndex);;
}
else i--;
}
break;
case 2: {
if(emptyIndex<(_currentSlideValue*(_currentSlideValue-1))) {
swapTiles(emptyIndex + _currentSlideValue.toInt(), emptyIndex);
}
else i--;
}
break;
case 3: {
if(emptyIndex%_currentSlideValue!=0) {
swapTiles(emptyIndex - 1, emptyIndex);
}
else i--;
}
break;
default: {}
break;
}
}
}
reset(){
isPlaying=false;
count=0;
previousEmptyIndex=-1;
tiles=createTileWidgets(_currentSlideValue,emptyIndex,createListTile(_currentSlideValue),isPlaying,_numbers);
}
}
\ No newline at end of file
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