Created
October 12, 2014 12:51
-
-
Save jdiez17/461e9307384b9f137966 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Week2 where | |
| import Data.List (transpose) | |
| import Control.Arrow | |
| data Piece = King | Queen | Rook | Bishop | Knight | Pawn | Empty | |
| deriving (Show) | |
| data Board = Board Int Int [[Piece]] | |
| instance Show Board where | |
| show (Board _ _ lst) = show lst | |
| data Vec2 = Vec2 Int Int | |
| deriving (Show) | |
| makeBoard :: Int -> Int -> Board | |
| makeBoard x y = Board x y . replicate y . replicate x $ Empty | |
| inBounds :: Vec2 -> Vec2 -> Bool | |
| inBounds (Vec2 x y) (Vec2 x' y') = | |
| (x > x') && (x' >= 0) && | |
| (y > y') && (y' >= 0) | |
| pieceSees :: Board -> Piece -> (Vec2 -> [Vec2]) | |
| pieceSees (Board sx sy _) p = (filter $ inBounds (Vec2 sx sy)) . pieceFunc p | |
| where | |
| pieceFunc Rook = \(Vec2 x y) -> | |
| [Vec2 x y' | y' <- [0..sy]] ++ | |
| [Vec2 x' y | x' <- [0..sx]] | |
| pieceFunc Knight = \(Vec2 x y) -> | |
| [Vec2 (x + dx) (y + dy) | [dx, dy] <- combos [[1, -1], [2, -2]]] | |
| where | |
| combos xs = sequence xs ++ sequence (reverse xs) | |
| pieceFunc p = \_ -> [] -- Undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment