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

Side by Side Diff: samples/pop_pop_win/lib/src/game/field.dart

Issue 786443002: Delete pop-pop-win from the repo (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « samples/pop_pop_win/lib/src/game.dart ('k') | samples/pop_pop_win/lib/src/game/game.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4 part of pop_pop_win.game;
5
6 class Field extends Array2d<bool> {
7 final int bombCount;
8 final Array2d<int> _adjacents;
9
10 factory Field([int bombCount = 40, int cols = 16, int rows = 16,
11 int seed = null]) {
12 var squares = new List<bool>.filled(rows * cols, false);
13 assert(bombCount < squares.length);
14 assert(bombCount > 0);
15
16 var rnd = new Random(seed);
17
18 // This is the most simple code, but it'll get slow as
19 // bombCount approaches the square count.
20 // But more efficient if bombCount << square count
21 // which is expected.
22 for (int i = 0; i < bombCount; i++) {
23 int index;
24 do {
25 index = rnd.nextInt(squares.length);
26 } while (squares[index]);
27 squares[index] = true;
28 }
29
30 return new Field._internal(bombCount, cols,
31 new UnmodifiableListView<bool>(squares));
32 }
33
34 factory Field.fromSquares(int cols, int rows, List<bool> squares) {
35 assert(cols > 0);
36 assert(rows > 0);
37 assert(squares.length == cols * rows);
38
39 int count = 0;
40 for (final m in squares) {
41 if (m) {
42 count++;
43 }
44 }
45 assert(count > 0);
46 assert(count < squares.length);
47
48 return new Field._internal(count, cols,
49 new UnmodifiableListView<bool>(squares));
50 }
51
52 Field._internal(this.bombCount, int cols, UnmodifiableListView<bool> source)
53 : this._adjacents = new Array2d<int>(cols, source.length ~/ cols),
54 super.wrap(cols, source.toList()) {
55 assert(width > 0);
56 assert(height > 0);
57 assert(bombCount > 0);
58 assert(bombCount < length);
59
60 int count = 0;
61 for (var m in this) {
62 if (m) {
63 count++;
64 }
65 }
66 assert(count == bombCount);
67 }
68
69 int getAdjacentCount(int x, int y) {
70 if (get(x, y)) {
71 return null;
72 }
73
74 int val = _adjacents.get(x, y);
75
76 if (val == null) {
77 val = 0;
78 for (var i in getAdjacentIndices(x, y)) {
79 if (this[i]) {
80 val++;
81 }
82 }
83 _adjacents.set(x, y, val);
84 }
85 return val;
86 }
87
88 String toString() => 'w${width}h${height}m${bombCount}';
89 }
OLDNEW
« no previous file with comments | « samples/pop_pop_win/lib/src/game.dart ('k') | samples/pop_pop_win/lib/src/game/game.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698