4 people like it.

Langton's ant // OOP :)

Implementation of Langton's ant route.. Takes first 1000 steps and returns only black fields.

 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: 
type Color = 
    Black | White
    with member x.Invert() = match x with | Black -> White | White -> Black
type Direction = 
    Up | Down | Left | Right
    with 
    member x.ToRight() = match x with | Up -> Right | Right -> Down | Down -> Left | Left -> Up
    member x.ToLeft() =  match x with | Up -> Left | Right -> Up | Down -> Right | Left -> Down
type Position =
    { X : int; Y : int }
    with 
    member this.Move(direction) =
        match direction with
        | Up    -> {this with Y=this.Y+1}
        | Right -> {this with X=this.X+1}
        | Down  -> {this with Y=this.Y-1}
        | Left  -> {this with X=this.X-1}
    member this.ToTup() = (this.X,this.Y)
type Field = {
    Pos : Position
    Color : Color
    }
    
type Ant = {
    Pos : Position
    Direction : Direction
    }
    with 
    member x.Move(field:Field) = 
        let newDirection = match field.Color with
                           | White -> x.Direction.ToRight()
                           | Black -> x.Direction.ToLeft()
        let newPosition =  x.Pos.Move(newDirection)
        { Direction = newDirection
          Pos = newPosition }       // return new Ant
  
let makeStep (ant:Ant) (fields:Map<int*int,Field>) =
    let position = ant.Pos.ToTup()
    let currentField = match fields.TryFind(position) with
                       | Some(f) -> f
                       | None    -> { Pos = ant.Pos; Color = White }

    let invertedField = { currentField with Color = currentField.Color.Invert() }
    ant.Move(currentField), invertedField
let rec antRoute ant fields = 
    seq {
        let newAnt, invertedField = makeStep ant fields
        yield invertedField
        yield! antRoute newAnt (fields.Add(ant.Pos.ToTup(), invertedField))
    }

let ant = { Pos = {X=0; Y=0;}; Direction = Up }
antRoute ant Map.empty
    |> Seq.take 1000 
    |> Seq.filter (fun f -> f.Color = Black)
    |> Seq.toList
    |> List.sortBy (fun f -> f.Pos)
    |> List.iter (fun f -> printfn "[%d,%d]-%A" f.Pos.X f.Pos.Y f.Color)
union case Color.Black: Color
union case Color.White: Color
val x : Color
member Color.Invert : unit -> Color

Full name: Script.Color.Invert
type Direction =
  | Up
  | Down
  | Left
  | Right
  member ToLeft : unit -> Direction
  member ToRight : unit -> Direction

Full name: Script.Direction
union case Direction.Up: Direction
union case Direction.Down: Direction
union case Direction.Left: Direction
union case Direction.Right: Direction
val x : Direction
member Direction.ToRight : unit -> Direction

Full name: Script.Direction.ToRight
member Direction.ToLeft : unit -> Direction

Full name: Script.Direction.ToLeft
type Position =
  {X: int;
   Y: int;}
  member Move : direction:Direction -> Position
  member ToTup : unit -> int * int

Full name: Script.Position
Position.X: int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

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

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
Position.Y: int
val this : Position
member Position.Move : direction:Direction -> Position

Full name: Script.Position.Move
val direction : Direction
member Position.ToTup : unit -> int * int

Full name: Script.Position.ToTup
type Field =
  {Pos: Position;
   Color: Color;}

Full name: Script.Field
Field.Pos: Position
Multiple items
Field.Color: Color

--------------------
type Color =
  | Black
  | White
  member Invert : unit -> Color

Full name: Script.Color
type Ant =
  {Pos: Position;
   Direction: Direction;}
  member Move : field:Field -> Ant

Full name: Script.Ant
Ant.Pos: Position
Multiple items
Ant.Direction: Direction

--------------------
type Direction =
  | Up
  | Down
  | Left
  | Right
  member ToLeft : unit -> Direction
  member ToRight : unit -> Direction

Full name: Script.Direction
val x : Ant
member Ant.Move : field:Field -> Ant

Full name: Script.Ant.Move
val field : Field
val newDirection : Direction
Field.Color: Color
Ant.Direction: Direction
member Direction.ToRight : unit -> Direction
member Direction.ToLeft : unit -> Direction
val newPosition : Position
member Position.Move : direction:Direction -> Position
val makeStep : ant:Ant -> fields:Map<(int * int),Field> -> Ant * Field

Full name: Script.makeStep
val ant : Ant
val fields : Map<(int * int),Field>
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 position : int * int
member Position.ToTup : unit -> int * int
val currentField : Field
member Map.TryFind : key:'Key -> 'Value option
union case Option.Some: Value: 'T -> Option<'T>
val f : Field
union case Option.None: Option<'T>
type Color =
  | Black
  | White
  member Invert : unit -> Color

Full name: Script.Color
val invertedField : Field
member Color.Invert : unit -> Color
member Ant.Move : field:Field -> Ant
val antRoute : ant:Ant -> fields:Map<(int * int),Field> -> seq<Field>

Full name: Script.antRoute
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

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

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

Full name: Microsoft.FSharp.Collections.seq<_>
val newAnt : Ant
member Map.Add : key:'Key * value:'Value -> Map<'Key,'Value>
val ant : Ant

Full name: Script.ant
val empty<'Key,'T (requires comparison)> : Map<'Key,'T> (requires comparison)

Full name: Microsoft.FSharp.Collections.Map.empty
module Seq

from Microsoft.FSharp.Collections
val take : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.take
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.filter
val toList : source:seq<'T> -> 'T list

Full name: Microsoft.FSharp.Collections.Seq.toList
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 sortBy : projection:('T -> 'Key) -> list:'T list -> 'T list (requires comparison)

Full name: Microsoft.FSharp.Collections.List.sortBy
val iter : action:('T -> unit) -> list:'T list -> unit

Full name: Microsoft.FSharp.Collections.List.iter
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Raw view Test code New version

More information

Link:http://fssnip.net/b0
Posted:12 years ago
Author:stejcz
Tags: sample , learning f# , wiki , algorithm