-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
66 lines (60 loc) · 1.42 KB
/
main.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
package main
import (
"fmt"
"math/rand"
"os"
"github.com/NotUnlikeTheWaves/minesweeper/board"
tea "github.com/charmbracelet/bubbletea"
)
func returnOneIfEmptyAndCellExists(minefield [][]board.Cell, y int, x int) int {
if y < 0 || x < 0 || y >= len(minefield) || x >= len(minefield[0]) {
return 0
}
if minefield[y][x].IsBomb {
return 1
}
return 0
}
// TODO: Generate a deterministic mine field
func generateMinefield(height int, width int) [][]board.Cell {
chanceOfBomb := 10
var minefield = make([][]board.Cell, height)
for h := range minefield {
minefield[h] = make([]board.Cell, width)
for w := range minefield[h] {
c := board.Cell{IsBomb: rand.Intn(100) < chanceOfBomb}
minefield[h][w] = c
}
}
for h := range minefield {
for w := range minefield[h] {
for y := -1; y <= 1; y++ {
for x := -1; x <= 1; x++ {
if x != 0 || y != 0 {
minefield[h][w].SurroundingBombs +=
returnOneIfEmptyAndCellExists(minefield, h+y, w+x)
}
}
}
}
}
return minefield
}
func initialBoard() board.Board {
minefield := generateMinefield(10, 40)
return board.Board{
Cells: minefield,
CurrentCell: &minefield[0][0],
Cursor: board.Cursor{X: 0, Y: 0},
Height: 10,
Width: 40,
GameState: board.Normal,
}
}
func main() {
p := tea.NewProgram(initialBoard())
if err := p.Start(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
}