mirror of
https://gitlab1.ptb.de/waltem01/Matrix
synced 2024-12-25 03:31:44 +00:00
uint typing and scale factor
This commit is contained in:
parent
a65dcddbc7
commit
de00ca4af9
37
conway.go
37
conway.go
@ -7,17 +7,17 @@ import (
|
||||
)
|
||||
|
||||
type Cell struct {
|
||||
x int
|
||||
y int
|
||||
x uint
|
||||
y uint
|
||||
live bool
|
||||
}
|
||||
|
||||
func (c *Cell) NeighborCount(arr [][]Cell) int {
|
||||
func (c *Cell) NeighborCount(arr [][]Cell) uint {
|
||||
// Read grid dimensions
|
||||
width := len(arr)
|
||||
height := len(arr[0])
|
||||
width := uint(len(arr))
|
||||
height := uint(len(arr[0]))
|
||||
|
||||
count := 0
|
||||
count := uint(0)
|
||||
// Iterate through 3x3 neighboring grid
|
||||
for i := -1; i <= 1; i++ {
|
||||
for j := -1; j <= 1; j++ {
|
||||
@ -26,8 +26,8 @@ func (c *Cell) NeighborCount(arr [][]Cell) int {
|
||||
continue
|
||||
}
|
||||
// Get neighbor coordinates
|
||||
nx := (c.x + i + width) % width
|
||||
ny := (c.y + j + height) % height
|
||||
nx := uint(int(c.x+width)+i) % width
|
||||
ny := uint(int(c.y+height)+j) % height
|
||||
// Count if neighbor is alive
|
||||
if arr[nx][ny].live {
|
||||
count++
|
||||
@ -37,7 +37,7 @@ func (c *Cell) NeighborCount(arr [][]Cell) int {
|
||||
return count
|
||||
}
|
||||
|
||||
func initGrid(width, height int, parent ...[][]Cell) ([][]Cell, error) {
|
||||
func initGrid(width, height uint, parent ...[][]Cell) ([][]Cell, error) {
|
||||
exists := len(parent) == 1
|
||||
if exists && len(parent) > 1 {
|
||||
return nil, errors.New("Too many parents specified")
|
||||
@ -45,10 +45,10 @@ func initGrid(width, height int, parent ...[][]Cell) ([][]Cell, error) {
|
||||
|
||||
// Make first dimension
|
||||
cells := make([][]Cell, width)
|
||||
for i := 0; i < width; i++ {
|
||||
for i := uint(0); i < width; i++ {
|
||||
// Make second dimension
|
||||
cells[i] = make([]Cell, height)
|
||||
for j := 0; j < height; j++ {
|
||||
for j := uint(0); j < height; j++ {
|
||||
// Make cells
|
||||
cells[i][j].x = i
|
||||
cells[i][j].y = j
|
||||
@ -63,9 +63,9 @@ func initGrid(width, height int, parent ...[][]Cell) ([][]Cell, error) {
|
||||
return cells, nil
|
||||
}
|
||||
|
||||
func setup(callback func([][]Cell), width, height int, FPS time.Duration) chan bool {
|
||||
// Initialize grid
|
||||
grid, _ := initGrid(width, height)
|
||||
func setup(callback func([][]Cell), scale, width, height uint, FPS time.Duration) chan bool {
|
||||
// Initialize grid at specified scale
|
||||
grid, _ := initGrid(width/scale, height/scale)
|
||||
|
||||
// Prepare ticker and finishing flag
|
||||
ticker := time.NewTicker((1000 / FPS) * time.Millisecond)
|
||||
@ -88,10 +88,13 @@ func setup(callback func([][]Cell), width, height int, FPS time.Duration) chan b
|
||||
}
|
||||
|
||||
func draw(grid [][]Cell, callback func([][]Cell)) [][]Cell {
|
||||
generation, _ := initGrid(len(grid), len(grid[0]), grid)
|
||||
width := uint(len(grid))
|
||||
height := uint(len(grid[0]))
|
||||
generation, _ := initGrid(width, height, grid)
|
||||
|
||||
// Iterate through grid
|
||||
for i := 0; i < len(grid); i++ {
|
||||
for j := 0; j < len(grid[0]); j++ {
|
||||
for i := uint(0); i < width; i++ {
|
||||
for j := uint(0); j < height; j++ {
|
||||
// Count neighbors
|
||||
cout := grid[i][j].NeighborCount(grid)
|
||||
// Get state
|
||||
|
15
main.go
15
main.go
@ -8,16 +8,16 @@ import (
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func output(client *resty.Client, url string, width, height int, arr [][]Cell) {
|
||||
func output(client *resty.Client, url string, scale, width, height uint, arr [][]Cell) {
|
||||
goterm.MoveCursor(1, 4)
|
||||
// Prepare instructions for matrix
|
||||
instructions := make([]interface{}, 1)
|
||||
instructions[0] = Clear{Endpoint: CLEAR}
|
||||
// Append all live cells as pixel instructions
|
||||
for j := 0; j < height; j++ {
|
||||
for i := 0; i < width; i++ {
|
||||
for j := 0; j < int(height/scale); j++ {
|
||||
for i := 0; i < int(width/scale); i++ {
|
||||
if arr[i][j].live {
|
||||
instructions = append(instructions, Pixel{X: i, Y: j, Endpoint: PIXEL})
|
||||
instructions = append(instructions, Rectangle{X: i * int(scale), Y: j * int(scale), W: scale, H: scale, Endpoint: RECTANGLE})
|
||||
goterm.Print("X")
|
||||
continue
|
||||
}
|
||||
@ -35,7 +35,8 @@ func output(client *resty.Client, url string, width, height int, arr [][]Cell) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
const FPS = 10
|
||||
const FPS = 2
|
||||
const SCALE = 2
|
||||
|
||||
// Try loading .env file if it exists
|
||||
godotenv.Load(".env")
|
||||
@ -54,8 +55,8 @@ func main() {
|
||||
|
||||
// Run Game of Life
|
||||
done := setup(func(c [][]Cell) {
|
||||
output(client, url, width, height, c)
|
||||
}, width, height, FPS)
|
||||
output(client, url, SCALE, width, height, c)
|
||||
}, SCALE, width, height, FPS)
|
||||
|
||||
// Wait for user input to quit
|
||||
fmt.Scanln()
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func loadMatrixData() (string, int, int) {
|
||||
func loadMatrixData() (string, uint, uint) {
|
||||
// Remote server info
|
||||
API_SERVER_IP, _ := getEnvFallback("API_SERVER_IP", "localhost")
|
||||
API_SERVER_PORT, _ := getEnvFallback("API_SERVER_PORT", "8080")
|
||||
@ -22,8 +22,8 @@ func loadMatrixData() (string, int, int) {
|
||||
url := fmt.Sprintf("http://%s:%s", API_SERVER_IP, API_SERVER_PORT)
|
||||
|
||||
// Calculate combined resolution
|
||||
width := PUBLIC_LED_WIDTH * PUBLIC_LED_CHAIN
|
||||
height := PUBLIC_LED_HEIGHT * PUBLIC_LED_PARALLEL
|
||||
width := uint(PUBLIC_LED_WIDTH * PUBLIC_LED_CHAIN)
|
||||
height := uint(PUBLIC_LED_HEIGHT * PUBLIC_LED_PARALLEL)
|
||||
|
||||
return url, width, height
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user