Commit 32558478 authored by mdakin's avatar mdakin

Fix alignment of mine counts, simplfy code a bit.

parent 1203b08b
...@@ -19,18 +19,21 @@ import 'package:sky/src/fn3.dart'; ...@@ -19,18 +19,21 @@ import 'package:sky/src/fn3.dart';
// CoveredMineNode and ExposedMineNode, none of them holding state. // CoveredMineNode and ExposedMineNode, none of them holding state.
// Colors for each mine count (0-8): // Colors for each mine count (0-8):
const List<TextStyle> textStyles = const <TextStyle>[ const List<Color> textColors = const <Color>[
const TextStyle(color: const Color(0xFF555555), fontWeight: bold), const Color(0xFF555555),
const TextStyle(color: const Color(0xFF0094FF), fontWeight: bold), // blue const Color(0xFF0094FF), // blue
const TextStyle(color: const Color(0xFF13A023), fontWeight: bold), // green const Color(0xFF13A023), // green
const TextStyle(color: const Color(0xFFDA1414), fontWeight: bold), // red const Color(0xFFDA1414), // red
const TextStyle(color: const Color(0xFF1E2347), fontWeight: bold), // black const Color(0xFF1E2347), // black
const TextStyle(color: const Color(0xFF7F0037), fontWeight: bold), // dark red const Color(0xFF7F0037), // dark red
const TextStyle(color: const Color(0xFF000000), fontWeight: bold), const Color(0xFF000000),
const TextStyle(color: const Color(0xFF000000), fontWeight: bold), const Color(0xFF000000),
const TextStyle(color: const Color(0xFF000000), fontWeight: bold), const Color(0xFF000000),
]; ];
final List<TextStyle> textStyles = textColors.map((c) =>
new TextStyle(color: c, fontWeight: bold, textAlign: TextAlign.center)).toList();
enum CellState { covered, exploded, cleared, flagged, shown } enum CellState { covered, exploded, cleared, flagged, shown }
class MineDigger extends StatefulComponent { class MineDigger extends StatefulComponent {
...@@ -235,11 +238,8 @@ class MineDiggerState extends State<MineDigger> { ...@@ -235,11 +238,8 @@ class MineDiggerState extends State<MineDigger> {
// Recursively uncovers cells whose totalMineCount is zero. // Recursively uncovers cells whose totalMineCount is zero.
void cull(int x, int y) { void cull(int x, int y) {
if ((x < 0) || (x > rows - 1)) if (!inBoard(x, y))
return;
if ((y < 0) || (y > cols - 1))
return; return;
if (uiState[y][x] == CellState.cleared) if (uiState[y][x] == CellState.cleared)
return; return;
uiState[y][x] = CellState.cleared; uiState[y][x] = CellState.cleared;
...@@ -259,25 +259,21 @@ class MineDiggerState extends State<MineDigger> { ...@@ -259,25 +259,21 @@ class MineDiggerState extends State<MineDigger> {
int mineCount(int x, int y) { int mineCount(int x, int y) {
int count = 0; int count = 0;
int my = cols - 1; count += bombs(x - 1, y);
int mx = rows - 1; count += bombs(x + 1, y);
count += bombs(x, y - 1);
count += x > 0 ? bombs(x - 1, y) : 0; count += bombs(x, y + 1 );
count += x < mx ? bombs(x + 1, y) : 0; count += bombs(x - 1, y - 1);
count += y > 0 ? bombs(x, y - 1) : 0; count += bombs(x + 1, y + 1);
count += y < my ? bombs(x, y + 1 ) : 0; count += bombs(x + 1, y - 1);
count += bombs(x - 1, y + 1);
count += (x > 0) && (y > 0) ? bombs(x - 1, y - 1) : 0;
count += (x < mx) && (y < my) ? bombs(x + 1, y + 1) : 0;
count += (x < mx) && (y > 0) ? bombs(x + 1, y - 1) : 0;
count += (x > 0) && (y < my) ? bombs(x - 1, y + 1) : 0;
return count; return count;
} }
int bombs(int x, int y) { int bombs(int x, int y) => inBoard(x, y) && cells[y][x] ? 1 : 0;
return cells[y][x] ? 1 : 0;
} bool inBoard(int x, int y) =>
(x > 0 && x < (cols - 1) && y > 0 && y < (rows -1));
} }
Widget buildCell(Widget child) { Widget buildCell(Widget child) {
...@@ -343,7 +339,6 @@ class ExposedMineNode extends StatelessComponent { ...@@ -343,7 +339,6 @@ class ExposedMineNode extends StatelessComponent {
} }
return buildCell(buildInnerCell(text)); return buildCell(buildInnerCell(text));
} }
} }
void main() { void main() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment