Submit this assignment on Canvas.
Author: Anthony Li
Reviewers: Jordan Hochman, Yuying Fan
Please leave feedback by posting on Ed or contacting the course staff.
Required Software: macOS Ventura, Xcode 15
Deadline: Monday, 2/12 @ 11:59 PM
Download Starter Code ↓ or clone from GitHub
Write a text adventure game of your own design! We'll provide the UI -- you provide the story and gameplay.
In a text adventure game, you walk the player through an interactive story. Generally, you describe the scene and the plot, and the player types in commands to interact with the world, a bit like this (with player inputs bolded, italicized, and underlined):
A flash of light. You're awake. Dazed, you look around. You're in a dark cave, with an exit to the east. You have no idea how you got here. Only one thing is certain: you have to get out.
east
Still confused, you sprint towards the light. As you exit the cave, sunlight engulfs you. You're at the edge of a rural village. A villager rests at the side of the road.
talk
You approach the villager. "Hello, traveler," she says. "Do you know if it's possible to get from two arbitrary but particular locations via the road network? Let G be an arbitrary but particular graph representing the road network. All I know is that G is an undirected graph with no cycles, and has exactly one fewer road than the number of locations."
All of a sudden, it hits you.
You're trapped in a CIS 1600 homework assignment.
Text adventure games have been around for a while, with games like Colossal Cave Adventure and Zork available for decades. (You can even install zork
on your computer using Homebrew.) Even more recent examples use AI to spice things up, like AI Dungeon. The possibilities are endless!
Your game should have:
In addition, your code should contain:
nil
case is meaningful and can occur in a live playthrough. For example, parsing a number from a string would be "non-trivial", as the string may not be a valid number. However, using Array.first or similar on an array guaranteed to be non-empty would not be "non-trivial".You should also fill in the README with:
All state should be stored in the adventure game struct itself, and not in a global variable. To test this, try clicking the "Reset" button. Everything in the struct should be reset to its initial state.
Be sure to follow the code style guidelines - we won't be grading on style too harshly, but unreadable or messy code may be reflected in the style grade.
Don't feel the need to go overboard with your game in terms of creativity or complexity - we're more interested in how you'll apply the concepts we've learned in class. If you're stuck for ideas, you can always implement a classic like Colossal Cave Adventure or Zork.
Note: This assignment is very design-heavy. You may find it helpful to sketch out your game's map and architecture on paper before you start coding.
To get started, download and unzip the starter code, and open up hw1.playground
. We've included a skeleton implementation to get you started. Feel free to rename the struct or add additional methods/properties as you see fit. You may notice some files in the "Sources" folder - those are used to implement the UI, and you shouldn't modify them.
You'll implement your game as a struct conforming to the AdventureGame
protocol, which is defined as follows:
/// A type that represents an Adventure game's state, behavior, and gameplay.
public protocol AdventureGame {
/// Creates a new game.
init()
/// Title to be displayed at the top of the game.
var title: String { get }
/// Runs at the start of every game.
/// - Parameter context: The object you use to write output and end the game.
mutating func start(context: AdventureGameContext)
/// Runs when the user enters a line of input.
/// - Parameters:
/// - input: The line the user typed.
/// - context: The object you use to write output and end the game.
mutating func handle(input: String, context: AdventureGameContext)
}
We'll pass in an AdventureGameContext
object which implements these 3 methods:
AdventureGameContext.write(_ attributedString: AttributedString)
: Adds the given line of rich text to the output.AdventureGameContext.write(_ string: String)
: Adds the given line to the output, without rich text formatting.AdventureGameContext.endGame()
: Ends the game immediately.Implement your game's logic in the start
and handle
methods, and keep track of its state through the struct's properties.
You can use these methods to write output to the game's UI. For example, you can write a line of text like this:
context.write("The forest beckons, dark and foreboding.")
To end the game, you can call context.endGame()
:
context.write("Game Over")
context.endGame()
Do not use print
- your output will not appear in the correct place.
To run your game, run the entirety of the playground. You'll see your game running on the right side of the screen. If you don't see it, go to the menu bar and make sure Editor > Live View is checked.
In general, any game object would make a good candidate for a struct or protocol. For example:
let arguments = input.split(separator: " ")
if arguments.isEmpty {
context.write("Please enter a command.")
return
}
switch arguments[0] {
case "help":
helpCommand(context: context)
case "north":
// ...
case "south":
// ...
case "east":
// ...
case "west":
// ...
default:
context.write("Invalid command.")
}
var attributedString = AttributedString("The forest beckons, dark and foreboding.")
attributedString.swiftUI.foregroundColor = .green
context.write(attributedString)
Note that setting attributedString.foregroundColor
(or similar properties) directly will not work - you must use the swiftUI
property.
We will grade your homework based on the following rubric, out of 100 points:
+5 points: Implement random outcomes/events that trigger when the player enters a location or performs an action. For this to count, the outcome must be random and not always occur.
+5 points: Implement a dynamically updating title
using a computed property. For example, the title could be the current location.
To submit your homework, zip (or tar.gz) both your playground and README and upload it to Canvas. Make sure that you've given the requirements a quick read before you do so.
Submit on Canvas →