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