Commit ac03a5a4 authored by Benjamin LEROUX's avatar Benjamin LEROUX

fin exo6

parent 7c849a6c
......@@ -3,19 +3,6 @@ import 'package:flutter/material.dart';
import 'Tile.dart';
class Plateau{
List<Tile> listTile;
double size;
Plateau({this.listTile,this.size});
List<Tile> getListTile(){
return listTile;
}
}
int size=5;
class DisplayPlateauWidget extends StatefulWidget {
@override
_DisplayPlateauWidgetState createState() => _DisplayPlateauWidgetState();
......@@ -82,9 +69,9 @@ class _DisplayPlateauWidgetState extends State<DisplayPlateauWidget> {
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/1024', alignment: Alignment(j, i),size: size));
j+=(1/((size-1)/2));
j+=(2/(size-1));
}
i+=(1/((size-1)/2));
i+=(2/(size-1));
}
return res;
}
......
import 'package:flutter/material.dart';
import 'dart:math' as math;
// ==============
// Models
// ==============
math.Random random = new math.Random();
class Tile {
Color color;
......@@ -14,14 +7,11 @@ class Tile {
Tile(this.index,this.color);
}
// ==============
// Widgets
// ==============
class TileWidget extends StatelessWidget {
final Tile tile;
Tile tile;
bool isUsable=false;
TileWidget(this.tile);
TileWidget(this.tile,this.isUsable);
@override
Widget build(BuildContext context) {
......@@ -39,19 +29,33 @@ class TileWidget extends StatelessWidget {
}
}
int emptyIndex=0;
BoxDecoration myBoxDecoration(bool isUsable) {
BoxDecoration res=BoxDecoration();
if(isUsable){
res= BoxDecoration(
border: Border.all(
color: Colors.red,
width: 5.0,
),
);
}
return res;
}
List<Widget>createTileWidgets(double slide){
List<Widget> tiles = <Widget>[];
List<TileWidget>createTileWidgets(double slide, int emptyIndex){
List<TileWidget> tiles = <TileWidget>[];
int max=slide.toInt()*slide.toInt();
tiles.add(TileWidget(Tile("Vide",Colors.white)));
for(int i=0;i<max-1;i++){
tiles.add(TileWidget(Tile((i+1).toString(), Colors.grey)));
for(int i=0;i<max;i++){
if(i==emptyIndex){
tiles.add(TileWidget(Tile("Vide",Colors.white),false));
}else if(i==emptyIndex+1 || i==emptyIndex+slide.toInt()){
tiles.add(TileWidget(Tile((i).toString(), Colors.grey),true));
}
else tiles.add(TileWidget(Tile((i).toString(), Colors.grey),false));
}
return tiles;
}
void main() => runApp(new MaterialApp(home: PositionedTiles()));
class PositionedTiles extends StatefulWidget {
......@@ -63,7 +67,8 @@ class PositionedTilesState extends State<PositionedTiles> {
TextStyle textStyle = TextStyle(fontSize: 16, fontWeight: FontWeight.bold,);
var appBar= AppBar(title: Text('Exo 6'),);
double _currentSlideValue=4;
List<Widget> tiles=createTileWidgets(4);
List<TileWidget> tiles=createTileWidgets(4,0);
int emptyIndex=0;
@override
Widget build(BuildContext context) {
......@@ -81,11 +86,14 @@ class PositionedTilesState extends State<PositionedTiles> {
mainAxisSpacing: 5.0,
),
itemBuilder: (BuildContext context, int index) {
return InkWell(
return Container(
decoration: myBoxDecoration(tiles[index].isUsable),
child:InkWell(
child: tiles[index],
onTap: (){
swapTiles(index,emptyIndex);
},
),
);
},
),
......@@ -99,7 +107,7 @@ class PositionedTilesState extends State<PositionedTiles> {
onChanged: (double value) {
setState(() {
_currentSlideValue = value;
tiles=createTileWidgets(_currentSlideValue);
tiles=createTileWidgets(_currentSlideValue,emptyIndex);
//initialSize=_currentSlideValue;
});
}
......@@ -114,10 +122,18 @@ class PositionedTilesState extends State<PositionedTiles> {
swapTiles(int current, int empty) {
setState(() {
//print(current);
//RESET bordures des cases apres deplacements
for(int i=0;i<tiles.length;i++){
if(i!=empty ) tiles[i].isUsable=false;
}
if(current==empty+1 && current%_currentSlideValue!=0){
emptyIndex=current;
tiles.insert(empty, tiles.removeAt(current));
// tiles[empty].isUsable=true;
// tiles[empty+2].isUsable=true;
// tiles[empty+_currentSlideValue.toInt()+1].isUsable=true;
}else if(current==empty-1 && empty%_currentSlideValue!=0){
emptyIndex=current;
tiles.insert(empty, tiles.removeAt(current));
......@@ -131,6 +147,11 @@ class PositionedTilesState extends State<PositionedTiles> {
tiles.insert(current, tiles.removeAt(empty));
tiles.insert(empty, tiles.removeAt(current+1));
}else print("Can't move this");
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;
});
}
......
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