-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcell.go
More file actions
60 lines (49 loc) · 968 Bytes
/
cell.go
File metadata and controls
60 lines (49 loc) · 968 Bytes
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
package minesweeper
// Cell constants
const (
Bomb = Cell(9)
Unfolded = Cell(1 << 4)
Flagged = Cell(1 << 5)
)
// Cell contains cell's state.
type Cell byte
// IsBomb checks does a cell contain a bomb
func (c Cell) IsBomb() bool {
return c.same(Bomb)
}
// Bombs returns bombs suggestions.
func (c Cell) Bombs() byte {
return byte(c &^ (Flagged | Unfolded))
}
// Unfolded checks that a cell is open.
func (c Cell) Unfolded() bool {
return c.same(Unfolded)
}
func (c Cell) any(t Cell) bool {
return c&t > 0
}
func (c Cell) same(t Cell) bool {
return c&t == t
}
func (c *Cell) unfold() {
c.Flag(false)
*c |= Unfolded
}
func (c *Cell) suggest(bombs byte) {
*c |= Cell(bombs)
}
// Flagged checks that a cell has a flag.
func (c Cell) Flagged() bool {
return c.same(Flagged)
}
// Flag sets or unsets a flag on the cell.
func (c *Cell) Flag(enable bool) {
if c.Unfolded() {
return
}
if enable {
*c |= Flagged
} else {
*c &^= Flagged
}
}