Created
July 3, 2015 11:22
-
-
Save Arnauld/e4921673c9d375e19717 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Learn more about F# at http://fsharp.net. See the 'F# Tutorial' project | |
// for more guidance on F# programming. | |
#light | |
open Microsoft.FSharp.Reflection | |
let Fail message = failwith message | |
let IsTrue(success) = if not success then failwith "Expected true" | |
let AreEqual(expected, actual) = | |
if not (expected = actual) then | |
sprintf "Expected '%A' Actual '%A'" expected actual |> failwith | |
let Throws<'T when 'T :> exn> (f) = | |
let fail () = failwith "Expected %s" typeof<'T>.Name | |
try f (); fail () with :? 'T as e -> e | _ -> fail() | |
// ------------------------------------------------------------------------------- | |
type Resource = Blue | White | Orange | LightBrown | Green | |
type UrbanizationToken = A | B | C | D | E | F | G | H | I | J | K | L | |
type UrbanizationCard = UrbanizationToken | |
type BuildingColor = Blue | Red | Yellow | |
type BuildingNum = int | |
type BuildingTile = {color:BuildingColor; number:BuildingNum} | |
type BuildingCard = BuildingTile | |
type Card = | |
| UrbanizationCard of UrbanizationCard | |
| BuildingCard of BuildingCard | |
type GreenSpaceTile = unit | |
type Tile = | |
| BuildingTile of BuildingTile | |
| GreenSpaceTile of GreenSpaceTile | |
type CharacterCard = unit | |
// -- | |
// http://www.devx.com/dotnet/Article/40537/0/page/3#sthash.xM1pn761.dpuf | |
let randomNumberGenerator = | |
new System.Random() | |
let shuffle cards = | |
let upperBound = (List.length cards) * 100 | |
let weightedCards = | |
List.map | |
(fun card -> card, randomNumberGenerator.Next(0, upperBound)) | |
cards | |
let sortedWeightedCards = | |
List.sortWith | |
(fun (_, leftWeight) (_, rightWeight) -> leftWeight-rightWeight) | |
weightedCards | |
List.map | |
(fun (card, _) -> card) | |
sortedWeightedCards | |
let initialCity = | |
[for num in 1 .. 3 do | |
for color in [ Blue; Red; Yellow ] -> BuildingCard {color=color; number=num} ] | |
printf "%A" initialCity | |
let shuffled = shuffle initialCity | |
printf "%A" shuffled |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment