diff options
| author | JP Appel <jeanpierre.appel01@gmail.com> | 2024-09-28 03:23:36 -0400 |
|---|---|---|
| committer | JP Appel <jeanpierre.appel01@gmail.com> | 2024-09-28 03:23:36 -0400 |
| commit | d93bf6e3f52664c7782b14de54d606b5450d1866 (patch) | |
| tree | 9c444c2aeeecf9dd71ffcb1f3a32d0a68ed96f7d /bingo/board.go | |
| parent | 854feb079406163a0c64978bb2c66e0f38c2a231 (diff) | |
Fix board iterators on square boards
Diffstat (limited to 'bingo/board.go')
| -rw-r--r-- | bingo/board.go | 27 |
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 + } } } |
