0 people like it.

LandOfF

  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: 
124: 
125: 
126: 
127: 
128: 
129: 
130: 
131: 
132: 
133: 
134: 
135: 
136: 
137: 
138: 
139: 
open System

type Location = Room | Garden | Attic
type Direction = North | South | East | West | Up | Down

type Thing = { Name: string; Article: string }
type Edge = { Dir: Direction; Portal: string; Loc: Location }

type Player(initial: Location, objects: ResizeArray<Thing>) =
    let mutable location = initial
    member this.PickUp(obj)  =
        objects.Add(obj)
    member this.Location 
        with get() = location
        and  set v = location <- v

type World() =
    let player = new Player(Room, new ResizeArray<Thing>())
    let mutable objects =
        [Room, [{ Name = "whiskey"; Article = "some" }; { Name = "bucket"; Article = "a" }];
        Garden, [{ Name = "chain"; Article = "a length of" }];
        Attic, []]
        |> Map.ofList
    let locations =
        [Room, "You are in a living room. A wizard is snoring loudly on the couch.";
        Garden, "You are in a beautiful garden. There is a well here.";
        Attic, "You are in an attic. There is a giant welding torch in the corner."]
        |> Map.ofList
    let edges =
        [Room, [{ Dir = West; Portal = "door"; Loc = Garden }; { Dir = Up; Portal = "ladder"; Loc = Attic }];
        Garden, [{ Dir = East; Portal = "door"; Loc = Room }];
        Attic, [{ Dir = Down; Portal = "ladder"; Loc = Room }]]
        |> Map.ofList
    member this.Locations = locations
    member this.Edges = edges
    member this.Player = player
    member this.Objects
        with get() = objects
        and  set v = objects <- v


let asOne (strs : seq<_>) =
    String.Join(" ", strs)
 
let describePath edge =
    (sprintf "%A" edge.Dir).ToLower() |> sprintf "There is a %s going %s from here." edge.Portal
 
let describePaths edges =
    edges |> Seq.map describePath |> asOne


let describeObjects objs =
    let describeObj obj = sprintf "You see here %s %s." obj.Article obj.Name
    objs |> Seq.map describeObj |> asOne

let look (world: World) =
    let player = world.Player
    let loc = world.Locations.[player.Location]
    let paths = world.Edges.[player.Location] |> describePaths
    let objs = world.Objects.[player.Location] |> describeObjects
    [loc; paths; objs] |> asOne

let walk dir (world: World) =
    let player = world.Player
    let attempt = world.Edges.[player.Location]
                  |> List.filter (fun e -> e.Dir = dir)
    match attempt with
    | [] -> "You can't go that way."
    | edge :: _ -> 
        world.Player.Location <- edge.Loc
        sprintf "You go %A..." dir

let pickUp thing (world: World) =
    let player = world.Player
    let objs = world.Objects.[player.Location]
    let attempt = objs |> List.partition (fun o -> o.Name = thing)
    match attempt with
    | [], _ -> "You cannot get that."
    | thing :: [], things ->
        world.Player.PickUp thing
        world.Objects <- world.Objects.Remove(player.Location)
        world.Objects <- world.Objects.Add(player.Location, things)
        sprintf "You are now carrying %s %s." thing.Article thing.Name
    | _ -> "I don't know what you mean."

let getLastWord (phrase: string) =
    let words = phrase.Split(' ')
    words.[words.Length - 1]
 
let parsePickUp cmd world =
    let obj = getLastWord cmd
    pickUp obj world |> printfn "%s"
 
let parseWalk (dir : string) world =
    let direction = 
        match dir with
        | d when d.StartsWith("e") -> Some(East)
        | d when d.StartsWith("n") -> Some(North)
        | d when d.StartsWith("s") -> Some(South)
        | d when d.StartsWith("w") -> Some(West)
        | d when d.StartsWith("u") -> Some(Up)
        | d when d.StartsWith("d") -> Some(Down)
        | _ -> None
    match direction with
    | Some(d) -> walk d world |> printfn "%s"
    | None -> printfn "You can't go that way."
 
let parseMovement cmd world =
    let dir = getLastWord cmd
    parseWalk dir world
 
let rec handleInput world =
    printf "> "
    let input = stdin.ReadLine().Trim().ToLower()
    match input with
    | "quit" | "exit" | "leave" -> 
        printfn "Goodbye!" 
        world, true
    | "look" -> world, false
    | "n" | "s" | "e" | "w" | "u" | "d" | "up" | "down" | "north" | "south" | "east" | "west" ->
        parseWalk input world
        world, false
    | cmd when cmd.StartsWith("go ") || cmd.StartsWith("walk") || cmd.StartsWith("climb")  ->
        parseMovement cmd world
        world, false
    | cmd when cmd.StartsWith("take") || cmd.StartsWith("get") || cmd.StartsWith("pick")  ->
        parsePickUp cmd world
        world, false
    | other ->
        printfn "You can't %s here." other
        handleInput world
 
let rec gameLoop world =
    look world |> printfn "%s"
    match handleInput world with
    | _, true -> ()
    | w, _ -> gameLoop w
 
World() |> gameLoop
namespace System
type Location =
  | Room
  | Garden
  | Attic

Full name: Script.Location
union case Location.Room: Location
union case Location.Garden: Location
union case Location.Attic: Location
type Direction =
  | North
  | South
  | East
  | West
  | Up
  | Down

Full name: Script.Direction
union case Direction.North: Direction
union case Direction.South: Direction
union case Direction.East: Direction
union case Direction.West: Direction
union case Direction.Up: Direction
union case Direction.Down: Direction
type Thing =
  {Name: string;
   Article: string;}

Full name: Script.Thing
Thing.Name: string
Multiple items
val string : value:'T -> string

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

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

Full name: Microsoft.FSharp.Core.string
Thing.Article: string
type Edge =
  {Dir: Direction;
   Portal: string;
   Loc: Location;}

Full name: Script.Edge
Edge.Dir: Direction
Edge.Portal: string
Edge.Loc: Location
Multiple items
type Player =
  new : initial:Location * objects:ResizeArray<Thing> -> Player
  member PickUp : obj:Thing -> unit
  member Location : Location
  member Location : Location with set

Full name: Script.Player

--------------------
new : initial:Location * objects:ResizeArray<Thing> -> Player
val initial : Location
val objects : ResizeArray<Thing>
type ResizeArray<'T> = Collections.Generic.List<'T>

Full name: Microsoft.FSharp.Collections.ResizeArray<_>
val mutable location : Location
val this : Player
member Player.PickUp : obj:Thing -> unit

Full name: Script.Player.PickUp
Multiple items
val obj : Thing

--------------------
type obj = Object

Full name: Microsoft.FSharp.Core.obj
Collections.Generic.List.Add(item: Thing) : unit
Multiple items
member Player.Location : Location with set

Full name: Script.Player.Location

--------------------
type Location =
  | Room
  | Garden
  | Attic

Full name: Script.Location
val set : elements:seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set
val v : Location
Multiple items
type World =
  new : unit -> World
  member Edges : Map<Location,Edge list>
  member Locations : Map<Location,string>
  member Objects : Map<Location,Thing list>
  member Player : Player
  member Objects : Map<Location,Thing list> with set

Full name: Script.World

--------------------
new : unit -> World
val player : Player
val mutable objects : Map<Location,Thing list>
Multiple items
module Map

from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> =
  interface IEnumerable
  interface IComparable
  interface IEnumerable<KeyValuePair<'Key,'Value>>
  interface ICollection<KeyValuePair<'Key,'Value>>
  interface IDictionary<'Key,'Value>
  new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
  member Add : key:'Key * value:'Value -> Map<'Key,'Value>
  member ContainsKey : key:'Key -> bool
  override Equals : obj -> bool
  member Remove : key:'Key -> Map<'Key,'Value>
  ...

Full name: Microsoft.FSharp.Collections.Map<_,_>

--------------------
new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
val ofList : elements:('Key * 'T) list -> Map<'Key,'T> (requires comparison)

Full name: Microsoft.FSharp.Collections.Map.ofList
val locations : Map<Location,string>
val edges : Map<Location,Edge list>
val this : World
member World.Locations : Map<Location,string>

Full name: Script.World.Locations
member World.Edges : Map<Location,Edge list>

Full name: Script.World.Edges
Multiple items
member World.Player : Player

Full name: Script.World.Player

--------------------
type Player =
  new : initial:Location * objects:ResizeArray<Thing> -> Player
  member PickUp : obj:Thing -> unit
  member Location : Location
  member Location : Location with set

Full name: Script.Player

--------------------
new : initial:Location * objects:ResizeArray<Thing> -> Player
member World.Objects : Map<Location,Thing list> with set

Full name: Script.World.Objects
val v : Map<Location,Thing list>
val asOne : strs:seq<string> -> string

Full name: Script.asOne
val strs : seq<string>
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
String.Join(separator: string, values: Collections.Generic.IEnumerable<string>) : string
String.Join<'T>(separator: string, values: Collections.Generic.IEnumerable<'T>) : string
String.Join(separator: string, [<ParamArray>] values: obj []) : string
String.Join(separator: string, [<ParamArray>] value: string []) : string
String.Join(separator: string, value: string [], startIndex: int, count: int) : string
val describePath : edge:Edge -> string

Full name: Script.describePath
val edge : Edge
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val describePaths : edges:seq<Edge> -> string

Full name: Script.describePaths
val edges : seq<Edge>
module Seq

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val describeObjects : objs:seq<Thing> -> string

Full name: Script.describeObjects
val objs : seq<Thing>
val describeObj : (Thing -> string)
val look : world:World -> string

Full name: Script.look
val world : World
property World.Player: Player
val loc : string
property World.Locations: Map<Location,string>
property Player.Location: Location
val paths : string
property World.Edges: Map<Location,Edge list>
val objs : string
property World.Objects: Map<Location,Thing list>
val walk : dir:Direction -> world:World -> string

Full name: Script.walk
val dir : Direction
val attempt : Edge list
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val filter : predicate:('T -> bool) -> list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.filter
val e : Edge
val pickUp : thing:string -> world:World -> string

Full name: Script.pickUp
val thing : string
val objs : Thing list
val attempt : Thing list * Thing list
val partition : predicate:('T -> bool) -> list:'T list -> 'T list * 'T list

Full name: Microsoft.FSharp.Collections.List.partition
val o : Thing
val thing : Thing
val things : Thing list
member Player.PickUp : obj:Thing -> unit
member Map.Remove : key:'Key -> Map<'Key,'Value>
member Map.Add : key:'Key * value:'Value -> Map<'Key,'Value>
val getLastWord : phrase:string -> string

Full name: Script.getLastWord
val phrase : string
val words : string []
String.Split([<ParamArray>] separator: char []) : string []
String.Split(separator: string [], options: StringSplitOptions) : string []
String.Split(separator: char [], options: StringSplitOptions) : string []
String.Split(separator: char [], count: int) : string []
String.Split(separator: string [], count: int, options: StringSplitOptions) : string []
String.Split(separator: char [], count: int, options: StringSplitOptions) : string []
property Array.Length: int
val parsePickUp : cmd:string -> world:World -> unit

Full name: Script.parsePickUp
val cmd : string
Multiple items
val obj : string

--------------------
type obj = Object

Full name: Microsoft.FSharp.Core.obj
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val parseWalk : dir:string -> world:World -> unit

Full name: Script.parseWalk
val dir : string
val direction : Direction option
val d : string
String.StartsWith(value: string) : bool
String.StartsWith(value: string, comparisonType: StringComparison) : bool
String.StartsWith(value: string, ignoreCase: bool, culture: Globalization.CultureInfo) : bool
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
val d : Direction
val parseMovement : cmd:string -> world:World -> unit

Full name: Script.parseMovement
val handleInput : world:'a -> 'a * bool (requires 'a :> World)

Full name: Script.handleInput
val world : #World
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
val input : string
val stdin<'T> : IO.TextReader

Full name: Microsoft.FSharp.Core.Operators.stdin
val other : string
val gameLoop : world:World -> unit

Full name: Script.gameLoop
val w : World
Raw view Test code New version

More information

Link:http://fssnip.net/9n
Posted:14 years ago
Author:
Tags: