Sha256: 83f6d46886eb7bad16d1791594fc09c2918eaaf3ac61e8524a10a9991bd8eb5e

Contents?: true

Size: 676 Bytes

Versions: 122

Compression:

Stored size: 676 Bytes

Contents

package queenattack

import "fmt"

func CanQueenAttack(w, b string) (attack bool, err error) {
	if err = valSq(w); err != nil {
		return false, err
	}
	if err = valSq(b); err != nil {
		return false, err
	}
	if w == b {
		return false, fmt.Errorf("queens on same square")
	}
	if w[0] == b[0] || // same file
		w[1] == b[1] { // same rank
		return true, nil
	}
	df := w[0] - b[0]
	dr := w[1] - b[1]
	return df == dr || // same white-kingside diagonal
			df+dr == 0, // same white-queenside diagonal
		nil
}

func valSq(s string) error {
	if len(s) != 2 ||
		s[0] < 'a' || s[0] > 'h' ||
		s[1] < '1' || s[1] > '8' {
		return fmt.Errorf("invalid square: %q", s)
	}
	return nil
}

Version data entries

122 entries across 122 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/go/exercises/queen-attack/example.go
trackler-2.2.1.179 tracks/go/exercises/queen-attack/example.go
trackler-2.2.1.178 tracks/go/exercises/queen-attack/example.go
trackler-2.2.1.177 tracks/go/exercises/queen-attack/example.go
trackler-2.2.1.176 tracks/go/exercises/queen-attack/example.go
trackler-2.2.1.175 tracks/go/exercises/queen-attack/example.go
trackler-2.2.1.174 tracks/go/exercises/queen-attack/example.go
trackler-2.2.1.173 tracks/go/exercises/queen-attack/example.go
trackler-2.2.1.172 tracks/go/exercises/queen-attack/example.go
trackler-2.2.1.171 tracks/go/exercises/queen-attack/example.go
trackler-2.2.1.170 tracks/go/exercises/queen-attack/example.go
trackler-2.2.1.169 tracks/go/exercises/queen-attack/example.go
trackler-2.2.1.167 tracks/go/exercises/queen-attack/example.go
trackler-2.2.1.166 tracks/go/exercises/queen-attack/example.go
trackler-2.2.1.165 tracks/go/exercises/queen-attack/example.go
trackler-2.2.1.164 tracks/go/exercises/queen-attack/example.go
trackler-2.2.1.163 tracks/go/exercises/queen-attack/example.go
trackler-2.2.1.162 tracks/go/exercises/queen-attack/example.go
trackler-2.2.1.161 tracks/go/exercises/queen-attack/example.go
trackler-2.2.1.160 tracks/go/exercises/queen-attack/example.go