Commit db74f242 authored by Benjamin LEROUX's avatar Benjamin LEROUX

taquin debut

parent ac03a5a4
......@@ -8,7 +8,8 @@ class Tile {
Tile({this.imageURL, this.alignment,this.size});
Widget croppedImageTile() {
return FittedBox(
return SizedBox.expand(
child: FittedBox(
fit: BoxFit.fill,
child: ClipRect(
child: Container(
......@@ -20,12 +21,13 @@ class Tile {
),
),
),
),
);
}
}
Tile tile = new Tile(
imageURL: 'https://picsum.photos/512/1024', alignment: Alignment(0, 0),size: 3);
imageURL: 'https://picsum.photos/512/1024', alignment: Alignment(0, 0),size: 4);
class DisplayTileWidget extends StatelessWidget {
@override
......
import 'package:flutter/material.dart';
import 'Tile.dart';
class TileWidget extends StatelessWidget {
Tile tile;
bool isUsable=false;
TileWidget(this.tile,this.isUsable);
@override
Widget build(BuildContext context) {
return Container(
child: Align(
child: tile.croppedImageTile(),
alignment: Alignment.center,
)
);
}
}
BoxDecoration myBoxDecoration(bool isUsable) {
BoxDecoration res=BoxDecoration();
if(isUsable){
res=BoxDecoration(
border: Border.all(
color: Colors.red,
width: 5.0,
),
);
}
return res;
}
List<Tile> createListTile(double size){
List<Tile> res=<Tile>[];
for(double i=-1.0;i<=1.0;){
for(double j=-1.0;j<=1.0;){
res.add(new Tile(imageURL: 'https://picsum.photos/512/512', alignment: Alignment(j, i),size: size));
j+=(2/(size-1));
}
i+=(2/(size-1));
}
return res;
}
List<TileWidget>createTileWidgets(double slide, int emptyIndex, List<Tile> listTiles){
List<TileWidget> tiles = <TileWidget>[];
int max=slide.toInt()*slide.toInt();
for(int i=0;i<max;i++){
if(i==emptyIndex){
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));
}
else tiles.add(TileWidget(listTiles[i],false));
}
return tiles;
}
void main() => runApp(new MaterialApp(home: JeuDuTaquin()));
class JeuDuTaquin extends StatefulWidget {
@override
State<StatefulWidget> createState() => JeuDuTaquinState();
}
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;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: appBar,
body: ListView(children: [
SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height*0.7,
child: Container(
child:GridView.builder(
padding: const EdgeInsets.all(10),
itemCount: _currentSlideValue.toInt()*_currentSlideValue.toInt(),
gridDelegate:SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: _currentSlideValue.toInt(),
crossAxisSpacing: 5.0,
mainAxisSpacing: 5.0,
),
itemBuilder: (BuildContext context, int index) {
return Container(
decoration: myBoxDecoration(tiles[index].isUsable),
child:InkWell(
child: tiles[index],
onTap: (){
swapTiles(index,emptyIndex);
},
),
);
},
),
),
),
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,
min: 2, max: 7, divisions: 5,
label: _currentSlideValue.round().toString(),
onChanged: (double value) {
setState(() {
_currentSlideValue = value;
tiles=createTileWidgets(_currentSlideValue,emptyIndex,createListTile(_currentSlideValue));
});
}
),
),
]
),
],
)
);
}
swapTiles(int current, int empty) {
setState(() {
//on retire les bordures rouges apres un deplacement
for(int i=0;i<tiles.length;i++){
if(i!=empty ) tiles[i].isUsable=false;
}
//deplacement de la case
if(current==empty+1 && current%_currentSlideValue!=0){
emptyIndex=current;
tiles.insert(empty, tiles.removeAt(current));
}else if(current==empty-1 && empty%_currentSlideValue!=0){
emptyIndex=current;
tiles.insert(empty, tiles.removeAt(current));
}
else if(current==empty+_currentSlideValue){
emptyIndex=current;
tiles.insert(current, tiles.removeAt(empty));
tiles.insert(empty, tiles.removeAt(current-1));
}else if(current==empty-_currentSlideValue){
emptyIndex=current;
tiles.insert(current, tiles.removeAt(empty));
tiles.insert(empty, tiles.removeAt(current+1));
}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;
});
}
}
\ No newline at end of file
......@@ -5,6 +5,7 @@ import 'package:tp2_taquin/Tile.dart';
import 'exo1.dart';
import 'exo2.dart';
import 'exo6.dart';
import 'jeuDuTaquin.dart';
void main() {
runApp(MyApp());
......@@ -108,6 +109,19 @@ class _MyHomePageState extends State<MyHomePage> {
],
),
),
Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
trailing: Icon(Icons.play_arrow_rounded, color: Colors.red,),
title: Text('Jeu du Taquin'),
subtitle: Text("Version Finale"),
onTap:() {Navigator.push(context, MaterialPageRoute(builder: (context) =>JeuDuTaquin()));},
),
],
),
),
],
),
),
......
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