Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "strconv"
- "strings"
- )
- type Activity int
- const (
- lives Activity = 0
- dies Activity = 1
- unaffected Activity= 2
- )
- //this is the main part to code
- // distributor divides the work between workers and interacts with other goroutines.
- func distributor(p golParams, d distributorChans, alive chan []cell) {
- // Create the 2D slice to store the world.
- world := make([][]byte, p.imageHeight)
- for i := range world {
- world[i] = make([]byte, p.imageWidth)
- }
- // Request the io goroutine to read in the image with the given filename.
- d.io.command <- ioInput
- d.io.filename <- strings.Join([]string{strconv.Itoa(p.imageWidth), strconv.Itoa(p.imageHeight)}, "x")
- // The io goroutine sends the requested image byte by byte, in rows.
- for y := 0; y < p.imageHeight; y++ {
- for x := 0; x < p.imageWidth; x++ {
- fmt.Println("before channel ")
- val := <-d.io.inputVal
- fmt.Println("after channel")
- if val != 0 {
- fmt.Println("Alive cell at", x, y)
- world[y][x] = val
- }
- }
- }
- newWorld := make([][]byte, p.imageHeight)
- for i:= range newWorld {
- newWorld[i] = make([]byte, p.imageWidth)
- }
- // Calculate the new state of Game of Life after the given number of turns.
- for turns := 0; turns < p.turns; turns++ {
- //world= newWorld
- for y := 0; y < p.imageHeight; y++ {
- for x := 0; x < p.imageWidth; x++ {
- // Placeholder for the actual Game of Life logic: flips alive cells to dead and dead cells to alive
- if CellActivity(p,x,y,world)==unaffected { continue }
- if CellActivity(p,x,y,world)== lives { newWorld[y][x]=0xFF } // cell lives
- if CellActivity(p,x,y,world)== dies { newWorld[y][x]=0x00 } // cell dies
- }
- }
- world = newWorld //overwriting the old world with the newWorld before a new turn starts
- }
- // Create an empty slice to store coordinates of cells that are still alive after p.turns are done.
- var finalAlive []cell
- // Go through the world and append the cells that are still alive.
- for y := 0; y < p.imageHeight; y++ {
- for x := 0; x < p.imageWidth; x++ {
- if world[y][x] != 0x00{
- finalAlive = append(finalAlive, cell{x: x, y: y})
- }
- }
- }
- // Make sure that the Io has finished any output before exiting.
- d.io.command <- ioCheckIdle
- <-d.io.idle
- // Return the coordinates of cells that are still alive.
- alive <- finalAlive //FOR TESTING FRAMEWORK
- }
- // Created a function that generates cell activity
- func CellActivity(p golParams ,x int ,y int, world [][] byte ) Activity {
- aliveNeighbour := 0
- for i := -1; i < 2; i++ {
- for j := -1; j < 2; j++ {
- if i == 0 && j == 0 { continue }
- if world[(y+i)%p.imageHeight][(x+j)%p.imageWidth] == 0xFF { aliveNeighbour++ }
- }
- }
- if aliveNeighbour < 2 {return dies}
- if aliveNeighbour == 2 || aliveNeighbour == 3 {return unaffected}
- if aliveNeighbour == 3 {return lives}
- return dies
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement