bool => uint8

This commit is contained in:
2024-06-15 12:06:17 +02:00
committed by T.v.Dein
parent 861ba86b0c
commit 6dec8c74ef
5 changed files with 27 additions and 27 deletions

View File

@@ -120,7 +120,7 @@ func removeWhitespace(input string) string {
} }
// Store a grid to an RLE file // 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) fd, err := os.Create(filename)
if err != nil { if err != nil {
return err return err
@@ -132,7 +132,7 @@ func StoreGridToRLE(grid [][]bool, filename, rule string, width, height int) err
line := "" line := ""
for x := 0; x < width; x++ { for x := 0; x < width; x++ {
char := "b" char := "b"
if grid[y][x] { if grid[y][x] == 1 {
char = "o" char = "o"
} }

View File

@@ -44,8 +44,8 @@ type Config struct {
const ( const (
VERSION = "v0.0.8" VERSION = "v0.0.8"
Alive = true Alive = 1
Dead = false Dead = 0
DEFAULT_GRID_WIDTH = 600 DEFAULT_GRID_WIDTH = 600
DEFAULT_GRID_HEIGHT = 400 DEFAULT_GRID_HEIGHT = 400

View File

@@ -13,16 +13,16 @@ import (
) )
type Cell struct { type Cell struct {
State bool State uint8
Neighbors [8]*Cell Neighbors [8]*Cell
NeighborCount int NeighborCount int
} }
func (cell *Cell) Count() int { func (cell *Cell) Count() uint8 {
count := 0 var count uint8
for idx := 0; idx < cell.NeighborCount; idx++ { for idx := 0; idx < cell.NeighborCount; idx++ {
count += bool2int(cell.Neighbors[idx].State) count += cell.Neighbors[idx].State
} }
return count return count
@@ -97,7 +97,7 @@ func (grid *Grid) SetupNeighbors(x, y int) {
} }
// count the living neighbors of a cell // 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() return grid.Data[y][x].Count()
} }
@@ -124,7 +124,7 @@ func (grid *Grid) Copy(other *Grid) {
func (grid *Grid) Clear() { func (grid *Grid) Clear() {
for y := range grid.Data { for y := range grid.Data {
for x := range grid.Data[y] { 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 y := range grid.Data {
for x := range grid.Data[y] { for x := range grid.Data[y] {
if rand.Intn(grid.Config.Density) == 1 { 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() { func (grid *Grid) Dump() {
for y := 0; y < grid.Config.Height; y++ { for y := 0; y < grid.Config.Height; y++ {
for x := 0; x < grid.Config.Width; x++ { for x := 0; x < grid.Config.Width; x++ {
if grid.Data[y][x].State { if grid.Data[y][x].State == 1 {
fmt.Print("XX") fmt.Print("XX")
} else { } else {
fmt.Print(" ") fmt.Print(" ")
@@ -168,7 +168,7 @@ func (grid *Grid) LoadRLE(pattern *rle.RLE) {
x = colIndex + startX x = colIndex + startX
y = rowIndex + startY 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 y := range grid.Data {
for _, cell := range grid.Data[y] { for _, cell := range grid.Data[y] {
row := "." row := "."
if cell.State { if cell.State == 1 {
row = "o" row = "o"
} }

View File

@@ -99,8 +99,8 @@ func (scene *ScenePlay) SetNext(next SceneName) {
scene.Next = next scene.Next = next
} }
func (scene *ScenePlay) CheckRule(state bool, neighbors int) bool { func (scene *ScenePlay) CheckRule(state uint8, neighbors uint8) uint8 {
var nextstate bool var nextstate uint8
// The standard Scene of Life is symbolized in rule-string notation // 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 // 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 // and dies otherwise. The first number, or list of numbers, is
// what is required for a dead cell to be born. // 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 nextstate = Alive
} else if state && Contains(scene.Config.Rule.Death, neighbors) { } else if state == 1 && Contains(scene.Config.Rule.Death, neighbors) {
nextstate = Alive nextstate = Alive
} else { } else {
nextstate = Dead nextstate = Dead
@@ -393,10 +393,10 @@ func (scene *ScenePlay) SaveRectRLE() {
height = scene.Mark.Y - scene.Point.Y height = scene.Mark.Y - scene.Point.Y
} }
grid := make([][]bool, height) grid := make([][]uint8, height)
for y := 0; y < height; y++ { for y := 0; y < height; y++ {
grid[y] = make([]bool, width) grid[y] = make([]uint8, width)
for x := 0; x < width; x++ { for x := 0; x < width; x++ {
grid[y][x] = scene.Grids[scene.Index].Data[y+starty][x+startx].State 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 // 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 // use cursor pos relative to the world
worldX, worldY := scene.Camera.ScreenToWorld(ebiten.CursorPosition()) worldX, worldY := scene.Camera.ScreenToWorld(ebiten.CursorPosition())
x := int(worldX) / scene.Config.Cellsize x := int(worldX) / scene.Config.Cellsize
@@ -478,7 +478,7 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
if scene.Config.ShowEvolution { if scene.Config.ShowEvolution {
scene.DrawEvolution(screen, x, y, op) scene.DrawEvolution(screen, x, y, op)
} else { } 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) scene.World.DrawImage(scene.Theme.Tile(ColLife), op)
} }
} }

View File

@@ -9,13 +9,13 @@ import (
// a GOL rule // a GOL rule
type Rule struct { type Rule struct {
Definition string Definition string
Birth []int Birth []uint8
Death []int Death []uint8
} }
// parse one part of a GOL rule into rule slice // parse one part of a GOL rule into rule slice
func NumbersToList(numbers string) []int { func NumbersToList(numbers string) []uint8 {
list := []int{} list := []uint8{}
items := strings.Split(numbers, "") items := strings.Split(numbers, "")
for _, item := range items { 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) log.Fatalf("failed to parse game rule part <%s>: %s", numbers, err)
} }
list = append(list, num) list = append(list, uint8(num))
} }
return list return list