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:
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)
Computes a value called Power, using this formula:
power := hp + mp*2 + level*10Prints 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.go— the name prompt done for you, and the rest laid out with TODO comments. Finish it so the sheet prints at the end. It runs as it is: it asks for a name and prints one placeholder line, which you replace.finished.go— a working version. Look at it after trying yours.
Run with:
go run projects/02-character-sheet/starter.go
Hints
- Each prompt is a
fmt.Print("Label: ")followed by a read. The first read declarestextwith:=, and later reads reuse it with=. - Trim every line with
strings.TrimSpace. Convert any value you will do maths on withstrconv.Atoi, and add"strconv"to the imports. - Use
fmt.Printflikefmt.Printf("| Name : %s\n", name)for the labelled lines — cleaner than joining strings with+. - For the borders,
strings.Repeat("-", 38)or similar saves you typing dashes by hand.
What you cannot use yet
ifto validate input. If the user typesabcfor HP, the program treats it as0and prints a lower Power. That is fine for now.- Loops. Write every prompt out explicitly.
- Your own functions. Only
mainand the library functions you have seen are needed.
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, else and switch, where your programs start making decisions.