-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoRangeEx.go
84 lines (75 loc) · 1.83 KB
/
goRangeEx.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import "fmt"
type File []bool
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 {
c := 0
for key, value := range cb {
if key == file {
for _, vraiFaux := range value {
if vraiFaux {
c++
}
}
}
}
return c
}
// CountInRank returns how many squares are occupied in the chessboard,
// within the given rank.
func CountInRank(cb Chessboard, rank int) int {
c := 0
if rank > 8 || rank < 1 {
return c
}
for _, value := range cb {
for i, vraiFaux := range value {
if vraiFaux && i+1 == rank {
c++
}
}
}
return c
}
// CountAll should count how many squares are present in the chessboard.
func CountAll(cb Chessboard) int {
c := 0
for _, value := range cb {
// c += len(value)
for i := 0; i < len(value); i++ {
c++
}
}
return c
}
// CountOccupied returns how many squares are occupied in the chessboard.
func CountOccupied(cb Chessboard) int {
c := 0
for _, value := range cb {
for _, vraiFaux := range value {
if vraiFaux {
c++
}
}
}
return c
}
func main() {
board := Chessboard{
"A": {true, false, true, false, false, false, false, true},
"B": {false, false, false, false, true, false, false, false},
"C": {false, false, true, false, false, false, false, false},
"D": {false, false, false, false, false, false, false, false},
"E": {false, false, false, false, false, true, false, true},
"F": {false, false, false, false, false, false, false, false},
"G": {false, false, false, true, false, false, false, false},
"H": {true, true, true, true, true, true, false, true},
}
fmt.Println(CountInFile(board, "A"))
fmt.Println(CountInRank(board, 2))
fmt.Println(CountAll(board))
fmt.Println(CountOccupied(board))
}
// Chessboard