-
Notifications
You must be signed in to change notification settings - Fork 0
/
tile_traveller.py
124 lines (111 loc) · 3.65 KB
/
tile_traveller.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
NORTH = 'n'
EAST = 'e'
SOUTH = 's'
WEST = 'w'
def move(direction, col, row):
''' Returns updated col, row given the direction '''
if direction == NORTH:
row += 1
elif direction == SOUTH:
row -= 1
elif direction == EAST:
col += 1
elif direction == WEST:
col -= 1
return(col, row)
def is_victory(col, row):
''' Return true is player is in the victory cell '''
return col == 3 and row == 1 # (3,1)
def print_directions_and_coins(directions_str, col, row, coins):
''' Prints the directions, and checks the col, row for lever. If lever, add to coins and return coins. '''
lever = False
lever = check_lever(col,row)
inp = ""
if lever:
inp = input("Pull a lever (y/n): ")
if inp == 'y' or inp == 'Y':
coins += 1
print("You received 1 coin, your total is now ", coins, ".", sep ='')
print("You can travel: ", end='')
first = True
for ch in directions_str:
if not first:
print(" or ", end='')
if ch == NORTH:
print("(N)orth", end='')
elif ch == EAST:
print("(E)ast", end='')
elif ch == SOUTH:
print("(S)outh", end='')
elif ch == WEST:
print("(W)est", end='')
first = False
print(".")
return coins
def find_directions(col, row):
''' Returns valid directions as a string given the supplied location '''
if col == 1 and row == 1: # (1,1)
valid_directions = NORTH
elif col == 1 and row == 2: # (1,2)
valid_directions = NORTH+EAST+SOUTH
elif col == 1 and row == 3: # (1,3)
valid_directions = EAST+SOUTH
elif col == 2 and row == 1: # (2,1)
valid_directions = NORTH
elif col == 2 and row == 2: # (2,2)
valid_directions = SOUTH+WEST
elif col == 2 and row == 3: # (2,3)
valid_directions = EAST+WEST
elif col == 3 and row == 2: # (3,2)
valid_directions = NORTH+SOUTH
elif col == 3 and row == 3: # (3,3)
valid_directions = SOUTH+WEST
return valid_directions
def check_lever(col, row):
''' Returns true if lever is on the col, and row'''
lever = False
if col == 1 and row == 2: # (1,2)
lever = True
elif col == 2 and row == 2: # (2,2)
lever = True
elif col == 2 and row == 3: # (2,3)
lever = True
elif col == 3 and row == 2: # (3,2)
lever = True
return lever
def play_one_move(col, row, valid_directions):
''' Plays one move of the game
Return if victory has been obtained and updated col,row '''
victory = False
direction = input("Direction: ")
direction = direction.lower()
if not direction in valid_directions:
print("Not a valid direction!")
else:
col, row = move(direction, col, row)
victory = is_victory(col, row)
return victory, col, row
# The main program starts here
victory = False
lever = False
row = 1
col = 1
coins = 0
valid_directions = NORTH
print_directions_and_coins(valid_directions, col, row, coins)
while not victory:
victory, col, row = play_one_move(col, row, valid_directions)
if victory:
print("Victory! Total coins ", coins, ".", sep = '')
play_again = input("Play again (y/n): ")
if play_again == 'y' or play_again == 'Y':
victory = False
lever = False
row = 1
col = 1
coins = 0
valid_directions = NORTH
print_directions_and_coins(valid_directions, col, row, coins)
else:
valid_directions = find_directions(col, row)
coins = print_directions_and_coins(valid_directions, col, row, coins)