aboutsummaryrefslogtreecommitdiffstats
path: root/bingo/board.go
diff options
context:
space:
mode:
Diffstat (limited to 'bingo/board.go')
-rw-r--r--bingo/board.go27
1 files changed, 24 insertions, 3 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
+ }
}
}