3 people like it.
Like the snippet!
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
namespace System
val words : string list
val toPartialWord : word:string -> used:seq<char> -> string
val word : string
Multiple items
val string : value:'T -> string
--------------------
type string = String
val used : seq<char>
Multiple items
val char : value:'T -> char (requires member op_Explicit)
--------------------
[<Struct>]
type char = Char
Multiple items
val seq : sequence:seq<'T> -> seq<'T>
--------------------
type seq<'T> = Collections.Generic.IEnumerable<'T>
Multiple items
type String =
interface IComparable
interface IEnumerable
interface IConvertible
interface IEnumerable<char>
interface IComparable<string>
interface IEquatable<string>
interface ICloneable
new : value: char [] -> unit + 8 overloads
member Clone : unit -> obj
member CompareTo : value: obj -> int + 1 overload
...
--------------------
String(value: char []) : String
String(value: nativeptr<char>) : String
String(value: nativeptr<sbyte>) : String
String(value: ReadOnlySpan<char>) : String
String(c: char, count: int) : String
String(value: char [], startIndex: int, length: int) : String
String(value: nativeptr<char>, startIndex: int, length: int) : String
String(value: nativeptr<sbyte>, startIndex: int, length: int) : String
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : String
val map : mapping:(char -> char) -> str:string -> string
val c : char
module Seq
from Microsoft.FSharp.Collections
val exists : predicate:('T -> bool) -> source:seq<'T> -> bool
val isGuessValid : used:seq<char> -> guess:char -> bool
val guess : char
val not : value:bool -> bool
val readGuess : used:seq<char> -> char
type Console =
static member Beep : unit -> unit + 1 overload
static member CheckNonNull : obj: obj * paramName: string -> unit
static member Clear : unit -> unit
static member CreateOutputWriter : outputStream: Stream -> TextWriter
static member EnsureInitialized<'T (requires reference type)> : field: byref<'T> * initializer: Func<'T> -> 'T
static member HandleBreakEvent : controlKey: ConsoleSpecialKey -> bool
static member MoveBufferArea : sourceLeft: int * sourceTop: int * sourceWidth: int * sourceHeight: int * targetLeft: int * targetTop: int -> unit + 1 overload
static member OpenStandardError : unit -> Stream + 1 overload
static member OpenStandardInput : unit -> Stream + 1 overload
static member OpenStandardOutput : unit -> Stream + 1 overload
...
Console.ReadKey() : ConsoleKeyInfo
Console.ReadKey(intercept: bool) : ConsoleKeyInfo
[<Struct>]
type Char =
member CompareTo : value: obj -> int + 1 overload
member Equals : obj: obj -> bool + 1 overload
member GetHashCode : unit -> int
member GetTypeCode : unit -> TypeCode
member System.IConvertible.ToBoolean : provider: IFormatProvider -> bool
member System.IConvertible.ToByte : provider: IFormatProvider -> byte
member System.IConvertible.ToChar : provider: IFormatProvider -> char
member System.IConvertible.ToDateTime : provider: IFormatProvider -> DateTime
member System.IConvertible.ToDecimal : provider: IFormatProvider -> decimal
member System.IConvertible.ToDouble : provider: IFormatProvider -> float
...
Char.ToUpper(c: char) : char
Char.ToUpper(c: char, culture: Globalization.CultureInfo) : char
val getGuess : used:seq<char> -> char
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
val used : char list
val tally : int
val word' : string
property List.Length: int with get
val exists : predicate:(char -> bool) -> str:string -> bool
Multiple items
type Random =
new : unit -> unit + 1 overload
member GetSampleForLargeRange : unit -> float
member InternalSample : unit -> int
member Next : unit -> int + 2 overloads
member NextBytes : buffer: byte [] -> unit + 1 overload
member NextDouble : unit -> float
member Sample : unit -> float
static member GenerateGlobalSeed : unit -> int
static member GenerateSeed : unit -> int
static val s_globalRandom : Random
...
--------------------
Random() : Random
Random(Seed: int) : Random
More information