mirror of
https://codeberg.org/scip/golsky.git
synced 2025-12-16 12:10:58 +01:00
bool => uint8
This commit is contained in:
@@ -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"
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
20
src/grid.go
20
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"
|
||||
}
|
||||
|
||||
|
||||
16
src/play.go
16
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)
|
||||
}
|
||||
}
|
||||
|
||||
10
src/rule.go
10
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
|
||||
|
||||
Reference in New Issue
Block a user