Skip to content

Commit

Permalink
Added a second F# version which is a bit simpler, and more similar to…
Browse files Browse the repository at this point in the history
… the c# version
  • Loading branch information
jpalmer committed Feb 24, 2011
1 parent d8e2f9b commit ae43ff4
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions fsharp/SimpleRL_2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module SimpleRL
open System
let Map =
[| "#### ####";
"# #### #";
"# #";
"## ##";
" # # ";
" # # ";
"## ##";
"# #";
"# #### #";
"#### ####"
|]

let mutable x,y = 2,2
let DrawTile (tile:char) =
//draw the given tile at the current player location
Console.SetCursorPosition(x,y )
Console.Write( tile )
let MovePlayer newX newY =
//don't move if the new square isn't empty
if( Map.[ newY ].[ newX ] = ' ' ) then
//set the new position
x <- newX;
y <- newY;
let rec Main() =
Console.Clear();
Map |> Array.iter (Console.WriteLine)
DrawTile('@')
let key = Console.ReadKey(true).Key
//clear the square they were standing on before
DrawTile(' ')
//change the player's location if they pressed an arrow key
match key with
|ConsoleKey.UpArrow -> MovePlayer x (y- 1 )
|ConsoleKey.DownArrow -> MovePlayer x (y+ 1 )
|ConsoleKey.LeftArrow -> MovePlayer (x - 1) y
|ConsoleKey.RightArrow -> MovePlayer (x + 1) y
| _-> () //do nothing if key is not matched
//now draw the player at the new square
DrawTile( '@' );
if key <> ConsoleKey.Q then Main() //recurse

Main()

0 comments on commit ae43ff4

Please sign in to comment.