|
| 1 | +module Tetris.Piece |
| 2 | + ( Piece(..) |
| 3 | + , initial |
| 4 | + , color |
| 5 | + ) where |
| 6 | + |
| 7 | +import System.Random |
| 8 | +import Data.Matrix |
| 9 | +import Tetris.Color |
| 10 | + |
| 11 | +data Piece = I | J | L | O | S | T | Z | Empty |
| 12 | + deriving (Show, Eq) -- TODO: Delete Show |
| 13 | + |
| 14 | +color :: Piece -> Color |
| 15 | +color x = case x of |
| 16 | + I -> Red |
| 17 | + J -> Blue |
| 18 | + L -> Orange |
| 19 | + O -> Yellow |
| 20 | + S -> Magenta |
| 21 | + T -> Cyan |
| 22 | + Z -> Green |
| 23 | + Empty -> Black |
| 24 | + |
| 25 | + |
| 26 | +initial :: Piece -> (Matrix Color, [(Int, Int)]) |
| 27 | +initial x = case x of |
| 28 | + I -> (fromLists [[Black,Black,Black,Black], |
| 29 | + [Red ,Red ,Red ,Red ], |
| 30 | + [Black,Black,Black,Black], |
| 31 | + [Black,Black,Black,Black]], |
| 32 | + [(2,1), (2,2), (2,3), (2,4)]) |
| 33 | + J -> (fromLists [[Blue,Black,Black], |
| 34 | + [Blue,Blue,Blue], |
| 35 | + [Black,Black,Black]], |
| 36 | + [(1,1),(2,1),(2,2),(2,3)]) |
| 37 | + L -> (fromLists [[Black,Black,Orange], |
| 38 | + [Orange,Orange,Orange], |
| 39 | + [Black,Black,Black]], |
| 40 | + [(1,3),(2,1),(2,2),(2,3)]) |
| 41 | + O -> (fromLists [[Black,Yellow,Yellow,Black], |
| 42 | + [Black,Yellow,Yellow,Black], |
| 43 | + [Black,Black,Black,Black]], |
| 44 | + [(1,2),(1,3),(2,2),(3,2)]) |
| 45 | + S -> (fromLists [[Black,Magenta,Magenta], |
| 46 | + [Magenta,Magenta,Black], |
| 47 | + [Black,Black,Black]], |
| 48 | + [(1,2),(1,3),(2,1),(2,2)]) |
| 49 | + T -> (fromLists [[Black,Cyan,Black], |
| 50 | + [Cyan,Cyan,Cyan], |
| 51 | + [Black,Black,Black]], |
| 52 | + [(1,2),(2,1),(2,2),(2,3)]) |
| 53 | + Z -> (fromLists [[Green,Green,Black], |
| 54 | + [Black,Green,Green], |
| 55 | + [Black,Black,Black]], |
| 56 | + [(1,1),(1,2),(2,2),(2,3)]) |
| 57 | + Empty -> (fromLists [[Black]], [(-1,-1)]) |
| 58 | + |
| 59 | +instance Random Piece where |
| 60 | + randomR (lo,hi) g = -- TODO: randomR completely broken |
| 61 | + case (fst rnd) `rem` 7 of |
| 62 | + 0 -> (I, snd rnd) |
| 63 | + 1 -> (J, snd rnd) |
| 64 | + 2 -> (L, snd rnd) |
| 65 | + 3 -> (O, snd rnd) |
| 66 | + 4 -> (S, snd rnd) |
| 67 | + 5 -> (T, snd rnd) |
| 68 | + 6 -> (Z, snd rnd) |
| 69 | + where rnd = next g |
| 70 | + |
| 71 | + random = randomR (I,Z) |
0 commit comments