Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Group Project
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ahmed Yusuf
Group Project
Commits
1499521d
Commit
1499521d
authored
2 years ago
by
Ahmed Yusuf
Browse files
Options
Downloads
Patches
Plain Diff
doctests removed
parent
8e06e3a5
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Group_game/game.py
+2
-204
2 additions, 204 deletions
Group_game/game.py
Group_game/gameparser.py
+0
-50
0 additions, 50 deletions
Group_game/gameparser.py
with
2 additions
and
254 deletions
Group_game/game.py
+
2
−
204
View file @
1499521d
...
...
@@ -6,24 +6,7 @@ from items import *
from
gameparser
import
*
def
list_of_items
(
items
):
"""
This function takes a list of items (see items.py for the definition) and
returns a comma-separated list of item names (as a string). For example:
>>>
list_of_items
([
item_pen
,
item_handbook
])
'
a pen, a student handbook
'
>>>
list_of_items
([
item_id
])
'
id card
'
>>>
list_of_items
([])
''
>>>
list_of_items
([
item_money
,
item_handbook
,
item_laptop
])
'
money, a student handbook, laptop
'
"""
new_list
=
""
for
number
in
range
(
len
(
items
)):
if
number
==
len
(
items
)
-
1
:
...
...
@@ -35,93 +18,16 @@ def list_of_items(items):
def
print_room_items
(
room
):
"""
This function takes a room as an input and nicely displays a list of items
found in this room (followed by a blank line). If there are no items in
the room, nothing is printed. See map.py for the definition of a room, and
items.py for the definition of an item. This function uses list_of_items()
to produce a comma-separated list of item names. For example:
>>>
print_room_items
(
rooms
[
"
Reception
"
])
There
is
a
pack
of
biscuits
,
a
student
handbook
here
.
<
BLANKLINE
>
>>>
print_room_items
(
rooms
[
"
Office
"
])
There
is
a
pen
here
.
<
BLANKLINE
>
>>>
print_room_items
(
rooms
[
"
Admins
"
])
(no output)
Note: <BLANKLINE> here means that doctest should expect a blank line.
"""
if
room
[
"
items
"
]:
print
(
"
There is
"
,
list_of_items
(
room
[
"
items
"
]),
"
here.
"
+
"
\n
"
)
def
print_inventory_items
(
items
):
"""
This function takes a list of inventory items and displays it nicely, in a
manner similar to print_room_items(). The only difference is in formatting:
print
"
You have ...
"
instead of
"
There is ... here.
"
. For example:
>>>
print_inventory_items
(
inventory
)
You
have
id
card
,
laptop
,
money
.
<
BLANKLINE
>
"""
if
items
:
print
(
"
You have
"
,
list_of_items
(
items
)
+
"
.
"
+
"
\n
"
)
def
print_room
(
room
):
"""
This function takes a room as an input and nicely displays its name
and description. The room argument is a dictionary with entries
"
name
"
,
"
description
"
etc. (see map.py for the definition). The name of the room
is printed in all capitals and framed by blank lines. Then follows the
description of the room and a blank line again. If there are any items
in the room, the list of items is printed next followed by a blank line
(use print_room_items() for this). For example:
>>>
print_room
(
rooms
[
"
Office
"
])
<
BLANKLINE
>
THE
GENERAL
OFFICE
<
BLANKLINE
>
You
are
standing
next
to
the
cashier
'
s till at
30-36 Newport Road. The cashier looks at you with hope
in their eyes. If you go west you can return to the
Queen
'
s
Buildings
.
<
BLANKLINE
>
There
is
a
pen
here
.
<
BLANKLINE
>
>>>
print_room
(
rooms
[
"
Reception
"
])
<
BLANKLINE
>
RECEPTION
<
BLANKLINE
>
You
are
in
a
maze
of
twisty
little
passages
,
all
alike
.
Next
to
you
is
the
School
of
Computer
Science
and
Informatics
reception
.
The
receptionist
,
Matt
Strangis
,
seems
to
be
playing
an
old
school
text
-
based
adventure
game
on
his
computer
.
There
are
corridors
leading
to
the
south
and
east
.
The
exit
is
to
the
west
.
<
BLANKLINE
>
There
is
a
pack
of
biscuits
,
a
student
handbook
here
.
<
BLANKLINE
>
>>>
print_room
(
rooms
[
"
Admins
"
])
<
BLANKLINE
>
MJ
AND
SIMON
'
S ROOM
<BLANKLINE>
You are leaning agains the door of the systems managers
'
room
.
Inside
you
notice
Matt
"
MJ
"
John
and
Simon
Jones
.
They
ignore
you
.
To
the
north
is
the
reception
.
<
BLANKLINE
>
Note: <BLANKLINE> here means that doctest should expect a blank line.
"""
# Display room name
print
()
print
(
room
[
"
name
"
].
upper
())
...
...
@@ -135,67 +41,16 @@ def print_room(room):
# COMPLETE ME!
#
def
exit_leads_to
(
exits
,
direction
):
"""
This function takes a dictionary of exits and a direction (a particular
exit taken from this dictionary). It returns the name of the room into which
this exit leads. For example:
>>>
exit_leads_to
(
rooms
[
"
Reception
"
][
"
exits
"
],
"
south
"
)
"
MJ and Simon
'
s room
"
>>>
exit_leads_to
(
rooms
[
"
Reception
"
][
"
exits
"
],
"
east
"
)
"
your personal tutor
'
s office
"
>>>
exit_leads_to
(
rooms
[
"
Tutor
"
][
"
exits
"
],
"
west
"
)
'
Reception
'
"""
return
rooms
[
exits
[
direction
]][
"
name
"
]
def
print_exit
(
direction
,
leads_to
):
"""
This function prints a line of a menu of exits. It takes a direction (the
name of an exit) and the name of the room into which it leads (leads_to),
and should print a menu line in the following format:
GO <EXIT NAME UPPERCASE> to <where it leads>.
For example:
>>>
print_exit
(
"
east
"
,
"
you personal tutor
'
s office
"
)
GO
EAST
to
you
personal
tutor
'
s office.
>>>
print_exit
(
"
south
"
,
"
MJ and Simon
'
s room
"
)
GO
SOUTH
to
MJ
and
Simon
'
s room.
"""
print
(
"
GO
"
+
direction
.
upper
()
+
"
to
"
+
leads_to
+
"
.
"
)
def
print_menu
(
exits
,
room_items
,
inv_items
):
"""
This function displays the menu of available actions to the player. The
argument exits is a dictionary of exits as exemplified in map.py. The
arguments room_items and inv_items are the items lying around in the room
and carried by the player respectively. The menu should, for each exit,
call the function print_exit() to print the information about each exit in
the appropriate format. The room into which an exit leads is obtained
using the function exit_leads_to(). Then, it should print a list of commands
related to items: for each item in the room print
"
TAKE <ITEM ID> to take <item name>.
"
and for each item in the inventory print
"
DROP <ITEM ID> to drop <item name>.
"
For example, the menu of actions available at the Reception may look like this:
You can:
GO EAST to your personal tutor
'
s
office
.
GO
WEST
to
the
parking
lot
.
GO
SOUTH
to
MJ
and
Simon
'
s room.
TAKE BISCUITS to take a pack of biscuits.
TAKE HANDBOOK to take a student handbook.
DROP ID to drop your id card.
DROP LAPTOP to drop your laptop.
DROP MONEY to drop your money.
What do you want to do?
"""
print
(
"
You can:
"
)
# Iterate over available exits
for
direction
in
exits
:
...
...
@@ -209,35 +64,15 @@ def print_menu(exits, room_items, inv_items):
#
# COMPLETE ME!
#
print
(
"
What do you want to do?
"
)
def
is_valid_exit
(
exits
,
chosen_exit
):
"""
This function checks, given a dictionary
"
exits
"
(see map.py) and
a players
'
s
choice
"
chosen_exit
"
whether
the
player
has
chosen
a
valid
exit
.
It
returns
True
if
the
exit
is
valid
,
and
False
otherwise
.
Assume
that
the
name
of
the
exit
has
been
normalised
by
the
function
normalise_input
().
For
example
:
>>>
is_valid_exit
(
rooms
[
"
Reception
"
][
"
exits
"
],
"
south
"
)
True
>>>
is_valid_exit
(
rooms
[
"
Reception
"
][
"
exits
"
],
"
up
"
)
False
>>>
is_valid_exit
(
rooms
[
"
Parking
"
][
"
exits
"
],
"
west
"
)
False
>>>
is_valid_exit
(
rooms
[
"
Parking
"
][
"
exits
"
],
"
east
"
)
True
"""
return
chosen_exit
in
exits
def
execute_go
(
direction
):
"""
This function, given the direction (e.g.
"
south
"
) updates the current room
to reflect the movement of the player if the direction is a valid exit
(and prints the name of the room into which the player is
moving). Otherwise, it prints
"
You cannot go there.
"
"""
global
current_room
if
is_valid_exit
(
current_room
[
"
exits
"
],
direction
):
new_room
=
move
(
current_room
[
"
exits
"
],
direction
)
...
...
@@ -248,11 +83,6 @@ def execute_go(direction):
def
execute_take
(
item_id
):
"""
This function takes an item_id as an argument and moves this item from the
list of items in the current room to the player
'
s
inventory
.
However
,
if
there
is
no
such
item
in
the
room
,
this
function
prints
"
You cannot take that.
"
"""
found
=
False
for
items
in
current_room
[
"
items
"
]:
if
item_id
==
items
[
"
id
"
]:
...
...
@@ -266,10 +96,6 @@ def execute_take(item_id):
def
execute_drop
(
item_id
):
"""
This
function
takes
an
item_id
as
an
argument
and
moves
this
item
from
the
player
'
s inventory to list of items in the current room. However, if there is
no such item in the inventory, this function prints
"
You cannot drop that.
"
"""
found
=
False
for
items
in
inventory
:
if
items
[
"
id
"
]
==
item_id
:
...
...
@@ -283,13 +109,6 @@ def execute_drop(item_id):
def
execute_command
(
command
):
"""
This function takes a command (a list of words as returned by
normalise_input) and, depending on the type of action (the first word of
the command:
"
go
"
,
"
take
"
, or
"
drop
"
), executes either execute_go,
execute_take, or execute_drop, supplying the second word as the argument.
"""
if
0
==
len
(
command
):
return
...
...
@@ -316,14 +135,6 @@ def execute_command(command):
def
menu
(
exits
,
room_items
,
inv_items
):
"""
This function, given a dictionary of possible exits from a room, and a list
of items found in the room and carried by the player, prints the menu of
actions using print_menu() function. It then prompts the player to type an
action. The players
'
s
input
is
normalised
using
the
normalise_input
()
function
before
being
returned
.
"""
# Display menu
print_menu
(
exits
,
room_items
,
inv_items
)
...
...
@@ -337,18 +148,6 @@ def menu(exits, room_items, inv_items):
def
move
(
exits
,
direction
):
"""
This
function
returns
the
room
into
which
the
player
will
move
if
,
from
a
dictionary
"
exits
"
of
avaiable
exits
,
they
choose
to
move
towards
the
exit
with
the
name
given
by
"
direction
"
.
For
example
:
>>>
move
(
rooms
[
"
Reception
"
][
"
exits
"
],
"
south
"
)
==
rooms
[
"
Admins
"
]
True
>>>
move
(
rooms
[
"
Reception
"
][
"
exits
"
],
"
east
"
)
==
rooms
[
"
Tutor
"
]
True
>>>
move
(
rooms
[
"
Reception
"
][
"
exits
"
],
"
west
"
)
==
rooms
[
"
Office
"
]
False
"""
# Next room to go to
return
rooms
[
exits
[
direction
]]
...
...
@@ -369,7 +168,6 @@ def main():
execute_command
(
command
)
# Are we being run as a script? If so, run main().
# '__main__' is the name of the scope in which top-level code executes.
# See https://docs.python.org/3.4/library/__main__.html for explanation
...
...
This diff is collapsed.
Click to expand it.
Group_game/gameparser.py
+
0
−
50
View file @
1499521d
...
...
@@ -13,20 +13,6 @@ skip_words = ['a', 'about', 'all', 'an', 'another', 'any', 'around', 'at',
def
filter_words
(
words
,
skip_words
):
"""
This function takes a list of words and returns a copy of the list from
which all words provided in the list skip_words have been removed.
For example:
>>>
filter_words
([
"
help
"
,
"
me
"
,
"
please
"
],
[
"
me
"
,
"
please
"
])
[
'
help
'
]
>>>
filter_words
([
"
go
"
,
"
south
"
],
skip_words
)
[
'
go
'
,
'
south
'
]
>>>
filter_words
([
'
how
'
,
'
about
'
,
'
i
'
,
'
go
'
,
'
through
'
,
'
that
'
,
'
little
'
,
'
passage
'
,
'
to
'
,
'
the
'
,
'
south
'
],
skip_words
)
[
'
go
'
,
'
passage
'
,
'
south
'
]
"""
new_list
=
[]
for
elements
in
words
:
...
...
@@ -41,18 +27,6 @@ def filter_words(words, skip_words):
def
remove_punct
(
text
):
"""
This function is used to remove all punctuation
marks from a string. Spaces do not count as punctuation and should
not be removed. The funcion takes a string and returns a new string
which does not contain any puctuation. For example:
>>>
remove_punct
(
"
Hello, World!
"
)
'
Hello World
'
>>>
remove_punct
(
"
-- ...Hey! -- Yes?!...
"
)
'
Hey Yes
'
>>>
remove_punct
(
"
,go!So.?uTh
"
)
'
goSouTh
'
"""
no_punct
=
""
for
char
in
text
:
if
not
(
char
in
string
.
punctuation
):
...
...
@@ -62,30 +36,6 @@ def remove_punct(text):
def
normalise_input
(
user_input
):
"""
This function removes all punctuation from the string and converts it to
lower case. It then splits the string into a list of words (also removing
any extra spaces between words) and further removes all
"
unimportant
"
words from the list of words using the filter_words() function. The
resulting list of
"
important
"
words is returned. For example:
>>>
normalise_input
(
"
Go south!
"
)
[
'
go
'
,
'
south
'
]
>>>
normalise_input
(
"
!!! tAkE,. LAmp!?!
"
)
[
'
take
'
,
'
lamp
'
]
>>>
normalise_input
(
"
HELP!!!!!!!
"
)
[
'
help
'
]
>>>
normalise_input
(
"
Now, drop the sword please.
"
)
[
'
drop
'
,
'
sword
'
]
>>>
normalise_input
(
"
Kill ~ tHe :- gObLiN,. wiTH my SWORD!!!
"
)
[
'
kill
'
,
'
goblin
'
,
'
sword
'
]
>>>
normalise_input
(
"
I would like to drop my laptop here.
"
)
[
'
drop
'
,
'
laptop
'
]
>>>
normalise_input
(
"
I wish to take this large gem now!
"
)
[
'
take
'
,
'
gem
'
]
>>>
normalise_input
(
"
How about I go through that little passage to the south...
"
)
[
'
go
'
,
'
passage
'
,
'
south
'
]
"""
# Remove punctuation and convert to lower case
no_punct
=
remove_punct
(
user_input
).
lower
()
sentence
=
no_punct
.
strip
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment