Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Programming some game

Name: Anonymous 2007-03-16 11:30 ID:+Cao99ew

Okay.. i'm a noob at C and i'm trying to do some game.
It's a game for 2 players. The first player inputs 4 numbers. The 2nd player tries to guess which numbers those are by inputting numbers too. If there is a correct number in wrong order, 0 will print. If there is a correct number in correct order, 1 will print. Game will end if the 2nd player gets 1111 or uses up all 10 turns.

I can't understand the array stuff much.. help me.

Name: Anonymous 2007-03-16 12:52 ID:gfSEJL7e

    module Main where

    import Control.Monad

    playerQuery :: String -> IO [Int]
    playerQuery msg = do
                putStrLn msg
                d <- liftM (map (\x -> (read [x]) :: Int)) getLine
                when (length d /= 4) (error "We want four nonnegative digits in a single word, fatass.")
                return d

    compareDigits :: [Int] -> [Int] -> [Int]
    compareDigits xs ys =
                  if null ys then []
                     else if xs == ys
                              then [1,1,1,1]
                              else map (\x -> case (elem x ys) of True -> 0; _ -> -1) xs


     gameloop :: [Int] -> [Int] -> Int -> IO ()
     gameloop c t r =
            case r of
                 0 -> do
                         c <-playerQuery "Player 1: Input a four nonnegative digit number to start the game: "
                         gameloop c t 11
                 1  -> do putStrLn "You've lost the game, fatass."
                          error "You have five seconds left to live."
                 _  -> do
                         let guess = compareDigits c t
                             case guess of
                              [] -> do return ()
                              [1,1,1,1] -> do putStrLn "You've won the game! Fatass."
                                              error "Congratulations"
                              _         -> do putStrLn $ "Here's how your guess fared: " ++ show guess
                                              putStrLn $ "You have " ++ show (r - 1) ++ " attempt(s) left."
                         t <- playerQuery "Player 2: Guess a four digit number to continue: "
                         gameloop c t (r - 1)

    main :: IO ()
    main = gameloop [] [] 0

hope that helps!

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List