-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchessboard.go
59 lines (53 loc) · 1.27 KB
/
chessboard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package chessboard
// Declare a type named File which stores if a square is occupied by a piece - this will be a slice of bools
type File []bool
// Declare a type named Chessboard which contains a map of eight Files, accessed with keys from "A" to "H"
type Chessboard map[string]File
// CountInFile returns how many squares are occupied in the chessboard,
// within the given file.
func CountInFile(cb Chessboard, file string) int {
var occupied int
if val, ok := cb[file]; ok {
for _, i := range val {
if i {
occupied++
}
}
return occupied
}
return 0
}
// CountInRank returns how many squares are occupied in the chessboard,
// within the given rank.
func CountInRank(cb Chessboard, rank int) int {
var occupied int
if rank > 0 && rank < 9 {
for _, val := range cb {
if val[rank-1] {
occupied++
}
}
return occupied
}
return 0
}
// CountAll should count how many squares are present in the chessboard.
func CountAll(cb Chessboard) int {
var total int
for _, val := range cb {
total += len(val)
}
return total
}
// CountOccupied returns how many squares are occupied in the chessboard.
func CountOccupied(cb Chessboard) int {
var occupied int
for _, i := range cb {
for _, j := range i {
if j {
occupied++
}
}
}
return occupied
}