

I know many people who started programming because they wanted to write a game of their own. I myself had never done game programming but after I ran into articles about programming a roguelike in Haskell I decided to give it a try using F#.
First I wanted to get hero to move around on map.
These are the types I came up with:
Game World consists of MapTiles and Hero. MapTile has a coordinate in the world and a char that symbolises content of the tile, for example symbol for wall is ‘#’. Hero has data of it’s location(currentPosition) and location on previous turn(oldPosition). Input is a discriminated union of the possible inputs from the player.
Since my roguelike is basically a console app the “graphics” module deals with writing out chars on the correct coordinate in the console. HideCursor-function is used to set cursor on the upper left corner of screen. Otherwise it will stay where a character has been written last. drawHero draws the @-character representing hero in currentPosition of hero record. Original tile from world record will replace @-character in oldPosition of hero. Originally I reprinted whole map after every move, but that caused screen to flicker.
The game logic is located in the Program.fs-file. Starting at the main function:
GenerateCoordinates breaks level map into coordinates and tiles. World initialized with hero starting from the top left corner.
GameLoop is a recursive function that draws hero, gets player input and calls gameLoop again with new state of the world.
Move returns hero with updated coordinates. Movement is allowed if the wanted location is not a wall(#) or a closed door(+). OpenDoor replaces closed door(+) located next to the hero in the given direction with an open door(-).
Here is the amazing result!


Not quite Nethack yet, but we’ll see what features future brings. The codes for SharpRogue can be found on GitHub.