Skip to content

Instantly share code, notes, and snippets.

@lefthandedgoat
Created March 27, 2012 01:17
Show Gist options
  • Save lefthandedgoat/2211454 to your computer and use it in GitHub Desktop.
Save lefthandedgoat/2211454 to your computer and use it in GitHub Desktop.
f# basic mancala
//test framework
let mutable tests = []
let test f = tests <- List.append tests [f]
let run _ = List.map (fun f -> (f ())) tests
let runp _ = List.toArray tests |> Array.Parallel.map (fun f -> (f ()))
let (==) value1 value2 = System.Console.WriteLine("{0} expected: {1} got: {2}", (value1 = value2), value2, value1)
let describe description = System.Console.WriteLine(description.ToString())
//implementation
let rec listLeft (list : int list) index =
match index with
| 0 -> []
| i -> (listLeft list (index - 1)) @ [list.[index - 1]]
let rec listRight (list : int list) index =
match index with
| i when i = (List.length list) - 1 -> []
| i -> [list.[index + 1]] @ (listRight list (index + 1))
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 : int list) = List.sum (listLeft board 7)
let playerTwoScore (board : int list) = List.sum (listRight board 6)
let sow index (board : int list) player =
let opponentGoal =
if player = 1 then
14
else
7
let seedCount = board.[index]
let left = (listLeft board index) @ [0]
let right = listRight board 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 5"
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 4"
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 1"
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 1"
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