Game of Life in Silverlight and F#

I’m too tired to write a proper blog post about this but I managed to port DonSyme’s Game of Life Demo that comes with the F# Distribution to Silverlight. It’s not a full port as there were some UI events that I couldn’t get to work for love nor money – I think these maybe bugs in Silverlight itself. However the game does look pretty cool. Also Silverlight currently has no way to marshal back to the UI thread which makes multithreading a bit difficult.

The game was relatively simple to port, in fact I kept the first few files, “alg.fsi”, “alg.fs” and “worker.fs” in tacked and just replaced the client.fs with the following:

#light

 

namespace SilverLife

 

open System;

open System.Collections.Generic;

open System.Windows.Browser;

open System.Threading;

open System.Windows

open System.Windows.Controls

open System.Windows.Documents

open System.Windows.Ink

open System.Windows.Input

open System.Windows.Media

open System.Windows.Media.Animation

open System.Windows.Shapes

 

type Page() = class

    inherit Canvas() as base

    member x.Page_Loaded(o : obj,  e : EventArgs) =

        let canvas = o :?> Canvas

        let gridSize = 23

       

        /// Create and configure the worker automata, ready to be placed

        /// onto a thread.

        let rec worker = Worker.mkWorker gridSize

       

        and grid =

            [| for x in 0 .. gridSize ->

                [| for y in 0 .. gridSize ->

                    let image = new Image()

                    image.Source <- new Uri("RedBloodcellOff.jpg", UriKind.Relative)

                    image.SetValue(Canvas.TopProperty, y * 14)

                    image.SetValue(Canvas.LeftProperty, x * 14)

                    canvas.Children.Add(image)

                    image |] |]

       

        and bornQueue = new Queue<Game.points>() 

        and diedQueue = new Queue<Game.points>() 

 

        // This is the refresh operation that will be invoked by the worker.

        and workerNotifyUpdates (born,died) =

            lock bornQueue (fun () -> bornQueue.Enqueue(born))

            lock diedQueue (fun () -> diedQueue.Enqueue(died))

            

            

 

        and workerThread = new Thread(start=(fun () -> worker.Start()), IsBackground = true, Priority = ThreadPriority.Lowest)

         

        worker.Updates.Add(workerNotifyUpdates)

        let dequeueAll (queue : Queue<Game.points>) image =

            while (queue.Count > 0) do

                let points = queue.Dequeue()

                List.iter (fun (x,y) -> grid.[x].[y].Source <- new Uri(image, UriKind.Relative) ) points

               

        let timer_Tick _ =

            lock bornQueue (fun () -> dequeueAll bornQueue "RedBloodcellOn.jpg")

            lock diedQueue (fun () -> dequeueAll diedQueue "RedBloodcellOff.jpg")

           

        let timer = new HtmlTimer()

        timer.Interval <- 1

        timer.Tick.Add(timer_Tick)

        timer.Enabled <- true

 

        workerThread.Start()

        ()

    member x.ClickSquare(sender : obj,  e : MouseEventArgs) = ()

end

You can see the game at the following URL (careful I’ve noticed it crashes the browser sometimes, I guess that’s what they mean by “alpha”):

http://www.strangelights.com/fsharp/silverlight/life.aspx

Of course you need the silverlight 1.1 alpha installed here’s a screen shot for those that don’t have that.

Bookmark
dotnetkicks+, digg+, reddit+, del.icio.us+, dzone+, facebook+

Print | posted @ Friday, May 25, 2007 5:27 PM

Comments on this entry:

Gravatar # Silverlight Cream for May 25
by Pingback/TrackBack at 5/26/2007 6:54 AM

Silverlight Cream for May 25
Gravatar # Game of Life in Silverlight and F#
by Pingback/TrackBack at 5/26/2007 6:55 AM

Game of Life in Silverlight and F#
Gravatar # Game of Life in Silverlight and F#
by Pingback/TrackBack at 5/27/2007 5:15 PM

Game of Life in Silverlight and F#
Gravatar # re: Game of Life in Silverlight and F#
by Richard Hein at 5/28/2007 6:31 AM

Great stuff, I wanted to put F# and Silverlight together as well. I am looking into how to best combine this with Monorail. Cheers, Richard Hein
Gravatar # re: Game of Life in Silverlight and F#
by Robert Pickering at 5/28/2007 7:55 AM

Hi Richard,

Thanks for the feedback. You maybe insterested in this more detailed post I've just written:
http://www.strangelights.com/blog/archive/2007/05/28/1585.aspx

I've not actually had chance to play with the monorail stuff but I'm looking forward to see what you come up with.

Cheers,
Rob
Gravatar # Silverlight Examples
by Michael's Blog at 6/7/2007 2:10 PM

During the weekend I spent some minutes to collect some of the greatest Silverlight examples. Most of
Gravatar # Non-Stop Mix 07 Paris
by Richard J. Rothery Jr. at 6/8/2007 1:35 PM

Robert,

Nice one F# and Silverlight!
Your are an Englishman a Paris...
I am and Amercican a Marseille...

See you at Mix 07 in Paris if you are planning to attend.

Best Regards,
Richard
Gravatar # 【转】分享数十个silverlight示例和blog
by Bryan Chow at 7/19/2007 2:30 PM

分享几个国外的silverlight例子和blogGreatVisualStudioQuickStarts
Gravatar # re: Game of Life in Silverlight and F#
by Sridhar Ratnakumar at 6/23/2008 9:29 AM

I tried my best to compile, but even though I have Silverlight latest version installed I get this error -


C:\Users\srid>fsc -I "C:\Program Files\Microsoft Silverlight\2.0.30523.6" -r System.SilverLight.dll -r agclr.dll -r Microsoft.Scripting.Vestigial.dll -r System.
dll -r Microsoft.Scripting.Silverlight.dll -r Microsoft.Scripting.dll --clr-root "C:\Program Files\Microsoft Silverlight" --no-framework --no-mllib --static-lin
k fslib test.fs
Implicitly referencing 'C:\Program Files\Microsoft Silverlight\2.0.30523.6\System.dll'...

startup(1,0): error FS0191: Unable to find the file System.SilverLight.dll in any of
C:\Program Files\Microsoft Silverlight
C:\Program Files\Microsoft Silverlight\2.0.30523.6
C:\Users\srid
C:\Program Files\FSharp-1.9.4.17\bin.
Gravatar # re: Game of Life in Silverlight and F#
by Ray at 7/19/2009 10:03 AM

Pretty interesting. Though, there is little to no use of F# in this example as code pretty much imperative - about just same as if it was wrote in C#...
Gravatar # re: Game of Life in Silverlight and F#
by Robert Pickering at 7/19/2009 11:09 AM

Hi Ray,

I agree the layer that interacts with silverlight is pretty imperative, mainly because its a UI layer, which is the sample code presented in this blog. However layers that actually drive the simulation, which come from the F# example pack are good functional style. These were distributed with F# but now need to be downloaded from:
http://code.msdn.microsoft.com/fsharpsamples

Cheers,
Rob

Your comment:

(Note: all comments are moderated so it may take sometime to appear)

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 8 and 1 and type the answer here: