aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bingo/board.go27
-rw-r--r--bingo/board_test.go25
2 files changed, 35 insertions, 17 deletions
diff --git a/bingo/board.go b/bingo/board.go
index 479623f..1c63592 100644
--- a/bingo/board.go
+++ b/bingo/board.go
@@ -1,6 +1,9 @@
package bingo
-import "iter"
+import (
+ "fmt"
+ "iter"
+)
type WinDirection int
@@ -27,6 +30,19 @@ func all(group []Tile) bool {
return allTrue
}
+// create a new game of a given side length
+//
+// returns an error if side length is not odd
+func newGame(length int) (*Game, error) {
+ if length%2 != 1 || length < 3 {
+ return nil, fmt.Errorf("Invalid Side Length: %d", length)
+ }
+ game := new(Game)
+ game.Length = length
+
+ return game, nil
+}
+
// Return if a game has been won
func (g Game) Win() bool {
for row := range g.Rows() {
@@ -53,7 +69,7 @@ func (g Game) Win() bool {
// Iterator for rows of a board
func (g Game) Rows() iter.Seq[[]Tile] {
return func(yield func([]Tile) bool) {
- for row := 0; (row+1)*g.Length > len(g.Board); row++ {
+ for row := 0; (row+1)*g.Length <= len(g.Board); row++ {
if !yield(g.Board[row*g.Length : (row+1)*g.Length]) {
return
}
@@ -64,7 +80,7 @@ func (g Game) Rows() iter.Seq[[]Tile] {
// Iterator for columns of a board
func (g Game) Cols() iter.Seq[[]Tile] {
return func(yield func([]Tile) bool) {
- for col := 0; col*g.Length+1 > len(g.Board); col++ {
+ for col := 0; col*g.Length+1 < len(g.Board); col++ {
column := make([]Tile, g.Length)
for i := range g.Length {
column[i] = g.Board[i*g.Length+col]
@@ -77,6 +93,8 @@ func (g Game) Cols() iter.Seq[[]Tile] {
}
// Iterator for diagonals of square boards
+//
+// On non-square boards yields nothing
func (g Game) Diags() iter.Seq[[]Tile] {
return func(yield func([]Tile) bool) {
if g.Length*g.Length != len(g.Board) {
@@ -94,5 +112,8 @@ func (g Game) Diags() iter.Seq[[]Tile] {
for i := 0; i < g.Length; i++ {
diagonal[i] = g.Board[i*g.Length+(g.Length-1-i)]
}
+ if !yield(diagonal) {
+ return
+ }
}
}
diff --git a/bingo/board_test.go b/bingo/board_test.go
index a48eda5..7bff655 100644
--- a/bingo/board_test.go
+++ b/bingo/board_test.go
@@ -1,7 +1,6 @@
package bingo_test
import (
- "fmt"
"iter"
"testing"
@@ -14,20 +13,20 @@ func TestIters(t *testing.T) {
testGame := func(size int, length int) {
g.Board = make([]bingo.Tile, size)
+ g.Length = length
testGroup := func(name string, iter iter.Seq[[]bingo.Tile]) {
+ t.Log("Testing:", name)
for i := range size {
g.Board[i].Checked = false
}
count := 0
for group := range iter {
- fmt.Println("!")
- if len(group) != length {
- t.Logf("Mismatching %s length: %d != %d", name, length, len(group))
- t.FailNow()
+ if len(group) != g.Length {
+ t.Fatalf("Mismatching %s length: %d != %d", name, g.Length, len(group))
}
- for i := range length {
+ for i := range g.Length {
if group[i].Checked != false {
t.Errorf("Incorrect value in %s!\n", name)
}
@@ -35,19 +34,19 @@ func TestIters(t *testing.T) {
count++
}
- if count != 2 || count != size {
- t.Errorf("Expected to iterate 2 or %d times, iterated %d\n", size, count)
+ if count != 2 && count != length {
+ t.Errorf("Expected to iterate 2 or %d times, iterated %d\n", length, count)
}
for i := range size {
g.Board[i].Checked = true
}
for group := range iter {
- if len(group) != length {
- t.Logf("Mismatching %s length: %d != %d\n", name, length, len(group))
+ if len(group) != g.Length {
+ t.Logf("Mismatching %s length: %d != %d\n", name, g.Length, len(group))
}
- for i := range length {
+ for i := range g.Length {
if group[i].Checked != true {
t.Errorf("Incorrect value in %s!\n", name)
}
@@ -61,9 +60,7 @@ func TestIters(t *testing.T) {
}
- t.Log("Testing Square Games")
testGame(9, 3)
testGame(25, 5)
- t.Log("Testing Non-Square Games")
- testGame(22, 2)
+ testGame(49, 7)
}