Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

LuabeginnercliAI generated

ASCII Roguelike Map

Build a program in Lua that generates a dungeon map for a roguelike game and draws it with ASCII characters: # for walls, . for floors, @ for the player's start, > for the stairs, and a few symbols for items. It places random rooms that don't overlap, connects them with corridors, and checks that every room can be reached. The same seed always gives the same map. At the end you'll serve the map in a web browser using OpenResty. It's a fun first Lua project: you'll practice tables, loops, functions, random numbers and modules, and see a classic game-development technique in action.

Estimate
~10.5h
Steps
7
Completed by
0
Proposed by
codeseed.app

LuaRocks · OpenResty

Project roadmap

  1. 01

    Set up the project

    ~1h

    Install Lua 5.4 (or LuaJIT) and LuaRocks. Create map.lua as a module that returns a table of functions, and main.lua that requires it and prints Hello. Install the busted test framework with luarocks install busted. Done when: lua main.lua prints Hello and busted --version works.

  2. 02

    Create the grid

    ~1h

    In map.lua, write new_map(width, height), which returns a 2D table (a table of rows) filled with # walls, and render(map), which joins each row into a string and returns the whole map as text. Read the width, height and an optional seed from the command line (defaults 60, 20 and the current time), and call math.randomseed(seed). Done when: lua main.lua prints a solid rectangle of # characters.

  3. 03

    Place rooms

    ~2h

    Try to place up to 10 rooms with random sizes (4 to 10 wide, 3 to 6 tall) at random positions. Keep a room only if it doesn't overlap any existing room, leaving at least one wall between rooms, and carve its inside into . floors. Store each room's position and size in a list. Done when: the map shows several separate rectangular rooms.

  4. 04

    Connect rooms with corridors

    ~2h

    Connect each room to the next one in the list with an L-shaped corridor: from the center of one room, go horizontally, then vertically, to the center of the next, carving . floors along the way. Randomly choose whether the corridor goes horizontal-first or vertical-first. Done when: every room is joined to the others by corridors.

  5. 05

    Add the player, stairs and items

    ~1.5h

    Place @ in the center of the first room and > in the last room, then scatter 5 items on random floor cells ($ for gold, ! for a potion), never on the player, the stairs or each other. Write a flood-fill function that starts at @ and checks that the stairs and every item can be reached by walking on floors. Done when: the map shows all symbols, and the reachability check returns true.

  6. 06

    Serve the map with OpenResty

    ~1.5h

    Install OpenResty and create an nginx.conf with a location / that uses content_by_lua_block to require your map module, generate a map (using a seed from the ?seed= query parameter when given) and return it inside an HTML <pre> tag. Add the steps to run it in the README. Done when: opening http://localhost:8080/?seed=42 shows the same map as lua main.lua with seed 42.

  7. 07

    Write tests

    ~1.5h

    Write busted tests in spec/map_spec.lua checking that: the same seed gives the same map, rooms never overlap, the map has the requested width and height, and everything is reachable from @ for 20 different seeds. Done when: busted passes.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~10.5h · 7 steps

Tech stack

LuaRocksOpenResty