47 lines
975 B
Dart
47 lines
975 B
Dart
import 'package:flutter/material.dart';
|
|
import 'tile.dart';
|
|
|
|
class SwapNotifier extends ChangeNotifier {
|
|
Tile? selectedTile;
|
|
int _score = 0;
|
|
|
|
int get score => _score;
|
|
|
|
void resetScore() {
|
|
_score = 0;
|
|
selectedTile = null;
|
|
notifyListeners();
|
|
}
|
|
|
|
void incrementScore(int value) {
|
|
_score += value;
|
|
notifyListeners();
|
|
}
|
|
|
|
void selectTile(Tile tile) {
|
|
if (selectedTile == null) {
|
|
selectedTile = tile;
|
|
tile.select();
|
|
} else {
|
|
if (_isNeighbor(selectedTile!, tile)) {
|
|
notifyListeners();
|
|
} else {
|
|
selectedTile?.deselect();
|
|
selectedTile = tile;
|
|
tile.select();
|
|
notifyListeners();
|
|
}
|
|
}
|
|
}
|
|
|
|
bool _isNeighbor(Tile tile1, Tile tile2) {
|
|
return (tile1.row == tile2.row && (tile1.col - tile2.col).abs() == 1) ||
|
|
(tile1.col == tile2.col && (tile1.row - tile2.row).abs() == 1);
|
|
}
|
|
|
|
void clearSelectedTile() {
|
|
selectedTile = null;
|
|
notifyListeners();
|
|
}
|
|
}
|