Advertisement
Guest User

gol1.go

a guest
Nov 11th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.88 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "strconv"
  6.     "strings"
  7. )
  8.  
  9. type Activity int
  10.  
  11. const (
  12.     lives Activity = 0
  13.     dies  Activity = 1
  14.     unaffected Activity= 2
  15.  
  16. )
  17. //this is the main part to code
  18. // distributor divides the work between workers and interacts with other goroutines.
  19. func distributor(p golParams, d distributorChans, alive chan []cell) {
  20.  
  21.     // Create the 2D slice to store the world.
  22.     world := make([][]byte, p.imageHeight)
  23.     for i := range world {
  24.         world[i] = make([]byte, p.imageWidth)
  25.     }
  26.  
  27.     // Request the io goroutine to read in the image with the given filename.
  28.     d.io.command <- ioInput
  29.     d.io.filename <- strings.Join([]string{strconv.Itoa(p.imageWidth), strconv.Itoa(p.imageHeight)}, "x")
  30.  
  31.     // The io goroutine sends the requested image byte by byte, in rows.
  32.     for y := 0; y < p.imageHeight; y++ {
  33.         for x := 0; x < p.imageWidth; x++ {
  34.             fmt.Println("before channel ")
  35.             val := <-d.io.inputVal
  36.             fmt.Println("after channel")
  37.             if val != 0 {
  38.                 fmt.Println("Alive cell at", x, y)
  39.                 world[y][x] = val
  40.             }
  41.         }
  42.     }
  43.  
  44.     newWorld := make([][]byte, p.imageHeight)
  45.     for i:= range newWorld {
  46.         newWorld[i] = make([]byte, p.imageWidth)
  47.     }
  48.     // Calculate the new state of Game of Life after the given number of turns.
  49.     for turns := 0; turns < p.turns; turns++ {
  50.         //world= newWorld
  51.         for y := 0; y < p.imageHeight; y++ {
  52.             for x := 0; x < p.imageWidth; x++ {
  53.                
  54.                 // Placeholder for the actual Game of Life logic: flips alive cells to dead and dead cells to alive
  55.                 if CellActivity(p,x,y,world)==unaffected { continue }
  56.                 if CellActivity(p,x,y,world)== lives { newWorld[y][x]=0xFF } // cell lives
  57.                 if CellActivity(p,x,y,world)== dies  { newWorld[y][x]=0x00 } //  cell dies
  58.             }
  59.         }
  60.         world = newWorld //overwriting the old world with the newWorld before a new turn starts
  61.     }
  62.  
  63.     // Create an empty slice to store coordinates of cells that are still alive after p.turns are done.
  64.     var finalAlive []cell
  65.     // Go through the world and append the cells that are still alive.
  66.     for y := 0; y < p.imageHeight; y++ {
  67.         for x := 0; x < p.imageWidth; x++ {
  68.             if world[y][x] != 0x00{
  69.                 finalAlive = append(finalAlive, cell{x: x, y: y})
  70.             }
  71.         }
  72.     }
  73.     // Make sure that the Io has finished any output before exiting.
  74.     d.io.command <- ioCheckIdle
  75.     <-d.io.idle
  76.  
  77.     // Return the coordinates of cells that are still alive.
  78.     alive <- finalAlive //FOR TESTING FRAMEWORK
  79. }
  80.  
  81.  
  82. // Created a function that generates cell activity
  83. func CellActivity(p golParams ,x int ,y int, world [][] byte ) Activity {
  84.     aliveNeighbour := 0
  85.     for i := -1; i < 2; i++ {
  86.         for j := -1; j < 2; j++ {
  87.             if i == 0 && j == 0 { continue }
  88.             if world[(y+i)%p.imageHeight][(x+j)%p.imageWidth] == 0xFF { aliveNeighbour++ }
  89.         }
  90.     }
  91.     if aliveNeighbour < 2 {return dies}
  92.     if aliveNeighbour == 2 || aliveNeighbour == 3 {return unaffected}
  93.     if aliveNeighbour == 3 {return lives}
  94.     return dies
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement