Commit 6d65bfc9 authored by Adam Barth's avatar Adam Barth

Merge pull request #1479 from mdakin/simplfy_digger

Simplfy resetting board and mine generation.
parents 49aba0cc 8fdd8cb5
...@@ -64,33 +64,25 @@ class MineDiggerState extends State<MineDigger> { ...@@ -64,33 +64,25 @@ class MineDiggerState extends State<MineDigger> {
alive = true; alive = true;
hasWon = false; hasWon = false;
detectedCount = 0; detectedCount = 0;
// Build the arrays. // Initialize matrices.
cells = new List<List<bool>>(); cells = new List<List>.generate(rows, (int row) {
uiState = new List<List<CellState>>(); return new List<bool>.filled(cols, false);
for (int iy = 0; iy != rows; iy++) { });
cells.add(new List<bool>()); uiState = new List<List>.generate(rows, (int row) {
uiState.add(new List<CellState>()); return new List<CellState>.filled(cols, CellState.covered);
for (int ix = 0; ix != cols; ix++) { });
cells[iy].add(false);
uiState[iy].add(CellState.covered);
}
}
// Place the mines. // Place the mines.
Random random = new Random(); Random random = new Random();
int cellsRemaining = rows * cols;
int minesRemaining = totalMineCount; int minesRemaining = totalMineCount;
for (int x = 0; x < cols; x += 1) { while (minesRemaining > 0) {
for (int y = 0; y < rows; y += 1) { int pos = random.nextInt(rows * cols);
if (random.nextInt(cellsRemaining) < minesRemaining) { int row = pos ~/ rows;
cells[y][x] = true; int col = pos % cols;
minesRemaining -= 1; if (!cells[row][col]) {
if (minesRemaining <= 0) cells[row][col] = true;
return; minesRemaining--;
}
cellsRemaining -= 1;
} }
} }
assert(false);
} }
PointerEventListener _pointerDownHandlerFor(int posX, int posY) { PointerEventListener _pointerDownHandlerFor(int posX, int posY) {
...@@ -106,9 +98,9 @@ class MineDiggerState extends State<MineDigger> { ...@@ -106,9 +98,9 @@ class MineDiggerState extends State<MineDigger> {
Widget buildBoard() { Widget buildBoard() {
bool hasCoveredCell = false; bool hasCoveredCell = false;
List<Row> flexRows = <Row>[]; List<Row> flexRows = <Row>[];
for (int iy = 0; iy != 9; iy++) { for (int iy = 0; iy < rows; iy++) {
List<Widget> row = <Widget>[]; List<Widget> row = <Widget>[];
for (int ix = 0; ix != 9; ix++) { for (int ix = 0; ix < cols; ix++) {
CellState state = uiState[iy][ix]; CellState state = uiState[iy][ix];
int count = mineCount(ix, iy); int count = mineCount(ix, iy);
if (!alive) { if (!alive) {
......
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