Skip to content

Instantly share code, notes, and snippets.

@edunham
Created January 31, 2013 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edunham/4681056 to your computer and use it in GitHub Desktop.
Save edunham/4681056 to your computer and use it in GitHub Desktop.
cyoa using only arrays and loops
texts = [
"You are walking through the woods.", # 0
"You see a bear! What do you do?", # 1
"The bear eats you and you die.", # 2
"You don't have a sandwich!", # 3
"You find a sandwich!", # 4
"The bear is very angry at you for stealing its food.", # 5
"The bear becomes your friend! You win!" # 6
]
options = [
["ok..."], # 0
["punch it", "give it a sandwich"], # 1
[], # 2
["Run away!", "Look for a sandwich."], # 3
["Eat it", "Give it to the bear"], # 4
["uhoh"], # 5
[] # 6
]
results = [
# each result should have as many states as the corresponding options
[1], # 0
[2, 3], # 1
[], # 2
[2, 4], # 3
[5, 6], # 4
[2], # 5
[] # 6
]
playing = True
state = 0
while(playing):
print texts[state]
for opt in enumerate(options[state]):
print str(opt[0]) + ' : ' + opt[1]
if len(results[state]) > 0:
choice = raw_input("> ")
state = results[state][int(choice)]
else:
print "Game Over!"
playing = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment