Note that there are some explanatory texts on larger screens.

plurals
  1. POVery simple RogueLike in F#, making it more "functional"
    primarykey
    data
    text
    <p>I have some existing C# code for a very, very simple RogueLike engine. It is deliberately naive in that I was trying to do the minimum amount as simply as possible. All it does is move an @ symbol around a hardcoded map using the arrow keys and System.Console:</p> <pre><code>//define the map var map = new List&lt;string&gt;{ " ", " ", " ", " ", " ############################### ", " # # ", " # ###### # ", " # # # # ", " #### #### # # # ", " # # # # # # ", " # # # # # # ", " #### #### ###### # ", " # = # ", " # = # ", " ############################### ", " ", " ", " ", " ", " " }; //set initial player position on the map var playerX = 8; var playerY = 6; //clear the console Console.Clear(); //send each row of the map to the Console map.ForEach( Console.WriteLine ); //create an empty ConsoleKeyInfo for storing the last key pressed var keyInfo = new ConsoleKeyInfo( ); //keep processing key presses until the player wants to quit while ( keyInfo.Key != ConsoleKey.Q ) { //store the player's current location var oldX = playerX; var oldY = playerY; //change the player's location if they pressed an arrow key switch ( keyInfo.Key ) { case ConsoleKey.UpArrow: playerY--; break; case ConsoleKey.DownArrow: playerY++; break; case ConsoleKey.LeftArrow: playerX--; break; case ConsoleKey.RightArrow: playerX++; break; } //check if the square that the player is trying to move to is empty if( map[ playerY ][ playerX ] == ' ' ) { //ok it was empty, clear the square they were standing on before Console.SetCursorPosition( oldX, oldY ); Console.Write( ' ' ); //now draw them at the new square Console.SetCursorPosition( playerX, playerY ); Console.Write( '@' ); } else { //they can't move there, change their location back to the old location playerX = oldX; playerY = oldY; } //wait for them to press a key and store it in keyInfo keyInfo = Console.ReadKey( true ); } </code></pre> <p>I was playing around with doing it in F#, initially I was trying to write it using functional concepts, but turned out I was a bit over my head, so I did pretty much a straight port - it's not <em>really</em> an F# program (though it compiles and runs) it's a procedural program written in F# syntax:</p> <pre><code>open System //define the map let map = [ " "; " "; " "; " "; " ############################### "; " # # "; " # ###### # "; " # # # # "; " #### #### # # # "; " # # # # # # "; " # # # # # # "; " #### #### ###### # "; " # = # "; " # = # "; " ############################### "; " "; " "; " "; " "; " " ] //set initial player position on the map let mutable playerX = 8 let mutable playerY = 6 //clear the console Console.Clear() //send each row of the map to the Console map |&gt; Seq.iter (printfn "%s") //create an empty ConsoleKeyInfo for storing the last key pressed let mutable keyInfo = ConsoleKeyInfo() //keep processing key presses until the player wants to quit while not ( keyInfo.Key = ConsoleKey.Q ) do //store the player's current location let mutable oldX = playerX let mutable oldY = playerY //change the player's location if they pressed an arrow key if keyInfo.Key = ConsoleKey.UpArrow then playerY &lt;- playerY - 1 else if keyInfo.Key = ConsoleKey.DownArrow then playerY &lt;- playerY + 1 else if keyInfo.Key = ConsoleKey.LeftArrow then playerX &lt;- playerX - 1 else if keyInfo.Key = ConsoleKey.RightArrow then playerX &lt;- playerX + 1 //check if the square that the player is trying to move to is empty if map.Item( playerY ).Chars( playerX ) = ' ' then //ok it was empty, clear the square they were standing on Console.SetCursorPosition( oldX, oldY ) Console.Write( ' ' ) //now draw them at the new square Console.SetCursorPosition( playerX, playerY ) Console.Write( '@' ) else //they can't move there, change their location back to the old location playerX &lt;- oldX playerY &lt;- oldY //wait for them to press a key and store it in keyInfo keyInfo &lt;- Console.ReadKey( true ) </code></pre> <p>So my question is, what do I need to learn in order to rewrite this more functionally, can you give me some hints, a vague overview, that kind of thing. </p> <p>I'd prefer a shove in the right direction rather than just seeing some code, but if that's the easiest way for you to explain it to me then fine, but in that case can you please also explain the "why" rather the "how" of it?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload