feat: create and render inital map.
This commit is contained in:
parent
87a28a1361
commit
038d214f75
5 changed files with 216 additions and 2 deletions
76
main.go
76
main.go
|
@ -2,8 +2,80 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello flake")
|
||||
// Create simple constants
|
||||
const (
|
||||
ROW = 35
|
||||
COL = 70
|
||||
)
|
||||
|
||||
// Just need a grid and buffer
|
||||
type gol struct {
|
||||
grid [ROW][COL]int
|
||||
buffer [ROW][COL]int
|
||||
}
|
||||
|
||||
// Initialize empty game of life
|
||||
func initGol() gol {
|
||||
start := [ROW][COL]int{}
|
||||
for i := 0; i < ROW; i++ {
|
||||
for j := 0; j < COL; j++ {
|
||||
if rand.Intn(2) == 1 {
|
||||
start[i][j] = 1
|
||||
} else {
|
||||
start[i][j] = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
return gol{
|
||||
grid: start,
|
||||
buffer: start,
|
||||
}
|
||||
}
|
||||
|
||||
// Simple init
|
||||
func (g gol) Init() tea.Cmd {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Simple update
|
||||
func (g gol) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
// Just quit when using ctrl+c or q
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "ctrl+c", "q":
|
||||
return g, tea.Quit
|
||||
}
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
|
||||
// Let's draw this all simply
|
||||
func (g gol) View() string {
|
||||
var s string
|
||||
for i := 0; i < ROW; i++ {
|
||||
for j := 0; j < COL; j++ {
|
||||
if g.grid[i][j] == 1 {
|
||||
s += "*"
|
||||
} else {
|
||||
s += " "
|
||||
}
|
||||
}
|
||||
s += "\n"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func main() {
|
||||
p := tea.NewProgram(initGol())
|
||||
if _, err := p.Run(); err != nil {
|
||||
fmt.Printf("Oh no, an error: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue