tetrisType:
Code: Select all
-- Types and piece shapes for Tetris game
-- Tim Lambert May 2005
module TetrisTypes (Direction(..), Position, PieceKind(..), pieceShapes) where
-- move directions
data Direction = LeftMove
| RightMove
| Rotate
| DownMove
deriving (Show,Eq)
-- position on game board
type Position = (Int,Int) -- row, column (0,0) is top left
-- different types of Tetris pieces
data PieceKind = O | S | Z | T | L | F | I
deriving (Show, Enum, Eq)
-- association list giveing piece shapes
pieceShapes :: [(PieceKind,[Position])]
pieceShapes = [(O,[(0,0),(0,1),(1,0),(1,1)]),
(S,[(0,0),(1,0),(2,1),(1,1)]),
(Z,[(2,0),(0,1),(1,0),(1,1)]),
(T,[(1,2),(0,1),(1,0),(1,1)]),
(F,[(0,0),(0,1),(1,0),(2,0)]),
(L,[(0,0),(2,0),(1,0),(2,1)]),
(I,[(2,1),(0,1),(3,1),(1,1)])]
[code]
TetrisPiece:
[code]
-- Pieces for Tetris game Stub file
-- Tim Lambert May 2005
module TetrisPiece (Piece, newPiece, movepiece, cells, kind) where
import TetrisTypes
import Maybe
--type activeCell = [Position]
data Piece = Pc PieceKind --activeCell
deriving (Show,Eq)
--- create a new piece of a particular shape
newPiece :: PieceKind -> Piece
newPiece kind = Pc kind
---move the piece in the direction indicated
movepiece :: Direction -> Piece -> Piece
movepiece dir pc = error "not implemented"
---what cells does the piece cover
cells :: Piece -> [Position]
cells pc = error "not implemented"
---what kindof piece is this?
kind :: Piece -> PieceKind
kind (Pc k) = k
[code]
TEtrisGame :
[code]
-- Stub fo Tetris game board
-- Tim Lambert May 2005
module TetrisGame (Game, newGame, insertPiece, activePiece, move, showBoard) where
import TetrisPiece
import TetrisTypes
-- the game
data Game = Gm Board -- more needed
deriving (Show)
type Board = [String] -- playing field
nRows = 18 -- rows in game
nCols = 10 -- cols on game
-- create new game with empty board
newGame :: Game
newGame = Gm gmboard1
gmboard1 = ["----------"] ++ replicate 18 "|..........|" ++ ["----------"]
-- what is active piece?
activePiece :: Game -> Maybe Piece
activePiece g = error "not implemented"
-- insert a piece into the game
insertPiece :: PieceKind -> Game -> Game
insertPiece pk g = head(pieceShape) --error"f"
-- make a move
move :: Direction -> Game -> Game
move dir gm = error "not implemented"
-- string representation of the game board
showBoard :: Game -> String
showBoard (Gm board) = unlines board
--put a value at a particular position in a list
put :: [a] -> Int -> a -> [a]
put list i v = take i list ++ [v] ++ drop (i+1) list
[code]
Game :
[code]
module Main where
import TetrisGame
import TetrisPiece
import TetrisTypes
import IO
import Maybe
import Random
main :: IO()
main = do
hSetBuffering stdin NoBuffering -- so we get keys pressed immediately
hSetBuffering stdout NoBuffering
putStr "Random Seed ? "
seed <- getLine
let gen = read seed ::StdGen -- random number generator
let pks = map toEnum (randomRs (0,6) gen) -- random list of pieces
tetris (insertPiece (head pks) newGame) (tail pks)
userInterface :: [(Char,Direction)]
userInterface = [('l',RightMove), ('h',LeftMove), ('j',DownMove), ('k',Rotate)]
getDirection :: IO Direction
getDirection = do c <- getChar
let dir = lookup c userInterface
if isNothing dir
then getDirection
else return (fromJust dir)
tetris :: Game -> [PieceKind] -> IO()
tetris game pks =
do putStr (showBoard game)
dir <- getDirection
putStrLn ""
let newg = move dir game
if isNothing (activePiece newg)
then
let inserted = insertPiece (head pks) newg in
if isNothing (activePiece inserted)
then return () -- game over
else tetris inserted (tail pks)
else tetris newg pks
[code]
it consist of 4 module that called tetrisgame, tetristype, game, tetris piece , all i have to do is to implement the function that is commented " not implemented". can any1 give me a solution and a hint how to create. i have try for 2 week straight but i got no solution :grade11math: . Thx for your guys help.
[color=#888888][size=85]Archived topic from Iceteks, old topic ID:3332, old post ID:27005[/size][/color]