-
Notifications
You must be signed in to change notification settings - Fork 0
/
choice.py
executable file
·126 lines (103 loc) · 3.26 KB
/
choice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
import sys
import csv
import webbrowser
import random
from pygame import mixer
from collections import deque
def _process_choices(row):
choices = []
for choice, next_step in zip(row[::2], row[1::2] ):
if next_step:
try:
item = (choice, int(next_step))
except ValueError:
print "Error reading choice " + str(row)
print "Choice list should be [Choice]:[step]"
print "Example - yes:3"
sys.exit()
else:
choices.append(item)
return choices
def _process_additional_field(field,item):
action = field.split("#")
item[action[0]] = action[1]
def _process_additional_fields(row,item):
if "@" not in row: return
field = row.pop(0)
while field != "@":
_process_additional_field(field,item)
field = row.pop(0)
def _process_row(row):
item = {}
item['text'] = row.pop(0)
_process_additional_fields(row,item)
item['choices'] = _process_choices(row)
return item
def load_choices(s):
choices = {}
with open(s,'r') as csvfile:
csvreader = csv.reader(csvfile, delimiter=':')
for row in csvreader:
if len(row) > 1:
choice_id = int(row.pop(0))
entry = _process_row(row)
choices[choice_id] = entry
return choices
def _print_available_choices(d):
for key, choice in d.iteritems():
answer,_ = choice
line = str(key) + ".) " + answer
print line
def _read_input(msg):
return int(raw_input(msg))
def _execute_actions(step):
if "sound" in step:
mixer.init()
mixer.music.load(step['sound'])
mixer.music.play()
if "html" in step:
webbrowser.open(step['html'])
return
def _print_story(d, step_id,previous_step_id):
step = d[step_id]
print step['text'].replace('\\n', '\n')
print ""
_execute_actions(step)
choice_list = {}
num=0
for num,choice in enumerate(step['choices'],start=1):
choice_list[num] = choice
num+=1
choice_list[num] = ("Go Back",previous_step_id)
num+=1
choice_list[num] = ("Quit", 0)
_print_available_choices(choice_list)
choice_selected = _read_input("Enter Choice: ")
return ('none',0) if choice_selected is 0 else choice_list[choice_selected]
def _check_for_passthrough_step(choice_dict,step_id):
step = choice_dict[step_id]
if "random" in step:
possible_choices = step["random"].split(",")
choice = random.choice(possible_choices)
return int(choice)
return step_id
def start_game(s):
choice_dict = load_choices(s)
choice_deque = deque([0])
step_id = 1
previous_step_id = 0
while(step_id != 0):
step_id = _check_for_passthrough_step(choice_dict,step_id)
choice_text,new_step_id = _print_story(choice_dict,step_id, previous_step_id)
if choice_text == "Go Back" and len(choice_deque) > 1:
choice_deque.popleft()
else:
choice_deque.appendleft(step_id)
previous_step_id = choice_deque[0]
step_id = new_step_id
print "Goodbye"
def main(story_file):
start_game(story_file)
if __name__ == "__main__":
main(sys.argv[1])