match_magic/lib/game/swap_notifier.dart
2024-08-05 11:04:54 +03:00

27 lines
672 B
Dart

import 'package:flutter/material.dart';
import 'board.dart';
import 'tile.dart';
class SwapNotifier extends ChangeNotifier {
Tile? selectedTile;
void selectTile(Tile tile, Board board) {
if (selectedTile == null) {
selectedTile = tile;
} else {
if (_isNeighbor(selectedTile!, tile)) {
board.swapTiles(selectedTile!, tile);
selectedTile = null;
} else {
selectedTile = tile;
}
}
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);
}
}