bingo

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package bingo provides structures to simulate bingo games, boards, and number values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Board

type Board [25]Number

Board represents a 5*5 square bingo board. The middle square (index 12) is left empty (0).

func BoardFromID

func BoardFromID(id string) (*Board, error)

BoardFromID converts the board id to a Board. An error is returned if the id is for an invalid board.

func NewBoard

func NewBoard() *Board

NewBoard creates a board by drawing numbers from a game. Each column of the board (5-cell group) only contains numbers of the same column.

func (Board) HasLine

func (b Board) HasLine(g Game) bool

HasLine determines if the board has a five-in-a row line, creating a BINGO for the game.

func (Board) ID

func (b Board) ID() (string, error)

ID encodes the board into a base64 string. Each two numbers can be shrunk to a 0-14 number, concatenated, and converted to a byte. This results in a byte array that is (25-1)/2 = 12 characters long Since there are 8 bits in a byte the array uses 8 * 12 = 96 bits. Base 64 uses 6 bits for each character, so the string will be 96 / 6 = 16 characters long

func (Board) IsFilled

func (b Board) IsFilled(g Game) bool

IsFilled determines if all the numbers in the board have been called in the game.

type Game

type Game struct {
	// contains filtered or unexported fields
}

Game represents a bingo game. The zero value can be used to start a new game.

func GameFromID

func GameFromID(id string) (*Game, error)

GameFromID creates a game from the identifying string.

func (*Game) DrawNumber

func (g *Game) DrawNumber()

DrawNumber move the next available number to DrawnNumbers. The game is reset if no numbers have been drawn.

func (Game) DrawnNumberColumns

func (g Game) DrawnNumberColumns() map[int][]Number

DrawnNumberColumns partitions the drawn numbers by columns in the order that they were drawn.

func (Game) DrawnNumbers

func (g Game) DrawnNumbers() []Number

DrawnNumbers is the numbers in the game that have been drawn

func (Game) ID

func (g Game) ID() (string, error)

ID encodes the game into an easy to transport string.

func (Game) NumbersLeft

func (g Game) NumbersLeft() int

NumbersLeft reports how many available numbers in the game can be drawn.

func (Game) PreviousNumberDrawn

func (g Game) PreviousNumberDrawn() Number

PreviousNumberDrawn is the last number drawn, or 0 of no numbers have been drawn.

type Number

type Number int

Number represents a value that can be drawn in the game. Boards are made of numbers. If a board has five numbers in a row, column, or diagonal, the board has a BINGO.

const (
	// MinNumber is the minimum allowed value of a Number.
	MinNumber Number = 1
	// MaxNumber is the maximum allowed value of a Number.
	MaxNumber Number = 75
)

func (Number) Column

func (n Number) Column() int

Column is the location on the board the Number should be located at.

func (Number) String

func (n Number) String() string

String is the concatenation of the Number's column letter, a space, and integer value as a string.

func (Number) Valid

func (n Number) Valid() bool

Valid returns whether or not the number is valid, that is, it is between 1 and 75.

func (Number) Value

func (n Number) Value() int

Value is integer numeric value of the Number.

type Resetter

type Resetter interface {
	// Reset resets the game.
	Reset(g *Game)
	// Seed sets the GameResetter to reset the next game from a starting point.
	Seed(seed int64)
}

Resetter resets games to valid, shuffled states. It can be seeded to be predictable reset the next reset game.

var GameResetter Resetter = &shuffler{
	Rand: rand.New(rand.NewSource(time.Now().UnixNano())),
	swap: func(numbers []Number) func(i, j int) {
		return func(i, j int) {
			numbers[i], numbers[j] = numbers[j], numbers[i]
		}
	},
}

GameResetter shuffles the game numbers. It is seeded to the time it is created; it should only be used when testing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL