Random Stories

Write a program that generates a random story, where the details of the story come from arrays. The program will ask for and store the main character’s name. The details of the story should be set by having an array of monsters, fantasy races, and weapons. When the program starts, the details are set to variables, which can then be used to tell the story. The story is then displayed to the user in story form.

What is your character's name? Johan

Johan was a brave human armed with a sword.
One day, while exploring a dark forest, they encountered a fearsome skeleton.
With a deep breath and a steady hand, Johan raised their sword and faced the creature head-on.
The battle was fierce, but in the end, Johan was victorious!

The land was safe once again, thanks to the courage of the human named Johan.
DECLARE monsters INITIALLY ["dragon", "troll", "vampire", "goblin", "skeleton", "orc", "goblin"]
DECLARE races INITIALLY ["elf", "dwarf", "human"]
DECLARE weapons INITIALLY ["sword", "axe", "bow", "staff"]

DECLARE name INITIALLY ""
DECLARE chosenMonster INITIALLY ""
DECLARE chosenRace INITIALLY ""
DECLARE chosenWeapon INITIALLY ""
DECLARE index INITIALLY 0

SEND "What is your character's name? " TO DISPLAY
RECEIVE name FROM (STRING) KEYBOARD

SET index TO RANDOM_NUMBER BETWEEN 0 AND LENGTH(monsters) - 1
SET chosenMonster TO monsters[index]

SET index TO RANDOM_NUMBER BETWEEN 0 AND LENGTH(races) - 1
SET chosenRace TO races[index]

SET index TO RANDOM_NUMBER BETWEEN 0 AND LENGTH(weapons) - 1
SET chosenWeapon TO weapons[index]

SEND "" TO DISPLAY
SEND "" TO DISPLAY

SEND name & " was a brave " & chosenRace & " armed with a " & chosenWeapon & "." TO DISPLAY
SEND "One day, while exploring a dark forest, they encountered a fearsome " & chosenMonster & "." TO DISPLAY
SEND "With a deep breath and a steady hand, " & name & " raised their " & chosenWeapon & " and faced the creature head-on." TO DISPLAY
SEND "The battle was fierce, but in the end, " & name & " was victorious!" TO DISPLAY
SEND "" TO DISPLAY
SEND "The land was safe once again, thanks to the courage of the " & chosenRace & " named " & name & "." TO DISPLAY

import random

monsters = ["dragon", "troll", "vampire", "goblin", "skeleton", "orc", "goblin"]
races = ["elf", "dwarf", "human"]
weapons = ["sword", "axe", "bow", "staff"]

name = input("What is your character's name? ")

chosen_monster = monsters[random.randint(0, len(monsters)-1)]
chosen_race = races[random.randint(0, len(monsters)-1)]
chosen_weapon = weapons[random.randint(0, len(weapons)-1)]

print()
print()
print(name, "was a brave", chosen_race, "armed with a", chosen_weapon + ".")
print("One day, while exploring a dark forest, they encountered a fearsome", chosen_monster + ".")
print("With a deep breath and a steady hand, " + name, "raised their", chosen_weapon, "and faced the creature head-on.")
print("The battle was fierce, but in the end,", name, "was victorious!")
print()
print("The land was safe once again, thanks to the courage of the", chosen_race + " named", name, ".")
import random

# Arrays of story elements
monsters = ["dragon", "troll", "vampire", "goblin", "skeleton", "orc", "goblin"]
races = ["elf", "dwarf", "human"]
weapons = ["sword", "axe", "bow", "staff"]

# Ask the user for their character name
name = input("What is your character's name? ")

# Randomly select story elements
chosen_monster = monsters[random.randint(0, len(monsters)-1)]
chosen_race = races[random.randint(0, len(monsters)-1)]
chosen_weapon = weapons[random.randint(0, len(weapons)-1)]

# Create and display the story
print()
print()
print(name, "was a brave", chosen_race, "armed with a", chosen_weapon + ".")
print("One day, while exploring a dark forest, they encountered a fearsome", chosen_monster + ".")
print("With a deep breath and a steady hand, " + name, "raised their", chosen_weapon, "and faced the creature head-on.")
print("The battle was fierce, but in the end,", name, "was victorious!")
print()
print("The land was safe once again, thanks to the courage of the", chosen_race + " named", name, ".")

Extension

Extend the program so that it includes the following:

  • An additional array that has a setting like a Volcano, Forest, Desert.
  • Allow the user to make a decision that changes the story.
    • This could be an if statement to ask what they will do – use Input Validation!
  • Create variables like Gold or Health.
    • Can you find a way to spend this? Perhaps create a shop or fight a monster.
  • Make the whole story replayable.
    • Ask the user if they would like to play again, and replay the game – but random!

Target

You should be able to use 1-D Arrays to solve a problem.