Reject move commands in non-cardinal directions

This commit is contained in:
Marc Di Luzio 2020-07-19 11:26:08 +01:00
parent 0e731df1a3
commit c2e3c9f090
4 changed files with 23 additions and 9 deletions

View file

@ -426,8 +426,10 @@ func (w *World) Enqueue(rover string, commands ...Command) error {
for _, c := range commands {
switch c.Command {
case roveapi.CommandType_move:
if _, err := maths.FromString(c.Bearing); err != nil {
if b, err := maths.BearingFromString(c.Bearing); err != nil {
return fmt.Errorf("unknown bearing: %s", c.Bearing)
} else if !b.IsCardinal() {
return fmt.Errorf("bearing must be cardinal")
}
case roveapi.CommandType_broadcast:
if len(c.Message) > 3 {
@ -505,7 +507,7 @@ func (w *World) ExecuteCommand(c *Command, rover string) (err error) {
switch c.Command {
case roveapi.CommandType_move:
if dir, err := maths.FromString(c.Bearing); err != nil {
if dir, err := maths.BearingFromString(c.Bearing); err != nil {
return err
} else if _, err := w.MoveRover(rover, dir); err != nil {
return err