Part 3 mini-project: Character Sheet

This project pulls together all of Part 3: variables, strings, numbers, and keyboard input.

It asks the user a few questions about their game character, then prints a formatted character sheet with every answer plus one value the program works out itself.

What to build

A program that, when run:

  1. Prompts the user for five pieces of information about a character:

    • Name (text)
    • Class (text, e.g. "Warrior", "Mage", "Rogue")
    • Level (whole number)
    • HP (whole number — hit points)
    • MP (whole number — magic points)
  2. Computes a value called Power, using this formula:

     power = hp + mp * 2 + level * 10
  3. Prints a character sheet in roughly this shape:

     +--------------------------------------+
     | CHARACTER SHEET                      |
     +--------------------------------------+
     | Name : Keiko
     | Class: Mage
     | Level: 7
     | HP   : 95
     | MP   : 60
     | Power: 285
     +--------------------------------------+

The box widths do not have to match exactly. What matters is that every value appears with a label, and the Power line shows the computed value.

Files

The starter and finished versions are in projects/02-character-sheet/:

  • starter.lua — the prompts laid out with TODO comments. Finish it so the sheet prints at the end.
  • finished.lua — a working version. Look at it after trying yours.

Run with:

lua projects/02-character-sheet/starter.lua

Hints

  • Each prompt needs an io.write followed by an io.read. Wrap any value you will do maths on with tonumber.
  • Use string.format with %s and %d for the labelled lines — cleaner than gluing each line together with five .. operators.
  • For the borders, string.rep("-", 38) or similar saves you typing dashes by hand.

What you cannot use yet

  • if to validate input. If the user types "abc" for HP, the program errors. That is fine for now.
  • Loops. Write every prompt out explicitly.
  • Your own functions. Built-ins (io.read, io.write, print, tonumber, string.format, string.rep) are all fair game.

Done?

If the sheet shows every value with a label and the Power line is the correct sum, you are done. Move on to Chapter 16 — if / elseif / else, where input validation becomes possible.