Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions solutions/python/ghost-gobble-arcade-game/1/arcade_game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""Functions for implementing the rules of the classic arcade game Pac-Man."""


def eat_ghost(power_pellet_active, touching_ghost):
"""Verify that Pac-Man can eat a ghost if he is empowered by a power pellet.

:param power_pellet_active: bool - does the player have an active power pellet?
:param touching_ghost: bool - is the player touching a ghost?
:return: bool - can a ghost be eaten?
"""
if power_pellet_active == True and touching_ghost == True:
return True
else:
return False



def score(touching_power_pellet, touching_dot):
"""Verify that Pac-Man has scored when a power pellet or dot has been eaten.

:param touching_power_pellet: bool - is the player touching a power pellet?
:param touching_dot: bool - is the player touching a dot?
:return: bool - has the player scored or not?
"""

if touching_power_pellet == True or touching_dot == True:
return True
else:
return False


def lose(power_pellet_active, touching_ghost):
"""Trigger the game loop to end (GAME OVER) when Pac-Man touches a ghost without his power pellet.

:param power_pellet_active: bool - does the player have an active power pellet?
:param touching_ghost: bool - is the player touching a ghost?
:return: bool - has the player lost the game?
"""

if touching_ghost == True and power_pellet_active != True:
return True
else:
return False


def win(has_eaten_all_dots, power_pellet_active, touching_ghost):
"""Trigger the victory event when all dots have been eaten.

:param has_eaten_all_dots: bool - has the player "eaten" all the dots?
:param power_pellet_active: bool - does the player have an active power pellet?
:param touching_ghost: bool - is the player touching a ghost?
:return: bool - has the player won the game?
"""

if has_eaten_all_dots and not touching_ghost:
return True
elif has_eaten_all_dots and power_pellet_active and touching_ghost:
return True
else:
return False
46 changes: 46 additions & 0 deletions solutions/python/ghost-gobble-arcade-game/2/arcade_game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Functions for implementing the rules of the classic arcade game Pac-Man."""


def eat_ghost(power_pellet_active, touching_ghost):
"""Verify that Pac-Man can eat a ghost if he is empowered by a power pellet.

:param power_pellet_active: bool - does the player have an active power pellet?
:param touching_ghost: bool - is the player touching a ghost?
:return: bool - can a ghost be eaten?
"""
return power_pellet_active and touching_ghost



def score(touching_power_pellet, touching_dot):
"""Verify that Pac-Man has scored when a power pellet or dot has been eaten.

:param touching_power_pellet: bool - is the player touching a power pellet?
:param touching_dot: bool - is the player touching a dot?
:return: bool - has the player scored or not?
"""

return touching_power_pellet or touching_dot


def lose(power_pellet_active, touching_ghost):
"""Trigger the game loop to end (GAME OVER) when Pac-Man touches a ghost without his power pellet.

:param power_pellet_active: bool - does the player have an active power pellet?
:param touching_ghost: bool - is the player touching a ghost?
:return: bool - has the player lost the game?
"""

return touching_ghost and not power_pellet_active


def win(has_eaten_all_dots, power_pellet_active, touching_ghost):
"""Trigger the victory event when all dots have been eaten.

:param has_eaten_all_dots: bool - has the player "eaten" all the dots?
:param power_pellet_active: bool - does the player have an active power pellet?
:param touching_ghost: bool - is the player touching a ghost?
:return: bool - has the player won the game?
"""

return has_eaten_all_dots and (not touching_ghost or power_pellet_active)