| OLD | NEW |
| 1 part of pop_pop_win.game; | 1 part of pop_pop_win.game; |
| 2 | 2 |
| 3 class Field extends Array2d<bool> { | 3 class Field extends Array2d<bool> { |
| 4 final int bombCount; | 4 final int bombCount; |
| 5 final Array2d<int> _adjacents; | 5 final Array2d<int> _adjacents; |
| 6 | 6 |
| 7 factory Field([bombCount = 40, cols = 16, rows = 16, int seed = null]) { | 7 factory Field([int bombCount = 40, int cols = 16, int rows = 16, |
| 8 final squares = new List<bool>.filled(rows * cols, false); | 8 int seed = null]) { |
| 9 var squares = new List<bool>.filled(rows * cols, false); |
| 9 assert(bombCount < squares.length); | 10 assert(bombCount < squares.length); |
| 10 assert(bombCount > 0); | 11 assert(bombCount > 0); |
| 11 | 12 |
| 12 final rnd = new math.Random(seed); | 13 var rnd = new Random(seed); |
| 13 | 14 |
| 14 // This is the most simple code, but it'll get slow as | 15 // This is the most simple code, but it'll get slow as |
| 15 // bombCount approaches the square count. | 16 // bombCount approaches the square count. |
| 16 // But more efficient if bombCount << square count | 17 // But more efficient if bombCount << square count |
| 17 // which is expected. | 18 // which is expected. |
| 18 for (int i = 0; i < bombCount; i++) { | 19 for (int i = 0; i < bombCount; i++) { |
| 19 int index; | 20 int index; |
| 20 do { | 21 do { |
| 21 index = rnd.nextInt(squares.length); | 22 index = rnd.nextInt(squares.length); |
| 22 } while (squares[index]); | 23 } while (squares[index]); |
| 23 squares[index] = true; | 24 squares[index] = true; |
| 24 } | 25 } |
| 25 | 26 |
| 26 return new Field._internal(bombCount, cols, | 27 return new Field._internal(bombCount, cols, |
| 27 new ReadOnlyCollection<bool>(squares)); | 28 new UnmodifiableListView<bool>(squares)); |
| 28 } | 29 } |
| 29 | 30 |
| 30 factory Field.fromSquares(int cols, int rows, List<bool> squares) { | 31 factory Field.fromSquares(int cols, int rows, List<bool> squares) { |
| 31 assert(cols > 0); | 32 assert(cols > 0); |
| 32 assert(rows > 0); | 33 assert(rows > 0); |
| 33 assert(squares.length == cols * rows); | 34 assert(squares.length == cols * rows); |
| 34 | 35 |
| 35 int count = 0; | 36 int count = 0; |
| 36 for (final m in squares) { | 37 for (final m in squares) { |
| 37 if (m) { | 38 if (m) { |
| 38 count++; | 39 count++; |
| 39 } | 40 } |
| 40 } | 41 } |
| 41 assert(count > 0); | 42 assert(count > 0); |
| 42 assert(count < squares.length); | 43 assert(count < squares.length); |
| 43 | 44 |
| 44 return new Field._internal(count, cols, | 45 return new Field._internal(count, cols, |
| 45 new ReadOnlyCollection<bool>(squares)); | 46 new UnmodifiableListView<bool>(squares)); |
| 46 } | 47 } |
| 47 | 48 |
| 48 Field._internal(this.bombCount, int cols, ReadOnlyCollection<bool> source) : | 49 Field._internal(this.bombCount, int cols, UnmodifiableListView<bool> source) |
| 49 this._adjacents = new Array2d<int>(cols, source.length ~/ cols), | 50 : this._adjacents = new Array2d<int>(cols, source.length ~/ cols), |
| 50 super.wrap(cols, source.toList()) { | 51 super.wrap(cols, source.toList()) { |
| 51 assert(width > 0); | 52 assert(width > 0); |
| 52 assert(height > 0); | 53 assert(height > 0); |
| 53 assert(bombCount > 0); | 54 assert(bombCount > 0); |
| 54 assert(bombCount < length); | 55 assert(bombCount < length); |
| 55 | 56 |
| 56 int count = 0; | 57 int count = 0; |
| 57 for (final m in this) { | 58 for (var m in this) { |
| 58 if (m) { | 59 if (m) { |
| 59 count++; | 60 count++; |
| 60 } | 61 } |
| 61 } | 62 } |
| 62 assert(count == bombCount); | 63 assert(count == bombCount); |
| 63 } | 64 } |
| 64 | 65 |
| 65 int getAdjacentCount(int x, int y) { | 66 int getAdjacentCount(int x, int y) { |
| 66 if (get(x, y)) { | 67 if (get(x, y)) { |
| 67 return null; | 68 return null; |
| 68 } | 69 } |
| 69 | 70 |
| 70 int val = _adjacents.get(x, y); | 71 int val = _adjacents.get(x, y); |
| 71 | 72 |
| 72 if (val == null) { | 73 if (val == null) { |
| 73 val = 0; | 74 val = 0; |
| 74 for (final i in getAdjacentIndices(x, y)) { | 75 for (var i in getAdjacentIndices(x, y)) { |
| 75 if (this[i]) { | 76 if (this[i]) { |
| 76 val++; | 77 val++; |
| 77 } | 78 } |
| 78 } | 79 } |
| 79 _adjacents.set(x, y, val); | 80 _adjacents.set(x, y, val); |
| 80 } | 81 } |
| 81 return val; | 82 return val; |
| 82 } | 83 } |
| 83 | 84 |
| 84 String toString() => 'w${width}h${height}m${bombCount}'; | 85 String toString() => 'w${width}h${height}m${bombCount}'; |
| 85 } | 86 } |
| OLD | NEW |