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

guessing a-z

Name: Mattew 2007-01-26 2:49

hey, i could use a little help with this code for C++. I have been at it for several hours now and being a fresh new programmer to C++. I was wondering if someone could give me some help or sutocode for this game.

And here is the rand() code that I have:
// ASCII code for lower case a-z.
char randChoice = rand() % 25 + 98;

Write a game that picks a random letter between ‘a’ and ‘z’ (the lowercase alphabet). Read a character (and the newline that must follow it) and tell the user how many letters away they are. Repeat until they guess the right one.

Name: Anonymous 2007-01-26 19:16

>>3

import Control.Monad
import Data.Char
import System.Random

play :: Char -> String -> [String]
play letter guesses =
    map offset $ takeWhile (/= letter) guesses
    where
      offset :: Char -> String
      offset guess
          | guess `elem` ['a'..'z'] =
              case (abs ((ord guess)  - (ord letter))) of
                1 -> "You are off by 1 letter."
                n -> "You are off by " ++ show n ++ " letters."
          | otherwise =
              guess : " is not a letter between a and z."

main :: IO ()
main =
    do letter <- randomRIO ('a','z')
       guesses <- liftM (filter (/= '\n')) getContents
       putStrLn "Guess a letter between a and z"
       mapM_ putStrLn $ play letter guesses
       putStrLn "You Win!"


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