Initial upload
This commit is contained in:
55
go/chessboard/chessboard.go
Normal file
55
go/chessboard/chessboard.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package chessboard
|
||||
|
||||
// Declare a type named Rank which stores if a square is occupied by a piece - this will be a slice of bools
|
||||
// Declare a type named Chessboard contains a map of eight Ranks, accessed with values from "A" to "H"
|
||||
type Rank []bool
|
||||
type Chessboard map[string]Rank
|
||||
// CountInRank returns how many squares are occupied in the chessboard,
|
||||
// within the given rank
|
||||
func CountInRank(cb Chessboard, rank string) int {
|
||||
sum := 0
|
||||
file, ok := cb[rank]
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
for _, f := range file {
|
||||
if f {
|
||||
sum += 1
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
// CountInFile returns how many squares are occupied in the chessboard,
|
||||
// within the given file
|
||||
func CountInFile(cb Chessboard, file int) int {
|
||||
if file < 1 || file > 8 {
|
||||
return 0
|
||||
}
|
||||
sum := 0
|
||||
for _, rank := range cb {
|
||||
if rank[file-1] {
|
||||
sum += 1
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
// CountAll should count how many squares are present in the chessboard
|
||||
func CountAll(cb Chessboard) int {
|
||||
sum := 0
|
||||
for _, rank := range cb {
|
||||
sum += len(rank)
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
||||
|
||||
// CountOccupied returns how many squares are occupied in the chessboard
|
||||
func CountOccupied(cb Chessboard) int {
|
||||
sum := 0
|
||||
for name := range cb {
|
||||
sum += CountInRank(cb, name)
|
||||
}
|
||||
return sum
|
||||
}
|
Reference in New Issue
Block a user