File tree Expand file tree Collapse file tree 5 files changed +38
-27
lines changed
starter_kits/Swift/Halite-III/Halite-III/hlt Expand file tree Collapse file tree 5 files changed +38
-27
lines changed Original file line number Diff line number Diff line change 88
99import Foundation
1010
11- enum Command : String {
12- case north = " n "
13- case south = " s "
14- case east = " e "
15- case west = " w "
16- case stayStill = " o "
17- case generate = " g "
18- case construct = " c "
19- case move = " m "
11+ enum Command {
12+ case move( shipId: Ship . ID , direction: Direction )
13+ case construct( fromShipId: Ship . ID )
14+ case generate
2015}
Original file line number Diff line number Diff line change @@ -23,19 +23,8 @@ enum Direction: CaseIterable {
2323 ///
2424 /// - Parameter direction: The direction to convert to a
2525 /// - Returns: A command equivalent to the given direction
26- static func convert( _ direction: Direction ) -> Command {
27- switch direction {
28- case . north:
29- return . north
30- case . south:
31- return . south
32- case . east:
33- return . east
34- case . west:
35- return . west
36- case . still:
37- return . stayStill
38- }
26+ static func convert( _ direction: Direction , forShipId shipId: Ship . ID ) -> Command {
27+ return . move( shipId: shipId, direction: direction)
3928 }
4029
4130 /// Returns the opposite cardinal direction given a direction
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ struct Game {
1414 var turnNumber = 0
1515
1616 var me : Player {
17- return players [ myId]
17+ return players. first ( where : { $0 . id == myId } ) !
1818 }
1919 var gameMap : Map
2020
@@ -103,6 +103,7 @@ struct Game {
103103 }
104104 let updatedPlayer = Player ( id: update. playerID,
105105 shipyard: playerToUpdate. shipyard,
106+ haliteAmount: update. halite,
106107 ships: update. ships,
107108 dropoffs: update. dropoffs)
108109 updatedPlayers. append ( updatedPlayer)
Original file line number Diff line number Diff line change @@ -137,7 +137,7 @@ class Networking {
137137 }
138138
139139 func write( commands: [ Command ] ) {
140- let output = commands. map { $0 . rawValue } . joined ( separator: " " )
140+ let output = commands. map { string ( from : $0 ) } . joined ( separator: " " )
141141 write ( string: output)
142142 }
143143
@@ -157,4 +157,30 @@ class Networking {
157157 }
158158 return input
159159 }
160+
161+ private func string( from command: Command ) -> String {
162+ switch command {
163+ case . move( let shipId, let direction) :
164+ return " m \( shipId) \( string ( from: direction) ) "
165+ case . generate:
166+ return " g "
167+ case . construct( let shipId) :
168+ return " c \( shipId) "
169+ }
170+ }
171+
172+ private func string( from direction: Direction ) -> String {
173+ switch direction {
174+ case . north:
175+ return " n "
176+ case . south:
177+ return " s "
178+ case . east:
179+ return " e "
180+ case . west:
181+ return " w "
182+ case . still:
183+ return " s "
184+ }
185+ }
160186}
Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ struct Ship: Placeable {
3232 ///
3333 /// - Returns: An engine command to convert this ship into a dropoff.
3434 func makeDropoff( ) -> Command {
35- return . construct
35+ return . construct( fromShipId : id )
3636 }
3737
3838 /// Returns an engine command to keep this ship where it is and collect halite.
@@ -41,7 +41,7 @@ struct Ship: Placeable {
4141 ///
4242 /// - Returns: An engine command to keep this ship where it is and collect halite.
4343 func stayStill( ) -> Command {
44- return . stayStill
44+ return . move ( shipId : id , direction : . still )
4545 }
4646
4747 /// Returns an engine command to move this ship in a direction without checking for collisions.
@@ -53,6 +53,6 @@ struct Ship: Placeable {
5353 /// - Parameter direction: The direction to move
5454 /// - Returns: An engine command to move this ship in a direction without checking for collisions.
5555 func move( direction: Direction ) -> Command {
56- return Direction . convert ( direction)
56+ return . move ( shipId : id , direction : direction)
5757 }
5858}
You can’t perform that action at this time.
0 commit comments