battleship

Safe HaskellSafe

MatrixUntrusted

Description

This untrusted module supplements the small matrices module with functions for describing boards as strings and lists of strings.

Synopsis

Documentation

class Cell a where

Typeclass for matrix cells: provides a function for converting a cell to a character.

Methods

toChar :: a -> Char

Convert an instance of class to character.

Instances

matrixToStr :: Cell a => Int -> Int -> Matrix a -> String

matrixToStr ht wid rows raises an exception if valid ht wid rows is False. Otherwise it converts rows into a string that describes it, using toChar (from typeclass Cell) to convert each cell of the matrix to a character, and indexing the rows and columns by lowercase letters (a, b, c, ...).

E.g., if

   instance Cell Bool where
     toChar True  = '1'
     toChar False = '0'

then

   matrixToStr 2 3 [[true, false, true], [false, true, true]]

produces a string that prints as

     | a | b | c |
   --+---+---+---|
   a | 1 | 0 | 1 |
   --+---+---+---|
   b | 0 | 1 | 1 |
   --+---+---+---|

matrixToStrs :: Cell a => Int -> Int -> Matrix a -> [String]

Like matrixToStr except divides the resulting string into lines (not including trailing newlines).