Skip to content

Commit

Permalink
bugfix: used -1 with uint32 for x and y coordinates
Browse files Browse the repository at this point in the history
On the edges the neighbor counter did not work correct.

Some testcases for the coordinate converter functions are needed
to proove this as fixed.
  • Loading branch information
r2p2 committed Dec 31, 2011
1 parent 920cb5d commit 8a6ddb9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
30 changes: 15 additions & 15 deletions life.go
Expand Up @@ -16,11 +16,11 @@ const MAXWORKERINDEX = 1


type Field struct { type Field struct {
iteration uint64 iteration uint64
width, height uint32 width, height int32
currentArea, nextArea []byte currentArea, nextArea []byte
} }


func NewField(width, height uint32) *Field { func NewField(width, height int32) *Field {
rand.Seed(time.Nanoseconds()) rand.Seed(time.Nanoseconds())


cells := width * height cells := width * height
Expand Down Expand Up @@ -49,25 +49,25 @@ func (f *Field) Clear() {
} }
} }


func (f *Field) Set(x, y uint32, value byte) { func (f *Field) Set(x, y int32, value byte) {
f.currentArea[f.toArea(x, y)] = value f.currentArea[f.toArea(x, y)] = value
} }


func (f *Field) Step() { func (f *Field) Step() {
f.iteration++ f.iteration++


resChan := make(chan byte, MAXWORKERINDEX) resChan := make(chan byte, MAXWORKERINDEX)
for workerIndex := uint32(1); workerIndex <= MAXWORKERINDEX; workerIndex++ { for workerIndex := int32(1); workerIndex <= MAXWORKERINDEX; workerIndex++ {
go f.worker(workerIndex, resChan) go f.worker(workerIndex, resChan)
} }


for workerIndex := uint32(1); workerIndex <= MAXWORKERINDEX; workerIndex++ { for workerIndex := int32(1); workerIndex <= MAXWORKERINDEX; workerIndex++ {
<-resChan <-resChan
} }
f.swapFields() f.swapFields()
} }


func (f *Field) CellCount() uint32 { func (f *Field) CellCount() int32 {
return f.width * f.height return f.width * f.height
} }


Expand All @@ -77,8 +77,8 @@ func (f *Field) Iteration() uint64 {


func (f *Field) String() string { func (f *Field) String() string {
sbuffer := bytes.NewBufferString("") sbuffer := bytes.NewBufferString("")
for y := uint32(0); y < f.height; y++ { for y := int32(0); y < f.height; y++ {
for x := uint32(0); x < f.width; x++ { for x := int32(0); x < f.width; x++ {
index := f.toArea(x, y) index := f.toArea(x, y)
if f.currentArea[index] == 1 { if f.currentArea[index] == 1 {
fmt.Fprint(sbuffer, "#") fmt.Fprint(sbuffer, "#")
Expand All @@ -93,18 +93,18 @@ func (f *Field) String() string {


func (f *Field) StringNeighborMap() string { func (f *Field) StringNeighborMap() string {
sbuffer := bytes.NewBufferString("") sbuffer := bytes.NewBufferString("")
for y := uint32(0); y < f.height; y++ { for y := int32(0); y < f.height; y++ {
for x := uint32(0); x < f.width; x++ { for x := int32(0); x < f.width; x++ {
fmt.Fprint(sbuffer, f.countNeighbors(x, y)) fmt.Fprint(sbuffer, f.countNeighbors(x, y))
} }
fmt.Fprint(sbuffer, "\n") fmt.Fprint(sbuffer, "\n")
} }
return string(sbuffer.Bytes()) return string(sbuffer.Bytes())
} }


func (f *Field) worker(workerIndex uint32, resChan chan byte) { func (f *Field) worker(workerIndex int32, resChan chan byte) {
var neighbors byte var neighbors byte
var x, y uint32 var x, y int32
for cellIndex := workerIndex - 1; cellIndex < f.CellCount(); cellIndex += workerIndex { for cellIndex := workerIndex - 1; cellIndex < f.CellCount(); cellIndex += workerIndex {
x, y = f.toReal(cellIndex) x, y = f.toReal(cellIndex)
neighbors = f.countNeighbors(x, y) neighbors = f.countNeighbors(x, y)
Expand All @@ -120,13 +120,13 @@ func (f *Field) worker(workerIndex uint32, resChan chan byte) {
resChan <- 0 resChan <- 0
} }


func (f *Field) toReal(index uint32) (x, y uint32) { func (f *Field) toReal(index int32) (x, y int32) {
y = index / f.width y = index / f.width
x = index - y*f.width x = index - y*f.width
return return
} }


func (f *Field) toArea(x, y uint32) uint32 { func (f *Field) toArea(x, y int32) int32 {
if x < 0 { if x < 0 {
x = f.width - 1 x = f.width - 1
} else if x >= f.width { } else if x >= f.width {
Expand All @@ -141,7 +141,7 @@ func (f *Field) toArea(x, y uint32) uint32 {
return y*f.width + x return y*f.width + x
} }


func (f *Field) countNeighbors(x, y uint32) (neighbors byte) { func (f *Field) countNeighbors(x, y int32) (neighbors byte) {
neighbors += f.currentArea[f.toArea(x-1, y-1)] neighbors += f.currentArea[f.toArea(x-1, y-1)]
neighbors += f.currentArea[f.toArea(x, y-1)] neighbors += f.currentArea[f.toArea(x, y-1)]
neighbors += f.currentArea[f.toArea(x+1, y-1)] neighbors += f.currentArea[f.toArea(x+1, y-1)]
Expand Down
10 changes: 5 additions & 5 deletions life_test.go
Expand Up @@ -10,13 +10,13 @@ import (
"testing" "testing"
) )


var testcases [][3]uint32 var testcases [][3]int32


func init() { func init() {
testcases = [][3]uint32{ testcases = [][3]int32{
[3]uint32{0, 0, 0}, [3]int32{0, 0, 0},
[3]uint32{2, 1, 7}, [3]int32{2, 1, 7},
[3]uint32{3, 2, 13}, [3]int32{3, 2, 13},
} }
} }


Expand Down

0 comments on commit 8a6ddb9

Please sign in to comment.