| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** A boat in the grid. */ | 5 /** A boat in the grid. */ |
| 6 class Boat { | 6 class Boat { |
| 7 final int startX; | 7 final int startX; |
| 8 final int startY; | 8 final int startY; |
| 9 final bool horizontal; | 9 final bool horizontal; |
| 10 final int length; | 10 final int length; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 assert(x < Constants.SIZE); | 42 assert(x < Constants.SIZE); |
| 43 assert(y < Constants.SIZE); | 43 assert(y < Constants.SIZE); |
| 44 assert(state.valueAt(x, y) == null); // repeated shot | 44 assert(state.valueAt(x, y) == null); // repeated shot |
| 45 Boat b = boatMap[x][y]; | 45 Boat b = boatMap[x][y]; |
| 46 if (b == null) { | 46 if (b == null) { |
| 47 state.miss(x, y); | 47 state.miss(x, y); |
| 48 return const [Constants.MISS]; | 48 return const [Constants.MISS]; |
| 49 } else { | 49 } else { |
| 50 state.hit(x, y); | 50 state.hit(x, y); |
| 51 b.hitCount++; | 51 b.hitCount++; |
| 52 return b.sunk ? const [Constants.SUNK, b.length] : const [Constants.HIT]; | 52 return b.sunk ? [Constants.SUNK, b.length] : const [Constants.HIT]; |
| 53 } | 53 } |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 | 56 |
| 57 /** Represents the current state of a boat grid. */ | 57 /** Represents the current state of a boat grid. */ |
| 58 class GridState { | 58 class GridState { |
| 59 List<List<int>> cells; | 59 List<List<int>> cells; |
| 60 | 60 |
| 61 GridState() | 61 GridState() |
| 62 : cells = new List(Constants.SIZE) { | 62 : cells = new List(Constants.SIZE) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 88 /** Static constants used by the game. */ | 88 /** Static constants used by the game. */ |
| 89 class Constants { | 89 class Constants { |
| 90 static final SIZE = 10; | 90 static final SIZE = 10; |
| 91 static final MISS = 1; | 91 static final MISS = 1; |
| 92 static final HIT = 2; | 92 static final HIT = 2; |
| 93 static final SUNK = 3; | 93 static final SUNK = 3; |
| 94 static final PENDING = 4; | 94 static final PENDING = 4; |
| 95 | 95 |
| 96 Constants() {} | 96 Constants() {} |
| 97 } | 97 } |
| OLD | NEW |