Code Monkey home page Code Monkey logo

hangman-haskell's Introduction

Programming in Haskell - Graham Hutton - Ch10 - Hangman

TABLE OF CONTENTS

Hangman With Haskell

Basics of IO programming using a variant of the game Hangman.

Imports

import System.IO

How to play:

  • One player secretly enter a word.
  • Another player then tries to deduce the word via a series of guesses.
  • For each guess, we indicate which letter in the secret word occur in the guess.
  • The game end when the guess is correct.

The game will be in a top-down manner:

  • Prompts first player to enter the secret word.
  • Then ask the second player to guess it.

Enter secret word

hangman :: IO ()
hangman = do putStrLn "Think of a word:"
             word <- sgetLine
             putStrLn "Try to guess it:"
             play word

sgetLine function

Reads string from keyboard and echos each characters dash symbol โ€œ-โ€œ.

sgetLine = do x <- getCh
              if x == '\n' then
                 do putChar x
                    return []
              else
                 do putChar '-'
                    xs <- sgetLine
                    return (x:xs)

getCh function

Reads single character from the keyboard without echoing it to the screen.

getCh :: IO Char
getCh = do hSetEcho stdin False
           x <- getChar
           hSetEcho stdin True
           return x

play function

Main loop game.

play :: String -> IO ()
play word = do putStr "? "
               guess <- getLine
               if guess == word then
                  putStrLn "You got it!!"
               else
                  do putStrLn (match word guess)
                     play word

match function

Using list comprehension to indicate which letters is the secret word occur anywhere in the guess.

match :: String -> String -> String
match xs ys = [if elem x ys then x else '-' | x <- xs]

hangman-haskell's People

Contributors

firwanaa avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.