-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrid.go
55 lines (49 loc) · 984 Bytes
/
grid.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
package minesweeper
import (
"math"
"math/rand"
)
// Grid is playground.
type Grid [][]Cell
func newGrid(rows, cols int) Grid {
m := make(Grid, rows)
for i := range m {
m[i] = make([]Cell, cols)
}
return m
}
// GenerateGrid creates a matrix with randomly distributed bombs.
// rand.Seed should be run manually before GenerateGrid call.
func GenerateGrid(rows, cols int, diffc float64) Grid {
cells := rows * cols
bombs := int(math.Ceil(float64(cells) * diffc))
m := newGrid(rows, cols)
var offset int
for bombs > 0 {
offset += rand.Intn(rand.Intn(cells-offset-bombs) + 1)
i := offset / cols
j := offset % cols
m[i][j] = Bomb
bombs--
offset++
}
return m
}
// Stat returns info about cells.
func (m Grid) Stat() CellsStat {
var s CellsStat
for _, r := range m {
for _, c := range r {
if c.IsBomb() {
s.Bombs++
} else {
s.Free++
}
}
}
return s
}
// CellsStat contains info about cells.
type CellsStat struct {
Bombs, Free int
}