| 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 part of dartcombatlib; | 5 part of dartcombatlib; |
| 6 | 6 |
| 7 /** A boat in the grid. */ | 7 /** A boat in the grid. */ |
| 8 class Boat { | 8 class Boat { |
| 9 final int startX; | 9 final int startX; |
| 10 final int startY; | 10 final int startY; |
| 11 final bool horizontal; | 11 final bool horizontal; |
| 12 final int length; | 12 final int length; |
| 13 int hitCount = 0; | 13 int hitCount = 0; |
| 14 | 14 |
| 15 Boat(this.startX, this.startY, this.horizontal, this.length) {} | 15 Boat(this.startX, this.startY, this.horizontal, this.length) {} |
| 16 | 16 |
| 17 bool get sunk => length == hitCount; | 17 bool get sunk => length == hitCount; |
| 18 } | 18 } |
| 19 | 19 |
| 20 /** Represents a grid configuration. */ | 20 /** Represents a grid configuration. */ |
| 21 class BoatGrid { | 21 class BoatGrid { |
| 22 List<List<Boat>> boatMap; | 22 List<List<Boat>> boatMap; |
| 23 | 23 |
| 24 BoatGrid() : boatMap = new List.fixedLength(Constants.SIZE) { | 24 BoatGrid() : boatMap = new List(Constants.SIZE) { |
| 25 for (int i = 0; i < Constants.SIZE; i++) { | 25 for (int i = 0; i < Constants.SIZE; i++) { |
| 26 boatMap[i] = new List.fixedLength(10); | 26 boatMap[i] = new List(10); |
| 27 } | 27 } |
| 28 } | 28 } |
| 29 | 29 |
| 30 void placeBoats(List<Boat> boats) { | 30 void placeBoats(List<Boat> boats) { |
| 31 for (int b = 0; b < boats.length; b++) { | 31 for (int b = 0; b < boats.length; b++) { |
| 32 Boat boat = boats[b]; | 32 Boat boat = boats[b]; |
| 33 for (int i = 0; i < boat.length; i++) { | 33 for (int i = 0; i < boat.length; i++) { |
| 34 int x = boat.startX + (boat.horizontal ? i : 0); | 34 int x = boat.startX + (boat.horizontal ? i : 0); |
| 35 int y = boat.startY + (boat.horizontal ? 0 : i); | 35 int y = boat.startY + (boat.horizontal ? 0 : i); |
| 36 boatMap[x][y] = boat; | 36 boatMap[x][y] = boat; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 54 return b.sunk ? [Constants.SUNK, b.length] : const [Constants.HIT]; | 54 return b.sunk ? [Constants.SUNK, b.length] : const [Constants.HIT]; |
| 55 } | 55 } |
| 56 } | 56 } |
| 57 } | 57 } |
| 58 | 58 |
| 59 /** Represents the current state of a boat grid. */ | 59 /** Represents the current state of a boat grid. */ |
| 60 class GridState { | 60 class GridState { |
| 61 List<List<int>> cells; | 61 List<List<int>> cells; |
| 62 | 62 |
| 63 GridState() | 63 GridState() |
| 64 : cells = new List.fixedLength(Constants.SIZE) { | 64 : cells = new List(Constants.SIZE) { |
| 65 for (int i = 0; i < Constants.SIZE; i++) { | 65 for (int i = 0; i < Constants.SIZE; i++) { |
| 66 cells[i] = new List.fixedLength(10); | 66 cells[i] = new List(10); |
| 67 } | 67 } |
| 68 } | 68 } |
| 69 | 69 |
| 70 int valueAt(int x, int y) => cells[x][y]; | 70 int valueAt(int x, int y) => cells[x][y]; |
| 71 | 71 |
| 72 void miss(int x, int y) { | 72 void miss(int x, int y) { |
| 73 cells[x][y] = Constants.MISS; | 73 cells[x][y] = Constants.MISS; |
| 74 } | 74 } |
| 75 | 75 |
| 76 void hit(int x, int y) { | 76 void hit(int x, int y) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 90 /** Static constants used by the game. */ | 90 /** Static constants used by the game. */ |
| 91 class Constants { | 91 class Constants { |
| 92 static const SIZE = 10; | 92 static const SIZE = 10; |
| 93 static const MISS = 1; | 93 static const MISS = 1; |
| 94 static const HIT = 2; | 94 static const HIT = 2; |
| 95 static const SUNK = 3; | 95 static const SUNK = 3; |
| 96 static const PENDING = 4; | 96 static const PENDING = 4; |
| 97 | 97 |
| 98 Constants() {} | 98 Constants() {} |
| 99 } | 99 } |
| OLD | NEW |