Last active
August 29, 2015 14:11
-
-
Save bjartwolf/a7f8d852e3e4341a0f0e to your computer and use it in GitHub Desktop.
antlong
This file contains hidden or 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
module ant | |
// The position is a x-y coordinate | |
type Pos = int*int | |
// The set of all points on the board which are black | |
type Blacks = Set<Pos> | |
type Dir = Right | Up | Left | Down | |
// This is the entire game state in one turn | |
type State = Pos * Dir * Blacks | |
// Returns the new position after moving in a given direction | |
let moveInDirection (pos: Pos) (dir: Dir) = | |
let x,y = pos | |
match dir with | |
| Right -> (x+1, y) | |
| Up -> (x,y+1) | |
| Left -> (x-1, y) | |
| Down -> (x,y-1) | |
let turnLeft (dir:Dir) = | |
match dir with | |
| Right -> Up | |
| Up -> Left | |
| Left -> Down | |
| Down -> Right | |
let turnRight(dir:Dir) = | |
match dir with | |
| Right -> Down | |
| Up -> Right | |
| Left -> Up | |
| Down -> Left | |
// Takes a game state and returns the next state after moving/rotating and flipping color | |
// of the new position | |
let step (state : State) : State = | |
let pos, dir, blacks = state | |
if blacks.Contains(pos) then | |
let newdir = turnRight dir | |
(moveInDirection pos newdir, newdir, blacks.Remove(pos)) | |
else | |
let newdir = turnLeft dir | |
(moveInDirection pos newdir, newdir, blacks.Add(pos)) | |
// Iterates turns times through the game | |
let rec play (state: State) (turns: int) : State = | |
if turns = 0 then state | |
else play (step state) (turns - 1) | |
[<EntryPoint>] | |
let main argv = | |
printfn "%A" (play ((0,0),Right, Set.empty) 15001) | |
System.Console.ReadLine() |> ignore | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment