Skip to content

Commit b297cf4

Browse files
committed
added a game5 example and made some general improvemnts to games 1-4
1 parent 196f728 commit b297cf4

File tree

6 files changed

+201
-35
lines changed

6 files changed

+201
-35
lines changed

examples/board.dat

+15-23
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
1-
******************************************
2-
* * *
3-
* * *
4-
* * *
5-
* *************
6-
* * *
7-
* * *
8-
* * *
9-
* ^ * *
10-
* * *
11-
******************************************
12-
* *
13-
* *
14-
* *
15-
* *
16-
* *
17-
* *
18-
* *
19-
* *
20-
* *
21-
* *
22-
* *
23-
******************************************
1+
******************************
2+
* * *
3+
* * *
4+
* * *
5+
* * o *
6+
* * *
7+
* ^ * *
8+
* * *
9+
*************-**********-*****
10+
* *
11+
* $ *
12+
* *
13+
* *
14+
* *
15+
******************************

examples/game1.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ def print_char(char):
2626
print(char, end='')
2727

2828

29+
def get_input():
30+
try:
31+
move = raw_input("Choose a direction (Type `quit` to quit): ")
32+
except NameError:
33+
# Python3
34+
move = input("Choose a direction (Type `quit` to quit): ")
35+
return move
36+
37+
2938
def load_board(filename):
3039
"""
3140
Given a filename of a text file, load the characters therein and return as
@@ -79,7 +88,7 @@ def main():
7988
# Display the board and player
8089
display(board, player_x, player_y)
8190
# Obtain and handle user input
82-
move = raw_input("Choose a direction (Type `quit` to quit): ")
91+
move = get_input()
8392
# FIXME: Insert code here to handle the 'move' the player has made.
8493
# (Hint: should manipulate player_x and player_y)
8594
if move == 'quit':

examples/game2.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ def print_char(char):
2727
print(char, end='')
2828

2929

30+
def get_input():
31+
try:
32+
move = raw_input("Choose a direction (Type `quit` to quit): ")
33+
except NameError:
34+
# Python3
35+
move = input("Choose a direction (Type `quit` to quit): ")
36+
return move
37+
38+
3039
def load_board(filename):
3140
"""
3241
Given a filename of a text file, load the characters therein and return as
@@ -79,7 +88,7 @@ def main():
7988
player_y = PLAYER_Y_START
8089
while True:
8190
display(board, player_x, player_y)
82-
move = raw_input("Choose a direction (Type `quit` to quit): ")
91+
move = get_input()
8392
if move == 'quit':
8493
return True
8594
# FIXME: Make it so the player cannot walk through walls

examples/game3.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,23 @@
2323
PLAYER_X_START = 5
2424
PLAYER_Y_START = 5
2525

26+
NON_SOLIDS = ['^', '$', 'o', ' ']
27+
2628

2729
def print_char(char):
2830
"""Print only the given `char` without a newline"""
2931
print(char, end='')
3032

3133

34+
def get_input():
35+
try:
36+
move = raw_input("Choose a direction (Type `quit` to quit): ")
37+
except NameError:
38+
# Python3
39+
move = input("Choose a direction (Type `quit` to quit): ")
40+
return move
41+
42+
3243
def load_board(filename):
3344
"""
3445
Given a filename of a text file, load the characters therein and return as
@@ -84,20 +95,20 @@ def main():
8495
display(board, player_x, player_y, player_inventory)
8596
print('Player inventory:')
8697
# FIXME: Print out the player's inventory here
87-
move = raw_input("Choose a direction (Type `quit` to quit): ")
98+
move = get_input()
8899
if move == 'quit':
89100
return True
90101
if move == 'up':
91-
if get_tile(board, player_x, player_y - 1) != '*':
102+
if get_tile(board, player_x, player_y - 1) in NON_SOLIDS:
92103
player_y -= 1
93104
if move == 'down':
94-
if get_tile(board, player_x, player_y + 1) != '*':
105+
if get_tile(board, player_x, player_y + 1) in NON_SOLIDS:
95106
player_y += 1
96107
if move == 'left':
97-
if get_tile(board, player_x - 1, player_y) != '*':
108+
if get_tile(board, player_x - 1, player_y) in NON_SOLIDS:
98109
player_x -= 1
99110
if move == 'right':
100-
if get_tile(board, player_x + 1, player_y) != '*':
111+
if get_tile(board, player_x + 1, player_y) in NON_SOLIDS:
101112
player_x += 1
102113
# FIXME: Check if the user has obtained the item (A ^ on the board)
103114
# Remove the item from the board if the user picks it up

examples/game4.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
* Moves the player on the board when the user types input such as "up"
1212
* Prevents the player from walking through walls
1313
* Makes it so the player can pick up the item (Item looks like ^)
14+
15+
Implement the following:
16+
* Make it so the player can use the key to open the door.
1417
"""
1518
from __future__ import print_function
1619

@@ -19,12 +22,23 @@
1922
PLAYER_X_START = 5
2023
PLAYER_Y_START = 5
2124

25+
NON_SOLIDS = ['^', '$', 'o', ' ']
26+
2227

2328
def print_char(char):
2429
"""Print only the given `char` without a newline"""
2530
print(char, end='')
2631

2732

33+
def get_input():
34+
try:
35+
move = raw_input("Choose a direction (Type `quit` to quit): ")
36+
except NameError:
37+
# Python3
38+
move = input("Choose a direction (Type `quit` to quit): ")
39+
return move
40+
41+
2842
def load_board(filename):
2943
"""
3044
Given a filename of a text file, load the characters therein and return as
@@ -81,20 +95,24 @@ def main():
8195
print('Player inventory:')
8296
for item in player_inventory:
8397
print(item)
84-
move = raw_input("Choose a direction (Type `quit` to quit): ")
98+
move = get_input()
8599
if move == 'quit':
86100
return True
87101
if move == 'up':
88-
if get_tile(board, player_x, player_y - 1) != '*':
102+
# FIXME: Add a check that allows the player to pass through a door
103+
# if they have a key.
104+
if get_tile(board, player_x, player_y - 1) in NON_SOLIDS:
89105
player_y -= 1
90106
if move == 'down':
91-
if get_tile(board, player_x, player_y + 1) != '*':
107+
# FIXME: Add a check that allows the player to pass through a door
108+
# if they have a key.
109+
if get_tile(board, player_x, player_y + 1) in NON_SOLIDS:
92110
player_y += 1
93111
if move == 'left':
94-
if get_tile(board, player_x - 1, player_y) != '*':
112+
if get_tile(board, player_x - 1, player_y) in NON_SOLIDS:
95113
player_x -= 1
96114
if move == 'right':
97-
if get_tile(board, player_x + 1, player_y) != '*':
115+
if get_tile(board, player_x + 1, player_y) in NON_SOLIDS:
98116
player_x += 1
99117
if get_tile(board, player_x, player_y) == '^' and '^' not in player_inventory:
100118
board[player_y][player_x] = ' '

examples/game5.py

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#!/usr/bin/python
2+
"""
3+
This is a simple text based game that displays a board and obtains input from
4+
the command line.
5+
6+
7+
In this stage, the following is already implemented:
8+
* Loads a text file as the playing board
9+
* Displays the board and obtains input in a loop until a user types `quit`
10+
* Displays the player on the board
11+
* Moves the player on the board when the user types input such as "up"
12+
* Prevents the player from walking through walls
13+
* Makes it so the player can pick up the item (Item looks like ^)
14+
15+
Implement the following:
16+
* Make it so the player can use the key to open the door.
17+
"""
18+
from __future__ import print_function
19+
20+
21+
DEFAULT_BOARD = 'board.dat'
22+
PLAYER_X_START = 5
23+
PLAYER_Y_START = 5
24+
25+
NON_SOLIDS = ['^', '$', 'o', ' ']
26+
27+
28+
def print_char(char):
29+
"""Print only the given `char` without a newline"""
30+
print(char, end='')
31+
32+
33+
def get_input():
34+
try:
35+
move = raw_input("Choose a direction (Type `quit` to quit): ")
36+
except NameError:
37+
# Python3
38+
move = input("Choose a direction (Type `quit` to quit): ")
39+
return move
40+
41+
42+
def load_board(filename):
43+
"""
44+
Given a filename of a text file, load the characters therein and return as
45+
a list of character lists. E.g.:
46+
47+
A file containing:
48+
xox
49+
oxx
50+
oxo
51+
52+
would return:
53+
[
54+
['x', 'o', 'x'],
55+
['o', 'x', 'x'],
56+
['o', 'x', 'o'],
57+
]
58+
"""
59+
board_file = open(filename)
60+
board_tiles = []
61+
for line in board_file.readlines():
62+
board_tiles.append([char for char in line.strip()])
63+
board_file.close()
64+
return board_tiles
65+
66+
67+
def get_tile(board, x, y):
68+
"""
69+
Returns the character for a give, `x` and `y` position of the given `board`
70+
"""
71+
return board[y][x]
72+
73+
74+
def display(board, player_x, player_y, player_inventory):
75+
"""
76+
Display the given `board` and the player at the position `player_x` and
77+
`player_y`
78+
"""
79+
for y, row in enumerate(board):
80+
for x, tile in enumerate(row):
81+
if x == player_x and y == player_y:
82+
print_char('@')
83+
else:
84+
print_char(tile)
85+
print_char('\n')
86+
87+
88+
def main():
89+
board = load_board(DEFAULT_BOARD)
90+
player_x = PLAYER_X_START
91+
player_y = PLAYER_Y_START
92+
player_inventory = []
93+
while True:
94+
display(board, player_x, player_y, player_inventory)
95+
print('Player inventory:')
96+
for item in player_inventory:
97+
print(item)
98+
move = get_input()
99+
if move == 'quit':
100+
return True
101+
if move == 'up':
102+
next_tile = get_tile(board, player_x, player_y - 1)
103+
if next_tile in NON_SOLIDS or '^' in player_inventory:
104+
player_y -= 1
105+
if move == 'down':
106+
next_tile = get_tile(board, player_x, player_y + 1)
107+
if next_tile in NON_SOLIDS or '^' in player_inventory:
108+
player_y += 1
109+
if move == 'left':
110+
next_tile = get_tile(board, player_x - 1, player_y)
111+
if next_tile in NON_SOLIDS or '^' in player_inventory:
112+
player_x -= 1
113+
if move == 'right':
114+
next_tile = get_tile(board, player_x + 1, player_y)
115+
if next_tile in NON_SOLIDS or '^' in player_inventory:
116+
player_x += 1
117+
current_tile = get_tile(board, player_x, player_y)
118+
if current_tile == '^' and '^' not in player_inventory:
119+
board[player_y][player_x] = ' '
120+
player_inventory.append('^')
121+
if current_tile == '-' and '^' in player_inventory:
122+
board[player_y][player_x] = ' '
123+
player_inventory.remove('^')
124+
125+
126+
if __name__ == '__main__':
127+
main()

0 commit comments

Comments
 (0)