Permalink
Browse files

bugfix: used -1 with uint32 for x and y coordinates

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...
1 parent 920cb5d commit 8a6ddb91eb550cd739ad8764012f85368f90035c @r2p2 committed Dec 31, 2011
Showing with 20 additions and 20 deletions.
  1. +15 −15 life.go
  2. +5 −5 life_test.go
View
30 life.go
@@ -16,11 +16,11 @@ const MAXWORKERINDEX = 1
type Field struct {
iteration uint64
- width, height uint32
+ width, height int32
currentArea, nextArea []byte
}
-func NewField(width, height uint32) *Field {
+func NewField(width, height int32) *Field {
rand.Seed(time.Nanoseconds())
cells := width * height
@@ -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
}
func (f *Field) Step() {
f.iteration++
resChan := make(chan byte, MAXWORKERINDEX)
- for workerIndex := uint32(1); workerIndex <= MAXWORKERINDEX; workerIndex++ {
+ for workerIndex := int32(1); workerIndex <= MAXWORKERINDEX; workerIndex++ {
go f.worker(workerIndex, resChan)
}
- for workerIndex := uint32(1); workerIndex <= MAXWORKERINDEX; workerIndex++ {
+ for workerIndex := int32(1); workerIndex <= MAXWORKERINDEX; workerIndex++ {
<-resChan
}
f.swapFields()
}
-func (f *Field) CellCount() uint32 {
+func (f *Field) CellCount() int32 {
return f.width * f.height
}
@@ -77,8 +77,8 @@ func (f *Field) Iteration() uint64 {
func (f *Field) String() string {
sbuffer := bytes.NewBufferString("")
- for y := uint32(0); y < f.height; y++ {
- for x := uint32(0); x < f.width; x++ {
+ for y := int32(0); y < f.height; y++ {
+ for x := int32(0); x < f.width; x++ {
index := f.toArea(x, y)
if f.currentArea[index] == 1 {
fmt.Fprint(sbuffer, "#")
@@ -93,18 +93,18 @@ func (f *Field) String() string {
func (f *Field) StringNeighborMap() string {
sbuffer := bytes.NewBufferString("")
- for y := uint32(0); y < f.height; y++ {
- for x := uint32(0); x < f.width; x++ {
+ for y := int32(0); y < f.height; y++ {
+ for x := int32(0); x < f.width; x++ {
fmt.Fprint(sbuffer, f.countNeighbors(x, y))
}
fmt.Fprint(sbuffer, "\n")
}
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 x, y uint32
+ var x, y int32
for cellIndex := workerIndex - 1; cellIndex < f.CellCount(); cellIndex += workerIndex {
x, y = f.toReal(cellIndex)
neighbors = f.countNeighbors(x, y)
@@ -120,13 +120,13 @@ func (f *Field) worker(workerIndex uint32, resChan chan byte) {
resChan <- 0
}
-func (f *Field) toReal(index uint32) (x, y uint32) {
+func (f *Field) toReal(index int32) (x, y int32) {
y = index / f.width
x = index - y*f.width
return
}
-func (f *Field) toArea(x, y uint32) uint32 {
+func (f *Field) toArea(x, y int32) int32 {
if x < 0 {
x = f.width - 1
} else if x >= f.width {
@@ -141,7 +141,7 @@ func (f *Field) toArea(x, y uint32) uint32 {
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, y-1)]
neighbors += f.currentArea[f.toArea(x+1, y-1)]
View
@@ -10,13 +10,13 @@ import (
"testing"
)
-var testcases [][3]uint32
+var testcases [][3]int32
func init() {
- testcases = [][3]uint32{
- [3]uint32{0, 0, 0},
- [3]uint32{2, 1, 7},
- [3]uint32{3, 2, 13},
+ testcases = [][3]int32{
+ [3]int32{0, 0, 0},
+ [3]int32{2, 1, 7},
+ [3]int32{3, 2, 13},
}
}

0 comments on commit 8a6ddb9

Please sign in to comment.