fyne-io / life

Fyne game of life simulation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

improvements

MatejMagat305 opened this issue · comments

I sugest these changes:

  1. add to board nextGenCells:
type board struct {
	cells         [][]bool
	nextGenCells  [][]bool
	generation    int
	width, height int
}
  1. change nextGen() to not copy version :
func (b *board) nextGen() {
	b.computeNextGen()
	b.generation++
	b.nextGenCells, b.cells = b.cells, b.nextGenCells
}
  1. change computeNextGen() to not create version:
func (b *board) computeNextGen() {
	for y := 0; y < b.height; y++ {
		for x := 0; x < b.width; x++ {
			n := b.countNeighbours(x, y)
			if b.cells[y][x] {
				b.nextGenCells[y][x] = n == 2 || n == 3
			} else {
				b.nextGenCells[y][x] = n == 3
			}
		}
	}
}
  1. maybe change computeNextGen() to concurrent version:
func (b *board) computeNextGen() {
	for y := 0; y < b.height; y++ {
		for x := 0; x < b.width; x++ {
			go func(x,y int) {
				n := b.countNeighbours(x, y)
				if b.cells[y][x] {
					b.nextGenCells[y][x] = n == 2 || n == 3
				} else {
					b.nextGenCells[y][x] = n == 3
				}
				b.ch<-true
			}(x,y)
		}
	}
	for y := 0; y < b.height; y++ {
		for x := 0; x < b.width; x++ {
			<-b.ch
		}
	}
}
  1. update buildUI() to container.new :
func (g *game) buildUI() fyne.CanvasObject {
	var pause *widget.Button
	pause = widget.NewButton("Pause", func() {
		g.paused = !g.paused

		if g.paused {
			pause.SetText("Resume")
		} else {
			pause.SetText("Pause")
		}
	})

	title := container.New(layout.NewGridLayout(2), g.genText, pause)
	return container.New(layout.NewBorderLayout(title, nil, nil, nil), title, g)
}

These changes are very much appreciated. Any help with improving code is great, but sorry, this is not how you suggest changes to open source projects. If you want to improve the code, please open a PR so we can review the code and then perhaps merge it in.