Commit 86c2180f authored by Benjamin LEROUX's avatar Benjamin LEROUX

Victory commit

parent 936f6a4b
...@@ -7,8 +7,9 @@ class TileWidget extends StatelessWidget { ...@@ -7,8 +7,9 @@ class TileWidget extends StatelessWidget {
bool isUsable=false; bool isUsable=false;
TextStyle textStyle = TextStyle(fontSize: 10, fontWeight: FontWeight.bold,color: Colors.red,); TextStyle textStyle = TextStyle(fontSize: 10, fontWeight: FontWeight.bold,color: Colors.red,);
String nbrText=""; String nbrText="";
int index;
TileWidget(this.tile,this.isUsable,this.nbrText); TileWidget(this.tile,this.isUsable,this.nbrText,this.index);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
...@@ -65,14 +66,14 @@ List<TileWidget>createTileWidgets(double slide, int emptyIndex, List<Tile> listT ...@@ -65,14 +66,14 @@ List<TileWidget>createTileWidgets(double slide, int emptyIndex, List<Tile> listT
for(int i=0;i<max;i++){ for(int i=0;i<max;i++){
if(i==emptyIndex && play){ 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", 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,"")); alignment: Alignment(-1,-1),size: slide),false,"",i));
}else if(((i==emptyIndex+1 && i%ligne!=0) || }else if(((i==emptyIndex+1 && i%ligne!=0) ||
i==emptyIndex+ligne || i==emptyIndex+ligne ||
(i==emptyIndex-1 && (i+1)%ligne!=0)|| (i==emptyIndex-1 && (i+1)%ligne!=0)||
i==emptyIndex-ligne) && play){ i==emptyIndex-ligne) && play){
tiles.add(TileWidget(listTiles[i],true,getRightText(nombres, i))); tiles.add(TileWidget(listTiles[i],true,getRightText(nombres, i),i));
} }
else tiles.add(TileWidget(listTiles[i],false,getRightText(nombres, i))); else tiles.add(TileWidget(listTiles[i],false,getRightText(nombres, i),i));
} }
return tiles; return tiles;
} }
...@@ -86,12 +87,14 @@ class JeuDuTaquin extends StatefulWidget { ...@@ -86,12 +87,14 @@ class JeuDuTaquin extends StatefulWidget {
class JeuDuTaquinState extends State<JeuDuTaquin> { class JeuDuTaquinState extends State<JeuDuTaquin> {
TextStyle textStyle = TextStyle(fontSize: 16, fontWeight: FontWeight.bold,); TextStyle textStyle = TextStyle(fontSize: 16, fontWeight: FontWeight.bold,);
TextStyle victoryStyle = TextStyle(fontSize: 26, fontWeight: FontWeight.bold,color: Colors.red);
var appBar= AppBar(title: Text('Jeu Du Taquin'),); var appBar= AppBar(title: Text('Jeu Du Taquin'),);
List<TileWidget> tiles=createTileWidgets(3, 4,createListTile(3),false,false); List<TileWidget> tiles=createTileWidgets(3, 4,createListTile(3),false,false);
double _currentSlideValue=3; double _currentSlideValue=3;
double _currentDifficultyValue=100; double _currentDifficultyValue=100;
bool isPlaying=false; bool isPlaying=false;
bool _numbers=false; bool _numbers=false;
bool _isWon=false;
int emptyIndex=4; int emptyIndex=4;
int previousEmptyIndex=-1; int previousEmptyIndex=-1;
int count=0; int count=0;
...@@ -104,7 +107,7 @@ class JeuDuTaquinState extends State<JeuDuTaquin> { ...@@ -104,7 +107,7 @@ class JeuDuTaquinState extends State<JeuDuTaquin> {
body: ListView(children: [ body: ListView(children: [
SizedBox( SizedBox(
width: MediaQuery.of(context).size.width, width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height*0.6, height: MediaQuery.of(context).size.height*0.53,
child: Container( child: Container(
child:GridView.builder( child:GridView.builder(
padding: const EdgeInsets.all(10), padding: const EdgeInsets.all(10),
...@@ -123,6 +126,10 @@ class JeuDuTaquinState extends State<JeuDuTaquin> { ...@@ -123,6 +126,10 @@ class JeuDuTaquinState extends State<JeuDuTaquin> {
previousEmptyIndex=emptyIndex; //on garde le coup précédent previousEmptyIndex=emptyIndex; //on garde le coup précédent
count++; //on incremente le compteur de coup count++; //on incremente le compteur de coup
swapTiles(index,emptyIndex); swapTiles(index,emptyIndex);
if(isWon()){
reset();
_isWon=true;
}
}, },
), ),
); );
...@@ -130,6 +137,14 @@ class JeuDuTaquinState extends State<JeuDuTaquin> { ...@@ -130,6 +137,14 @@ class JeuDuTaquinState extends State<JeuDuTaquin> {
), ),
), ),
), ),
AnimatedOpacity(
opacity: _isWon ? 1.0 : 0.0,
duration: Duration(milliseconds: 2000),
child: Container(height: 40,child:Align(
child:Text("VICTOIRE !",style: victoryStyle,),
alignment: Alignment.center,
))
),
isPlaying? Container(height: 40,child:Align( isPlaying? Container(height: 40,child:Align(
child:Text("Compteur de coups: "+count.toString(),style: textStyle,), child:Text("Compteur de coups: "+count.toString(),style: textStyle,),
alignment: Alignment.center, alignment: Alignment.center,
...@@ -151,8 +166,6 @@ class JeuDuTaquinState extends State<JeuDuTaquin> { ...@@ -151,8 +166,6 @@ class JeuDuTaquinState extends State<JeuDuTaquin> {
Container(height:((MediaQuery.of(context).size.height)/7)-appBar.preferredSize.height,width: MediaQuery.of(context).size.width-90,child: Slider( Container(height:((MediaQuery.of(context).size.height)/7)-appBar.preferredSize.height,width: MediaQuery.of(context).size.width-90,child: Slider(
value: _currentDifficultyValue, value: _currentDifficultyValue,
min: 20, max: 220, divisions: 10, min: 20, max: 220, divisions: 10,
activeColor: Colors.blue,
inactiveColor: Colors.grey,
label: _currentDifficultyValue.round().toString(), label: _currentDifficultyValue.round().toString(),
onChanged: (double value) { onChanged: (double value) {
setState(() { setState(() {
...@@ -188,10 +201,14 @@ class JeuDuTaquinState extends State<JeuDuTaquin> { ...@@ -188,10 +201,14 @@ class JeuDuTaquinState extends State<JeuDuTaquin> {
value: _numbers, value: _numbers,
onChanged: (value) { onChanged: (value) {
setState(() { setState(() {
if(!isPlaying)_numbers = !_numbers; if(!isPlaying) {
}); _numbers = !_numbers;
tiles = createTileWidgets(_currentSlideValue, emptyIndex, createListTile(_currentSlideValue), isPlaying, _numbers);
}
}
);
} }
), ),
), ),
] ]
): new Container(), ): new Container(),
...@@ -202,11 +219,12 @@ class JeuDuTaquinState extends State<JeuDuTaquin> { ...@@ -202,11 +219,12 @@ class JeuDuTaquinState extends State<JeuDuTaquin> {
setState(() { setState(() {
if(!isPlaying){ //lancement du jeu if(!isPlaying){ //lancement du jeu
isPlaying=true; isPlaying=true;
_isWon=false;
tiles=createTileWidgets(_currentSlideValue,emptyIndex,createListTile(_currentSlideValue),isPlaying,_numbers); tiles=createTileWidgets(_currentSlideValue,emptyIndex,createListTile(_currentSlideValue),isPlaying,_numbers);
shuffle(_currentDifficultyValue.toInt()); shuffle(_currentDifficultyValue.toInt());
}else reset(); }else reset();
}
}); );
}, },
tooltip: 'Play', tooltip: 'Play',
child: new Icon(isPlaying? Icons.cancel:Icons.play_arrow), child: new Icon(isPlaying? Icons.cancel:Icons.play_arrow),
...@@ -243,7 +261,7 @@ class JeuDuTaquinState extends State<JeuDuTaquin> { ...@@ -243,7 +261,7 @@ class JeuDuTaquinState extends State<JeuDuTaquin> {
if(emptyIndex>=_currentSlideValue) tiles[emptyIndex-_currentSlideValue.toInt()].isUsable=true; //haut if(emptyIndex>=_currentSlideValue) tiles[emptyIndex-_currentSlideValue.toInt()].isUsable=true; //haut
if(emptyIndex<(_currentSlideValue*(_currentSlideValue-1))) tiles[emptyIndex+_currentSlideValue.toInt()].isUsable=true; //bas 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+1)%_currentSlideValue!=0)tiles[emptyIndex+1].isUsable=true; //droite
if(emptyIndex%_currentSlideValue!=0)tiles[emptyIndex-1].isUsable=true; //gauche if(emptyIndex%_currentSlideValue!=0)tiles[emptyIndex-1].isUsable=true;//gauche
}); });
} }
...@@ -296,4 +314,13 @@ class JeuDuTaquinState extends State<JeuDuTaquin> { ...@@ -296,4 +314,13 @@ class JeuDuTaquinState extends State<JeuDuTaquin> {
tiles=createTileWidgets(_currentSlideValue,emptyIndex,createListTile(_currentSlideValue),isPlaying,_numbers); tiles=createTileWidgets(_currentSlideValue,emptyIndex,createListTile(_currentSlideValue),isPlaying,_numbers);
} }
bool isWon(){
bool res=true;
int max=_currentSlideValue.toInt()*_currentSlideValue.toInt();
for(int i=0;i<max;i++){
if(tiles[i].index!=i)res=false;
}
return res;
}
} }
\ 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