rexbelia
rexbelia

Reputation: 159

Dice Game in Haskell

I'm trying to spew out randomly generated dice for every roll that the user plays. The user has 3 rolls per turn and he gets to play 5 turns (I haven't implemented this part yet and I would appreciate suggestions).

I'm also wondering how I can display the colors randomly. I have the list of tuples in place, but I reckon I need some function that uses random and that list to match those colors. I'm struggling as to how.

module Main where

import System.IO
import System.Random
import Data.List

diceColor = [("Black",1),("Green",2),("Purple",3),("Red",4),("White",5),("Yellow",6)]

{-
randomList :: (RandomGen g) -> Int -> g -> [Integer]
random 0 _ = []
randomList n generator = r : randomList (n-1) newGenerator
        where (r, newGenerator) = randomR (1, 6) generator
-}

rand :: Int -> [Int] -> IO ()
rand n rlst = do
    num <- randomRIO (1::Int, 6)
    if n == 0
        then doSomething rlst
        else rand (n-1) (num:rlst)

doSomething x = putStrLn (show (sort x))

main :: IO ()
main = do
    --hSetBuffering stdin LineBuffering
    putStrLn "roll, keep, score?"
    cmd <- getLine
    doYahtzee cmd
    --rand (read cmd) []

doYahtzee :: String -> IO ()
doYahtzee cmd = do
if cmd == "roll" 
    then rand 5 []
        else do print "You won"

Upvotes: 1

Views: 1325

Answers (1)

Daniel Wagner
Daniel Wagner

Reputation: 153152

There's really a lot of errors sprinkled throughout this code, which suggests to me that you tried to build the whole thing at once. This is a recipe for disaster; you should be building very small things and testing them often in ghci.

Lecture aside, you might find the following facts interesting (in order of the associated errors in your code):

  • List is deprecated; you should use Data.List instead.
  • No let is needed for top-level definitions.
  • Variable names must begin with a lower case letter.
  • Class prerequisites are separated from a type by =>.
  • The top-level module block should mainly have definitions; you should associate every where clause (especially the one near randomList) with a definition by either indenting it enough not to be a new line in the module block or keeping it on the same line as the definition you want it to be associated with.
  • do introduces a block; those things in the block should be indented equally and more than their context.
  • doYahtzee is declared and used as if it has three arguments, but seems to be defined as if it only has one.
  • The read function is used to parse a String. Unless you know what it does, using read to parse a String from another String is probably not what you want to do -- especially on user input.
  • putStrLn only takes one argument, not four, and that argument has to be a String. However, making a guess at what you wanted here, you might like the (!!) and print functions.
  • dieRoll doesn't seem to be defined anywhere.

It's possible that there are other errors, as well. Stylistically, I recommend that you check out replicateM, randomRs, and forever. You can use hoogle to search for their names and read more about them; in the future, you can also use it to search for functions you wish existed by their type.

Upvotes: 8

Related Questions