Created
August 2, 2019 10:34
-
-
Save brosenan/dcaace111d497decf6303bde90f8b77c to your computer and use it in GitHub Desktop.
A "Game of Sorts"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'dart:math'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
backgroundColor: Colors.white, | |
colorScheme: ColorScheme.light(), | |
), | |
home: GameHome(), | |
); | |
} | |
} | |
class GameHome extends StatefulWidget { | |
GameHome({Key key}) : super(key: key); | |
@override | |
_GameHomeState createState() => _GameHomeState(); | |
} | |
class _GameHomeState extends State<GameHome> { | |
int size = 4; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text("Game of Sorts"),), | |
body: Row( | |
children: <Widget>[ | |
Text("Choose board size:"), | |
DropdownButton(items: [3, 4, 5].map((n) => | |
DropdownMenuItem(child: Text(n.toString() + "X" + n.toString()), | |
value: n,)).toList(), | |
onChanged: (v) { | |
setState(() { | |
size = v; | |
}); | |
}, | |
value: size, | |
), | |
], | |
), | |
floatingActionButton: FloatingActionButton( | |
child: IconButton(icon: Icon(Icons.play_arrow)), | |
onPressed: () { | |
Navigator.push(context, MaterialPageRoute( | |
builder: (context) => GamePage(size), | |
)); | |
}, | |
), | |
); | |
} | |
} | |
class GamePage extends StatefulWidget { | |
GamePage(int this.size, {Key key}) : super(key: key); | |
int size; | |
@override | |
_GamePageState createState() => _GamePageState(size, size); | |
} | |
class _GamePageState extends State<GamePage> { | |
_GamePageState(this.rows, this.columns) | |
: board = _initialBoard(rows, columns) { | |
_shuffle(); | |
} | |
final int rows; | |
final int columns; | |
final List<int> board; | |
int moves = 0; | |
static List<int> _initialBoard(int rows, int columns) { | |
List<int> board = List<int>(); | |
for (int i = 1; i < rows * columns; i++) { | |
board.add(i); | |
} | |
board.add(0); | |
return board; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text("Sort of Fun...")), | |
body: Center(child: _buildGame()), | |
floatingActionButton: FloatingActionButton( | |
child: IconButton(icon: Icon(Icons.shuffle)), | |
onPressed: () { | |
setState(() { | |
_shuffle(); | |
}); | |
}, | |
), | |
); | |
} | |
void _shuffle() { | |
Random rand = Random.secure(); | |
for (int i = 0; i < rows * rows * columns * columns * 4; i++) { | |
_trySwap(rand.nextInt(rows * columns)); | |
} | |
moves = 0; | |
} | |
Widget _buildGame() { | |
return Column( | |
children: <Widget>[ | |
Text( | |
"Moves: " + moves.toString(), | |
style: TextStyle(fontSize: 20), | |
), | |
GridView.count( | |
shrinkWrap: true, | |
crossAxisCount: columns, | |
padding: EdgeInsets.all(10), | |
children: List<Widget>.generate( | |
rows * columns, | |
(n) => _buildCell(n), | |
), | |
), | |
], | |
); | |
} | |
Widget _buildCell(int n) { | |
int v = board.elementAt(n); | |
if (v > 0) { | |
return Container( | |
padding: EdgeInsets.all(5), | |
child: RaisedButton( | |
child: Text( | |
v.toString(), | |
style: TextStyle(fontSize: 20), | |
), | |
shape: | |
RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)), | |
color: (v == n+1) ? Colors.blue : Colors.grey, | |
onPressed: () { | |
this.setState(() { | |
_trySwap(n); | |
}); | |
}, | |
), | |
); | |
} else { | |
return Container(); | |
} | |
} | |
void _trySwap(int n) { | |
int column = n % columns; | |
if (column > 0) { | |
_swapZero(n, n - 1); | |
} | |
if (column < columns - 1) { | |
_swapZero(n, n + 1); | |
} | |
_swapZero(n, n - columns); | |
_swapZero(n, n + columns); | |
} | |
void _swapZero(int src, int dest) { | |
if (dest < 0 || dest >= rows * columns) { | |
return; | |
} | |
if (board[dest] == 0) { | |
board[dest] = board[src]; | |
board[src] = 0; | |
moves++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment