bingo-sim-0.0.5.0: A small playground to learn about profiling Haskell.

Safe HaskellNone
LanguageHaskell2010

BingoSim.Board

Contents

Description

This module contains the data types we'll be using, which is really just a way to represent and operate on boards.

For our purposes, we only need random boards, so it's sufficient to identify a board position by its index and then represent a board by a bit vector. Compared to a list or some other kind of sequence, this lets us use less space and perform operations on boards in fewer instructions.

Since we're using a Word64 but only representing boards with 36 grid spaces, we only use the 36 least significant bits: 0xf_ffff_ffff

We represent boards in row-major order, so each multiple of 6 bits is a row.

Synopsis

Boards

newtype Board Source #

The type of a 6x6 game board, as a bit sequence. Only the 36 least significant bits are meaningful.

Constructors

Board Word64 
Instances
Eq Board Source #

Only compares the 36 least significant bits.

Instance details

Defined in BingoSim.Board

Methods

(==) :: Board -> Board -> Bool #

(/=) :: Board -> Board -> Bool #

Show Board Source #

Visualize a board as a 6x6 grid.

>>> print (Board 0x123456789)
000100
100011
010001
010110
011110
001001
Instance details

Defined in BingoSim.Board

Methods

showsPrec :: Int -> Board -> ShowS #

show :: Board -> String #

showList :: [Board] -> ShowS #

PrintfArg Board Source #

So we can access the underlying Word64 easily when printing:

>>> printf "%#0*b\n" 38 (Board 0x1)
0b000000000000000000000000000000000001
Instance details

Defined in BingoSim.Board

Bingos

The main operation we want to do on a board is check whether it has a bingo or not.

hasBingo :: Board -> Maybe BingoLocation Source #

Check whether there's a bingo, and if so, return the BingoLocation corresponding to where the bingo occured (i.e., which row, column, or diagonal).

In the case where there are multiple bingos, returns one of them arbitrarily.

>>> hasBingo (Board 0x123456789)
Nothing
>>> hasBingo (Board 0x3f)
Just Row6

data BingoLocation Source #

An enum of where a bingo could occur on a 6x6 board.

Constructors

Dia1

Top left to bottom right

Dia2

Top right to bottom left

Row1

Top row

Row2 
Row3 
Row4 
Row5 
Row6

Bottom row

Col1

Far left column

Col2 
Col3 
Col4 
Col5 
Col6

Far right column

Board Constants

These are a handful of bit masks to project out the rows, columns, and diagonals of a board.

dia1 :: Word64 Source #

Diagonal from top left to bottom right

dia2 :: Word64 Source #

Diagonal from top right to bottom left

row1 :: Word64 Source #

Top row

row2 :: Word64 Source #

Second row from top

row3 :: Word64 Source #

Third row from top

row4 :: Word64 Source #

Fourth row from top

row5 :: Word64 Source #

Fifth row from top

row6 :: Word64 Source #

Bottom row

col1 :: Word64 Source #

Far left column

col2 :: Word64 Source #

Second column from left

col3 :: Word64 Source #

Third column from left

col4 :: Word64 Source #

Fourth column from left

col5 :: Word64 Source #

Fifth column from left

col6 :: Word64 Source #

Far right column