{-# LANGUAGE BinaryLiterals #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NumericUnderscores #-}
module BingoSim.Board where
import Data.Bits
import Data.Word
import Text.Printf
newtype Board = Board Word64
deriving
( PrintfArg
)
instance Eq Board where
(==) (Board board1) (Board board2) =
let mask = 0xf_ffff_ffff
in (board1 .&. mask) == (board2 .&. mask)
instance Show Board where
show (Board board) =
let r1 = (board .&. row1) `shiftR` 30
r2 = (board .&. row2) `shiftR` 24
r3 = (board .&. row3) `shiftR` 18
r4 = (board .&. row4) `shiftR` 12
r5 = (board .&. row5) `shiftR` 6
r6 = (board .&. row6)
in printf "%06b\n%06b\n%06b\n%06b\n%06b\n%06b\n" r1 r2 r3 r4 r5 r6
hasBingo :: Board -> Maybe BingoLocation
hasBingo (Board board) | (board .&. dia1 == dia1) = Just Dia1
hasBingo (Board board) | (board .&. dia2 == dia2) = Just Dia2
hasBingo (Board board) | (board .&. row1 == row1) = Just Row1
hasBingo (Board board) | (board .&. row2 == row2) = Just Row2
hasBingo (Board board) | (board .&. row3 == row3) = Just Row3
hasBingo (Board board) | (board .&. row4 == row4) = Just Row4
hasBingo (Board board) | (board .&. row5 == row5) = Just Row5
hasBingo (Board board) | (board .&. row6 == row6) = Just Row6
hasBingo (Board board) | (board .&. col1 == col1) = Just Col1
hasBingo (Board board) | (board .&. col2 == col2) = Just Col2
hasBingo (Board board) | (board .&. col3 == col3) = Just Col3
hasBingo (Board board) | (board .&. col4 == col4) = Just Col4
hasBingo (Board board) | (board .&. col5 == col5) = Just Col5
hasBingo (Board board) | (board .&. col6 == col6) = Just Col6
hasBingo _ = Nothing
data BingoLocation
= Dia1
| Dia2
| Row1
| Row2
| Row3
| Row4
| Row5
| Row6
| Col1
| Col2
| Col3
| Col4
| Col5
| Col6
deriving (Eq, Show)
dia1 :: Word64
dia1 = 0b100000010000001000000100000010000001
dia2 :: Word64
dia2 = 0b000001000010000100001000010000100000
row1 :: Word64
row1 = 0b111111000000000000000000000000000000
row2 :: Word64
row2 = 0b000000111111000000000000000000000000
row3 :: Word64
row3 = 0b000000000000111111000000000000000000
row4 :: Word64
row4 = 0b000000000000000000111111000000000000
row5 :: Word64
row5 = 0b000000000000000000000000111111000000
row6 :: Word64
row6 = 0b000000000000000000000000000000111111
col1 :: Word64
col1 = 0b100000100000100000100000100000100000
col2 :: Word64
col2 = 0b010000010000010000010000010000010000
col3 :: Word64
col3 = 0b001000001000001000001000001000001000
col4 :: Word64
col4 = 0b000100000100000100000100000100000100
col5 :: Word64
col5 = 0b000010000010000010000010000010000010
col6 :: Word64
col6 = 0b000001000001000001000001000001000001