Advertisement
Guest User

Unit 2 exercise complete

a guest
Jun 25th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.52 KB | None | 0 0
  1. module Unit2Test
  2.  
  3. let r = System.Random()
  4.  
  5. let clamp min max value =
  6.     if value < min then min
  7.     elif value > max then max
  8.     else value
  9.  
  10. type Point2D =
  11.     { Position : float * float }
  12.     static member Create(x : float, y : float) = { Position = (x, y) }
  13.     static member CreateRandom(min : float, max : float) =
  14.         Point2D.Create
  15.             (min + (r.NextDouble() * (max - min)),
  16.              min + (r.NextDouble() * (max - min)))
  17.     member this.X = fst this.Position
  18.     member this.Y = snd this.Position
  19.     member this.Distance(Point2D : Point2D) =
  20.         System.Math.Sqrt
  21.             ((((this.X - Point2D.X) ** 2.0) + ((this.Y - Point2D.Y) ** 2.0)))
  22.  
  23. type Blob =
  24.     { Position : Point2D
  25.       Size : int }
  26.  
  27.     static member Create() =
  28.         { Position = (Point2D.CreateRandom(-50.0, 50.0))
  29.           Size = r.Next(1, 6) }
  30.  
  31.     member this.Up =
  32.         { this with Position =
  33.                         Point2D.Create
  34.                             (this.Position.X,
  35.                              clamp -50.0 50.0 (this.Position.Y + 10.0)) }
  36.     member this.Down =
  37.         { this with Position =
  38.                         Point2D.Create
  39.                             (this.Position.X,
  40.                              clamp -50.0 50.0 (this.Position.Y - 10.0)) }
  41.     member this.Left =
  42.         { this with Position =
  43.                         Point2D.Create
  44.                             (clamp -50.0 50.0 (this.Position.X) - 10.0,
  45.                              this.Position.Y) }
  46.     member this.Right =
  47.         { this with Position =
  48.                         Point2D.Create
  49.                             (clamp -50.0 50.0 (this.Position.X) + 10.0,
  50.                              this.Position.Y) }
  51.     member this.Move() =
  52.         let direction = r.Next(0, 4)
  53.         match direction with
  54.         | 0 -> this.Up
  55.         | 1 -> this.Down
  56.         | 2 -> this.Left
  57.         | 3 -> this.Right
  58.         | _ -> this
  59.  
  60. type World =
  61.     { Blob1 : Blob
  62.       Blob2 : Blob
  63.       Tick : int }
  64.  
  65.     static member Create(Blob1 : Blob, Blob2 : Blob, Tick : int) =
  66.         { Blob1 = Blob1
  67.           Blob2 = Blob2
  68.           Tick = Tick }
  69.  
  70.     member this.Run() =
  71.         if this.Tick <= 0 then
  72.             printfn "%A" this
  73.             this
  74.         else
  75.             printfn "%A" this
  76.             { Blob1 = this.Blob1.Move()
  77.               Blob2 = this.Blob2.Move()
  78.               Tick = this.Tick - 1 }
  79.                 .Run()
  80.  
  81. let testUnit2() =
  82.     let w = World.Create(Blob.Create(), Blob.Create(), 1000)
  83.     w.Run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement