Skip to content

Instantly share code, notes, and snippets.

@lefthandedgoat
Created August 29, 2012 01:29
Show Gist options
  • Save lefthandedgoat/3505905 to your computer and use it in GitHub Desktop.
Save lefthandedgoat/3505905 to your computer and use it in GitHub Desktop.
shorter mancala
//test framework
open System
let mutable tests = []
let test f = tests <- List.append tests [f]
let xtest f = ()
let mutable before = fun () -> ()
let run _ = List.map (fun f -> (before ()
f ())) tests
let describe description = Console.WriteLine(description.ToString())
let (==) value1 value2 =
if value1 = value2 then Console.ForegroundColor <- ConsoleColor.Green else Console.ForegroundColor <- ConsoleColor.Red
Console.Write((value1 = value2))
Console.ResetColor()
Console.WriteLine(" expected: {0} got: {1}", value2, value1)
//implementation
let left index (list : int list) = list |> Seq.take index |> Seq.toList
let right index (list : int list) = list |> List.rev |> left index |> List.rev
let gameOver (board : int list)=
match board with
| 0 :: 0 :: 0 :: 0 :: 0 :: 0 :: _ -> true
| _ :: 0 :: 0 :: 0 :: 0 :: 0 :: 0 :: _ :: [] -> true
| _ -> false
let playerOneScore board = board |> left 7 |> List.sum
let playerTwoScore board = board |> right 7 |> List.sum
let sow index (board : int list) player =
let opponentGoal = if player = 1 then 14 else 7
let seedCount = board.[index]
let left = (board |> left index) @ [0]
let right = board |> right (board.Length - 1 - index)
let rec turn (left : int list) (right : int list) count =
match right with
| [] when count > 0 -> turn [(left.Head + 1)] (left.Tail) (count - 1)
| [] when count = 0 -> left
| _ when ((List.length left) + 1) = opponentGoal -> turn (left @ [right.Head]) right.Tail (count)
| _ when count = 0 -> left @ right
| _ -> turn (left @ [(right.Head + 1)]) right.Tail (count - 1)
let result = turn left right seedCount
result
//tests
test(fun _ ->
describe "player one moves well 2"
let board = [4;4;4;4;4;4; 0; 4;4;4;4;4;4; 0]
let index = 1
let results = (sow index board 1)
results == [4;0;5;5;5;5; 0; 4;4;4;4;4;4; 0]
(gameOver results) == false)
test(fun _ ->
describe "player one moves well 3"
let board = [4;4;4;4;4;4; 0; 4;4;4;4;4;4; 0]
let index = 2
let results = (sow index board 1)
results == [4;4;0;5;5;5; 1; 4;4;4;4;4;4; 0]
(gameOver results) == false)
test(fun _ ->
describe "player one moves well 6"
let board = [4;4;4;4;4;8; 0; 4;4;4;4;4;4; 0]
let index = 5
let results = (sow index board 1)
results == [5;4;4;4;4;0; 1; 5;5;5;5;5;5; 0]
(gameOver results) == false)
test(fun _ ->
describe "player two moves well 13"
let board = [4;4;4;4;4;4; 0; 4;4;4;4;4;10; 0]
let index = 12
let results = (sow index board 2)
results == [5;5;5;5;5;5; 0; 5;5;5;4;4;0; 1]
(gameOver results) == false)
test(fun _ ->
describe "game ends when player one runs out of seeds"
let board = [0;0;0;0;0;1; 0; 4;4;4;4;4;10; 0]
let index = 5
let results = (sow index board 1)
results == [0;0;0;0;0;0; 1; 4;4;4;4;4;10; 0]
gameOver results == true
(playerOneScore results) == 1
(playerTwoScore results) == 30)
run()
System.Console.ReadKey()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment