Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1266)

Unified Diff: samples/third_party/pop-pop-win/lib/src/poppopwin/field.dart

Issue 200723008: samples/poppopwin: upgrade to first-class sample (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fixed pkgbuild.status for rename Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: samples/third_party/pop-pop-win/lib/src/poppopwin/field.dart
diff --git a/samples/third_party/pop-pop-win/lib/src/poppopwin/field.dart b/samples/third_party/pop-pop-win/lib/src/poppopwin/field.dart
deleted file mode 100644
index 3d492e65d8089a10064145c99a6b77851281aa87..0000000000000000000000000000000000000000
--- a/samples/third_party/pop-pop-win/lib/src/poppopwin/field.dart
+++ /dev/null
@@ -1,85 +0,0 @@
-part of ppw;
-
-class Field extends Array2d<bool> {
- final int bombCount;
- final Array2d<int> _adjacents;
-
- factory Field([bombCount = 40, cols = 16, rows = 16, int seed = null]) {
- final squares = new List<bool>.filled(rows * cols, false);
- assert(bombCount < squares.length);
- assert(bombCount > 0);
-
- final rnd = new math.Random(seed);
-
- // This is the most simple code, but it'll get slow as
- // bombCount approaches the square count.
- // But more efficient if bombCount << square count
- // which is expected.
- for(int i = 0; i < bombCount; i++) {
- int index;
- do {
- index = rnd.nextInt(squares.length);
- } while(squares[index]);
- squares[index] = true;
- }
-
- return new Field._internal(bombCount, cols,
- new ReadOnlyCollection<bool>(squares));
- }
-
- factory Field.fromSquares(int cols, int rows, List<bool> squares) {
- assert(cols > 0);
- assert(rows > 0);
- assert(squares.length == cols * rows);
-
- int count = 0;
- for(final m in squares) {
- if(m) {
- count++;
- }
- }
- assert(count > 0);
- assert(count < squares.length);
-
- return new Field._internal(count, cols,
- new ReadOnlyCollection<bool>(squares));
- }
-
- Field._internal(this.bombCount, int cols, ReadOnlyCollection<bool> source) :
- this._adjacents = new Array2d<int>(cols, source.length ~/ cols),
- super.wrap(cols, source.toList()) {
- assert(width > 0);
- assert(height > 0);
- assert(bombCount > 0);
- assert(bombCount < length);
-
- int count = 0;
- for(final m in this) {
- if(m) {
- count++;
- }
- }
- assert(count == bombCount);
- }
-
- int getAdjacentCount(int x, int y) {
- if(get(x,y)) {
- return null;
- }
-
- int val = _adjacents.get(x, y);
-
- if(val == null) {
- val = 0;
- for(final i in getAdjacentIndices(x,y)) {
- if(this[i]) {
- val++;
- }
- }
- _adjacents.set(x, y, val);
- }
- return val;
- }
-
- String toString() => 'w${width}h${height}m${bombCount}';
-}

Powered by Google App Engine
This is Rietveld 408576698