Skip to content
Snippets Groups Projects
data.dart 1.96 KiB
import 'package:flutter/foundation.dart';
import 'package:puzzlegame/entities/moving_tile.dart';

class Data extends ChangeNotifier {
  // application configuration
  int _tilesCount = 4;

  // application data
  List _images = [];
  String _image = '';
  List<MovingTile> _tiles = [];

  // application state
  bool _isShufflingBoard = false;
  bool _displayTipImage = false;

  String get image => _image;

  set updateImage(String value) {
    _image = value;
    notifyListeners();
  }

  int get tilesCount => _tilesCount;

  set updateTilesCount(int value) {
    _tilesCount = value;
    notifyListeners();
  }

  List get images => _images;

  set updateImages(List value) {
    _images = value;
    notifyListeners();
  }

  List<MovingTile> get tiles => _tiles;

  set updateTiles(List<MovingTile> value) {
    _tiles = value;
    notifyListeners();
  }

  bool get isShufflingBoard => _isShufflingBoard;

  set updateIsShufflingBoard(bool value) {
    _isShufflingBoard = value;
    notifyListeners();
  }

  bool get displayTipImage => _displayTipImage;

  set updateDisplayTipImage(bool value) {
    _displayTipImage = value;
    notifyListeners();
  }

  void swapTiles(List<int> tile1, List<int> tile2) {
    int indexTile1 = _tiles.indexWhere(
        (tile) => ((tile.currentCol == tile1[0]) && (tile.currentRow == tile1[1])));
    int indexTile2 = _tiles.indexWhere(
        (tile) => ((tile.currentCol == tile2[0]) && (tile.currentRow == tile2[1])));

    MovingTile swap = _tiles[indexTile1];
    _tiles[indexTile1] = _tiles[indexTile2];
    _tiles[indexTile2] = swap;

    int swapCol = _tiles[indexTile1].currentCol;
    _tiles[indexTile1].currentCol = _tiles[indexTile2].currentCol;
    _tiles[indexTile2].currentCol = swapCol;

    int swapRow = _tiles[indexTile1].currentRow;
    _tiles[indexTile1].currentRow = _tiles[indexTile2].currentRow;
    _tiles[indexTile2].currentRow = swapRow;

    notifyListeners();
  }

  void resetGame() {
    _image = '';
    notifyListeners();
  }
}