summaryrefslogtreecommitdiff
path: root/go/queen-attack/queen_attack.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-08-26 09:41:44 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-08-26 09:41:44 +0200
commitcf38d5bfd8567e7b35a3e8e04998b87e38e7af94 (patch)
tree3d152ccdab56cc289a7ff3527921827312f38d4f /go/queen-attack/queen_attack.go
parent5905c68a1fbae71682ff2edea7c009ad0355e9fb (diff)
Solve queens
Diffstat (limited to 'go/queen-attack/queen_attack.go')
-rw-r--r--go/queen-attack/queen_attack.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/go/queen-attack/queen_attack.go b/go/queen-attack/queen_attack.go
new file mode 100644
index 0000000..536ae47
--- /dev/null
+++ b/go/queen-attack/queen_attack.go
@@ -0,0 +1,38 @@
+package queenattack
+
+import "errors"
+
+func abs(n int) int {
+ if n < 0 {
+ return -n
+ }
+ return n
+}
+
+func parseLoc(l string) (int, int, error) {
+ if len(l) != 2 {
+ return 0, 0, errors.New("invalid")
+ }
+ if l[0] < 'a' || l[0] > 'h' {
+ return 0, 0, errors.New("off board")
+ }
+ if l[1] < '1' || l[1] > '8' {
+ return 0, 0, errors.New("off board")
+ }
+ return int(l[0] - 'a'), int(l[1] - '1'), nil
+}
+
+func CanQueenAttack(w, b string) (bool, error) {
+ if w == b {
+ return false, errors.New("same square")
+ }
+ wx, wy, err := parseLoc(w)
+ if err != nil {
+ return false, err
+ }
+ bx, by, err := parseLoc(b)
+ if err != nil {
+ return false, err
+ }
+ return wx == bx || wy == by || abs(wx-bx) == abs(wy-by), nil
+}