diff --git a/lib/main.dart b/lib/main.dart index b0319bf..73c4823 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -59,6 +59,8 @@ class MyHomePage extends StatefulWidget { class _MyHomePageState extends State { late List> _grid; + int _selectedRow = -1; + int _selectedCol = -1; @override void initState() { @@ -122,8 +124,20 @@ class _MyHomePageState extends State { } void _onTileTap(int row, int col) { - setState(() { - _grid[row][col] = Random().nextInt(5); - }); + setState(() { + if (_selectedRow == -1 && _selectedCol == -1) { + _selectedRow = row; + _selectedCol = col; + } else { + _swapTiles(_selectedRow, _selectedCol, row, col); + _selectedRow = -1; + _selectedCol = -1; + } + }); + } + void _swapTiles(int row1, int col1, int row2, int col2) { + int temp = _grid[row1][col1]; + _grid[row1][col1] = _grid[row2][col2]; + _grid[row2][col2] = temp; } }