-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex45.py
126 lines (92 loc) · 3.05 KB
/
ex45.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
126
from sys import exit
import random
# a very simple procedurally generated world tree
class Engine(object):
def __init__(self, world):
print "initializing game..."
self.world = world
def play(self):
print "game begin!\n"
current_area = self.world.starting_area() # root
next_area_id = current_area.enter()
current_area = self.world.next_area(next_area_id) # first child
while True:
# TODO: enter from the map
next_area_id = current_area.enter()
current_area = self.world.next_area(next_area_id)
class Area(object):
def enter(self):
print "not yet configured area"
exit(1)
def select_next(self, num_branch):
userInput = -1
while True:
try:
userInput = int(raw_input("> "))
except ValueError:
print "please enter a number between 0 and %d" % num_branch
continue
if not(userInput >= 0 and userInput <= num_branch):
print "please enter a number between 0 and %d" % num_branch
continue
else:
break
return userInput
class Area_0(object):
def __init__(self, num_branch):
self.area = Area()
self.num_branch = num_branch
def select_next(self):
return self.area.select_next(self.num_branch)
def enter(self):
print "This is Area_0, your journey begins!"
print "You have %r choices to move forward" % self.num_branch
x = self.select_next()
print "you've chosen %r\n" % x
if x == 0:
return 'Area_0'
elif x == 1:
return 'Area_1'
elif x == 2:
return 'Area_2'
elif x == 3:
return 'Area_3'
else:
print "you've broken the game..."
exit(1)
# class Area_p(object):
# def __init__(self, num_branch, area_id):
# self.area = Area()
# self.num_branch = num_branch
# self.area_id = area_id
# def select_next(self):
# return self.area.select_next(self.brnach)
# def enter(self):
# print "This is Area_%d" % self.area_id
# print "You have %d choices to move forward" % self.num_branch
# if num_branch == 0:
# print "you've reached a dead end"
# class Area_end(object):
# def __init__(self, num_branch):
# self.area = Area()
class World(object):
def __init__(self):
# area name and its class to call
self.areas = {
'Area_0': Area_0(2)
# 'Area_end': Area_end()
}
self.next_area_id = 1
def next_area(self, area_id):
# if area_id is not in the areas dict, then add_area
return self.areas.get(area_id)
def starting_area(self):
return self.areas.get('Area_0')
def add_area(self):
# generate a int for number of num_branch
# create area objects
# add id to the areas dictionary
pass
world = World()
game = Engine(world)
game.play()