From 6dec8c74ef83e3ee3a888d32ba751b1f12772bc6 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Sat, 15 Jun 2024 12:06:17 +0200 Subject: [PATCH] bool => uint8 --- rle/rle.go | 4 ++-- src/config.go | 4 ++-- src/grid.go | 20 ++++++++++---------- src/play.go | 16 ++++++++-------- src/rule.go | 10 +++++----- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/rle/rle.go b/rle/rle.go index 6d10412..dbb931a 100644 --- a/rle/rle.go +++ b/rle/rle.go @@ -120,7 +120,7 @@ func removeWhitespace(input string) string { } // Store a grid to an RLE file -func StoreGridToRLE(grid [][]bool, filename, rule string, width, height int) error { +func StoreGridToRLE(grid [][]uint8, filename, rule string, width, height int) error { fd, err := os.Create(filename) if err != nil { return err @@ -132,7 +132,7 @@ func StoreGridToRLE(grid [][]bool, filename, rule string, width, height int) err line := "" for x := 0; x < width; x++ { char := "b" - if grid[y][x] { + if grid[y][x] == 1 { char = "o" } diff --git a/src/config.go b/src/config.go index 0c16e82..8a04095 100644 --- a/src/config.go +++ b/src/config.go @@ -44,8 +44,8 @@ type Config struct { const ( VERSION = "v0.0.8" - Alive = true - Dead = false + Alive = 1 + Dead = 0 DEFAULT_GRID_WIDTH = 600 DEFAULT_GRID_HEIGHT = 400 diff --git a/src/grid.go b/src/grid.go index 6f8af69..d78034c 100644 --- a/src/grid.go +++ b/src/grid.go @@ -13,16 +13,16 @@ import ( ) type Cell struct { - State bool + State uint8 Neighbors [8]*Cell NeighborCount int } -func (cell *Cell) Count() int { - count := 0 +func (cell *Cell) Count() uint8 { + var count uint8 for idx := 0; idx < cell.NeighborCount; idx++ { - count += bool2int(cell.Neighbors[idx].State) + count += cell.Neighbors[idx].State } return count @@ -97,7 +97,7 @@ func (grid *Grid) SetupNeighbors(x, y int) { } // count the living neighbors of a cell -func (grid *Grid) CountNeighbors(x, y int) int { +func (grid *Grid) CountNeighbors(x, y int) uint8 { return grid.Data[y][x].Count() } @@ -124,7 +124,7 @@ func (grid *Grid) Copy(other *Grid) { func (grid *Grid) Clear() { for y := range grid.Data { for x := range grid.Data[y] { - grid.Data[y][x].State = false + grid.Data[y][x].State = 0 } } } @@ -135,7 +135,7 @@ func (grid *Grid) FillRandom() { for y := range grid.Data { for x := range grid.Data[y] { if rand.Intn(grid.Config.Density) == 1 { - grid.Data[y][x].State = true + grid.Data[y][x].State = 1 } } } @@ -145,7 +145,7 @@ func (grid *Grid) FillRandom() { func (grid *Grid) Dump() { for y := 0; y < grid.Config.Height; y++ { for x := 0; x < grid.Config.Width; x++ { - if grid.Data[y][x].State { + if grid.Data[y][x].State == 1 { fmt.Print("XX") } else { fmt.Print(" ") @@ -168,7 +168,7 @@ func (grid *Grid) LoadRLE(pattern *rle.RLE) { x = colIndex + startX y = rowIndex + startY - grid.Data[y][x].State = true + grid.Data[y][x].State = 1 } } } @@ -279,7 +279,7 @@ func (grid *Grid) SaveState(filename, rule string) error { for y := range grid.Data { for _, cell := range grid.Data[y] { row := "." - if cell.State { + if cell.State == 1 { row = "o" } diff --git a/src/play.go b/src/play.go index ad46ede..e57766d 100644 --- a/src/play.go +++ b/src/play.go @@ -99,8 +99,8 @@ func (scene *ScenePlay) SetNext(next SceneName) { scene.Next = next } -func (scene *ScenePlay) CheckRule(state bool, neighbors int) bool { - var nextstate bool +func (scene *ScenePlay) CheckRule(state uint8, neighbors uint8) uint8 { + var nextstate uint8 // The standard Scene of Life is symbolized in rule-string notation // as B3/S23 (23/3 here). A cell is born if it has exactly three @@ -108,9 +108,9 @@ func (scene *ScenePlay) CheckRule(state bool, neighbors int) bool { // and dies otherwise. The first number, or list of numbers, is // what is required for a dead cell to be born. - if !state && Contains(scene.Config.Rule.Birth, neighbors) { + if state != 1 && Contains(scene.Config.Rule.Birth, neighbors) { nextstate = Alive - } else if state && Contains(scene.Config.Rule.Death, neighbors) { + } else if state == 1 && Contains(scene.Config.Rule.Death, neighbors) { nextstate = Alive } else { nextstate = Dead @@ -393,10 +393,10 @@ func (scene *ScenePlay) SaveRectRLE() { height = scene.Mark.Y - scene.Point.Y } - grid := make([][]bool, height) + grid := make([][]uint8, height) for y := 0; y < height; y++ { - grid[y] = make([]bool, width) + grid[y] = make([]uint8, width) for x := 0; x < width; x++ { grid[y][x] = scene.Grids[scene.Index].Data[y+starty][x+startx].State @@ -445,7 +445,7 @@ func (scene *ScenePlay) Update() error { } // set a cell to alive or dead -func (scene *ScenePlay) ToggleCellOnCursorPos(alive bool) { +func (scene *ScenePlay) ToggleCellOnCursorPos(alive uint8) { // use cursor pos relative to the world worldX, worldY := scene.Camera.ScreenToWorld(ebiten.CursorPosition()) x := int(worldX) / scene.Config.Cellsize @@ -478,7 +478,7 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) { if scene.Config.ShowEvolution { scene.DrawEvolution(screen, x, y, op) } else { - if scene.Grids[scene.Index].Data[y][x].State { + if scene.Grids[scene.Index].Data[y][x].State == 1 { scene.World.DrawImage(scene.Theme.Tile(ColLife), op) } } diff --git a/src/rule.go b/src/rule.go index 23585f7..3f2ad05 100644 --- a/src/rule.go +++ b/src/rule.go @@ -9,13 +9,13 @@ import ( // a GOL rule type Rule struct { Definition string - Birth []int - Death []int + Birth []uint8 + Death []uint8 } // parse one part of a GOL rule into rule slice -func NumbersToList(numbers string) []int { - list := []int{} +func NumbersToList(numbers string) []uint8 { + list := []uint8{} items := strings.Split(numbers, "") for _, item := range items { @@ -24,7 +24,7 @@ func NumbersToList(numbers string) []int { log.Fatalf("failed to parse game rule part <%s>: %s", numbers, err) } - list = append(list, num) + list = append(list, uint8(num)) } return list