6 people like it.

Hangman

Word guessing game using ASCII art.

  1: 
  2: 
  3: 
  4: 
  5: 
  6: 
  7: 
  8: 
  9: 
 10: 
 11: 
 12: 
 13: 
 14: 
 15: 
 16: 
 17: 
 18: 
 19: 
 20: 
 21: 
 22: 
 23: 
 24: 
 25: 
 26: 
 27: 
 28: 
 29: 
 30: 
 31: 
 32: 
 33: 
 34: 
 35: 
 36: 
 37: 
 38: 
 39: 
 40: 
 41: 
 42: 
 43: 
 44: 
 45: 
 46: 
 47: 
 48: 
 49: 
 50: 
 51: 
 52: 
 53: 
 54: 
 55: 
 56: 
 57: 
 58: 
 59: 
 60: 
 61: 
 62: 
 63: 
 64: 
 65: 
 66: 
 67: 
 68: 
 69: 
 70: 
 71: 
 72: 
 73: 
 74: 
 75: 
 76: 
 77: 
 78: 
 79: 
 80: 
 81: 
 82: 
 83: 
 84: 
 85: 
 86: 
 87: 
 88: 
 89: 
 90: 
 91: 
 92: 
 93: 
 94: 
 95: 
 96: 
 97: 
 98: 
 99: 
100: 
101: 
102: 
103: 
104: 
105: 
106: 
107: 
108: 
109: 
110: 
111: 
112: 
113: 
114: 
115: 
116: 
117: 
118: 
119: 
120: 
121: 
122: 
123: 
let hangman = [ """ 
____
|/   |
|   
|    
|    
|    
|
|_____
""";
"""
 ____
|/   |
|   (_)
|    
|    
|    
|
|_____
""";
"""
 ____
|/   |
|   (_)
|    |
|    |    
|    
|
|_____
""";
"""
 ____
|/   |
|   (_)
|   \|
|    |
|    
|
|_____
""";
"""
 ____
|/   |
|   (_)
|   \|/
|    |
|    
|
|_____
""";
"""
 ____
|/   |
|   (_)
|   \|/
|    |
|   / 
|
|_____
""";
"""
 ____
|/   |
|   (_)
|   \|/
|    |
|   / \
|
|_____
""";
"""
 ____
|/   |
|   (_)
|   /|\
|    |
|   | |
|
|_____
"""]


open System

let words = ["PENCIL";"CHALK";"CRAYON";"BRUSH"]

let toPartialWord (word:string) (used:char seq) =
   word |> String.map (fun c -> 
      if Seq.exists ((=) c) used then c else '_'
   )

let isGuessValid (used:char seq) (guess:char) =
   Seq.exists ((=) guess) ['A'..'Z'] &&
   not (used |> Seq.exists ((=) guess))

let rec readGuess used =
   let guess = Console.ReadKey(true).KeyChar |> Char.ToUpper
   if isGuessValid used guess then guess
   else readGuess used

let getGuess used =
   Console.Write("Guess: ")
   let guess = readGuess used
   Console.WriteLine(guess)
   guess

let rec play word used tally =
   Console.Write(hangman.[tally])
   let word' = toPartialWord word used
   Console.WriteLine(word')
   if word = word' then 
      Console.WriteLine("CORRECT")
   elif tally = hangman.Length-1 then 
      Console.WriteLine("HANGMAN")
   else
      let guess = getGuess used
      let used = guess::used
      if word |> String.exists ((=) guess)
      then play word used tally
      else play word used (tally+1)

let word = words.[Random().Next(words.Length)]
do play word [] 0
val hangman : string list

Full name: Script.hangman
namespace System
val words : string list

Full name: Script.words
val toPartialWord : word:string -> used:seq<char> -> string

Full name: Script.toPartialWord
val word : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
val used : seq<char>
Multiple items
val char : value:'T -> char (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.char

--------------------
type char = Char

Full name: Microsoft.FSharp.Core.char
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
String(value: nativeptr<char>) : unit
String(value: nativeptr<sbyte>) : unit
String(value: char []) : unit
String(c: char, count: int) : unit
String(value: nativeptr<char>, startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
String(value: char [], startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : unit
val map : mapping:(char -> char) -> str:string -> string

Full name: Microsoft.FSharp.Core.String.map
val c : char
module Seq

from Microsoft.FSharp.Collections
val exists : predicate:('T -> bool) -> source:seq<'T> -> bool

Full name: Microsoft.FSharp.Collections.Seq.exists
val isGuessValid : used:seq<char> -> guess:char -> bool

Full name: Script.isGuessValid
val guess : char
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
val readGuess : used:seq<char> -> char

Full name: Script.readGuess
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
Console.ReadKey() : ConsoleKeyInfo
Console.ReadKey(intercept: bool) : ConsoleKeyInfo
type Char =
  struct
    member CompareTo : value:obj -> int + 1 overload
    member Equals : obj:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member GetTypeCode : unit -> TypeCode
    member ToString : unit -> string + 1 overload
    static val MaxValue : char
    static val MinValue : char
    static member ConvertFromUtf32 : utf32:int -> string
    static member ConvertToUtf32 : highSurrogate:char * lowSurrogate:char -> int + 1 overload
    static member GetNumericValue : c:char -> float + 1 overload
    ...
  end

Full name: System.Char
Char.ToUpper(c: char) : char
Char.ToUpper(c: char, culture: Globalization.CultureInfo) : char
val getGuess : used:seq<char> -> char

Full name: Script.getGuess
Console.Write(value: string) : unit
   (+0 other overloads)
Console.Write(value: obj) : unit
   (+0 other overloads)
Console.Write(value: uint64) : unit
   (+0 other overloads)
Console.Write(value: int64) : unit
   (+0 other overloads)
Console.Write(value: uint32) : unit
   (+0 other overloads)
Console.Write(value: int) : unit
   (+0 other overloads)
Console.Write(value: float32) : unit
   (+0 other overloads)
Console.Write(value: decimal) : unit
   (+0 other overloads)
Console.Write(value: float) : unit
   (+0 other overloads)
Console.Write(buffer: char []) : unit
   (+0 other overloads)
Console.WriteLine() : unit
   (+0 other overloads)
Console.WriteLine(value: string) : unit
   (+0 other overloads)
Console.WriteLine(value: obj) : unit
   (+0 other overloads)
Console.WriteLine(value: uint64) : unit
   (+0 other overloads)
Console.WriteLine(value: int64) : unit
   (+0 other overloads)
Console.WriteLine(value: uint32) : unit
   (+0 other overloads)
Console.WriteLine(value: int) : unit
   (+0 other overloads)
Console.WriteLine(value: float32) : unit
   (+0 other overloads)
Console.WriteLine(value: float) : unit
   (+0 other overloads)
Console.WriteLine(value: decimal) : unit
   (+0 other overloads)
val play : word:string -> used:char list -> tally:int -> unit

Full name: Script.play
val used : char list
val tally : int
val word' : string
property List.Length: int
val exists : predicate:(char -> bool) -> str:string -> bool

Full name: Microsoft.FSharp.Core.String.exists
val word : string

Full name: Script.word
Multiple items
type Random =
  new : unit -> Random + 1 overload
  member Next : unit -> int + 2 overloads
  member NextBytes : buffer:byte[] -> unit
  member NextDouble : unit -> float

Full name: System.Random

--------------------
Random() : unit
Random(Seed: int) : unit
Raw view Test code New version

More information

Link:http://fssnip.net/mO
Posted:10 years ago
Author:Phillip Trelford
Tags: game