Skip to content

Commit 6eb5791

Browse files
author
Chris Downie
committed
Fix issue where the Swift bot sent commands in the wrong format.
1 parent 9a54e45 commit 6eb5791

File tree

4 files changed

+36
-26
lines changed

4 files changed

+36
-26
lines changed

starter_kits/Swift/Halite-III/Halite-III/hlt/Command.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,8 @@
88

99
import 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
}

starter_kits/Swift/Halite-III/Halite-III/hlt/Direction.swift

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff 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

starter_kits/Swift/Halite-III/Halite-III/hlt/Networking.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff 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
}

starter_kits/Swift/Halite-III/Halite-III/hlt/Ship.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)