Page 1 of 1

Haskell HElp !!! Tetriss

Posted: Thu May 19, 2005 10:06 am
by Anonymous
hey iam new in here, iam currently making tetris for my assignment in uni using haskell. here is the program code:


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]

Haskell HElp !!! Tetriss

Posted: Thu May 19, 2005 11:48 am
by wtd
There really would seem to be three functions you have to worry about.

Code: Select all

-- 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"[code]

Unfortunately none of the types involved, as far as I can see, actually keeps track of the last piece added to the board, so it would be impossible, as far as I can see, to determine the "activePiece" based on that information.

The game's board hould be, in my estimation, be purely a list of strings, but rather a grid of boolean values.  True and the space is filled.  False and it's open.  The active piece should be kept track of as a piece, not just data about it's location. 

[color=#888888][size=85]Archived topic from Iceteks,  old topic ID:3332, old post ID:27006[/size][/color]

Haskell HElp !!! Tetriss

Posted: Fri May 20, 2005 2:08 am
by Anonymous
Thx the Tetris Game is basically look like this

For a new game :
------------
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
------------

then another board with a piece
Insert T
------------
|.T........|
|TTT.......|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
|..........|
------------

SO everytime a piece move it actually creaate a new board that replace it
and the piece also cannot move down byitself, for more detail requirement here is the website
www.cse.unsw.edu.au/~cs1011
thx for ur help :)

Archived topic from Iceteks, old topic ID:3332, old post ID:27027

Haskell HElp !!! Tetriss

Posted: Fri May 20, 2005 2:12 am
by Anonymous
umm sorry the website is not detail
is in the left hand side where under work and in the link assign2
thx

Archived topic from Iceteks, old topic ID:3332, old post ID:27028

Haskell HElp !!! Tetriss

Posted: Sat May 21, 2005 12:41 pm
by wtd
The drawing a new board each time thing is standard behavior for a text-based console. There are graphics modules for Haskell, but you're best off wrapping your mind around basic concepts first.

Archived topic from Iceteks, old topic ID:3332, old post ID:27057