exercism/go/queen-attack/queen_attack.go

30 lines
728 B
Go

package queenattack
import (
"errors"
"math"
)
// CanQueenAttack takes two positions one for a
// white queen (wq) and one for a black queen (bq)
// it returns a boolean value: if an attack can
// be made, and an error
func CanQueenAttack(wq, bq string) (bool, error) {
if len(wq) != 2 || len(bq) != 2 ||
wq[0] < 'a' || wq[0] > 'z' ||
wq[1] < '0' || wq[1] > '8' ||
bq[0] < 'a' || bq[0] > 'z' ||
bq[1] < '0' || bq[1] > '8' ||
wq == bq {
return false, errors.New("Invalid pieces/placement")
}
wx, wy := float64(int8(wq[0])), float64(int8(wq[1]))
bx, by := float64(int8(bq[0])), float64(int8(bq[1]))
if wx == bx || wy == by ||
math.Abs(wx-bx) == math.Abs(wy-by) {
return true, nil
}
return false, nil
}