Browse Source

Ghost Gobble Arcade Game

master
Julio Biason 3 years ago
parent
commit
801bfe5151
  1. 13
      python/ghost-gobble-arcade-game/.exercism/config.json
  2. 1
      python/ghost-gobble-arcade-game/.exercism/metadata.json
  3. 63
      python/ghost-gobble-arcade-game/HELP.md
  4. 24
      python/ghost-gobble-arcade-game/HINTS.md
  5. 94
      python/ghost-gobble-arcade-game/README.md
  6. 40
      python/ghost-gobble-arcade-game/arcade_game.py
  7. 110
      python/ghost-gobble-arcade-game/arcade_game_test.py

13
python/ghost-gobble-arcade-game/.exercism/config.json

@ -0,0 +1,13 @@
{
"blurb": "Learn about bools by setting up the rules for the Ghost Gobble arcade game.",
"icon": "matching-brackets",
"authors": ["neenjaw"],
"contributors": ["cmccandless", "bethanyg"],
"files": {
"solution": ["arcade_game.py"],
"test": ["arcade_game_test.py"],
"exemplar": [".meta/exemplar.py"]
},
"forked_from": ["elixir/pacman-rules"],
"language_versions": ">=3.5"
}

1
python/ghost-gobble-arcade-game/.exercism/metadata.json

@ -0,0 +1 @@
{"track":"python","exercise":"ghost-gobble-arcade-game","id":"385d5eaa65f84688899828686d1f0366","url":"https://exercism.org/tracks/python/exercises/ghost-gobble-arcade-game","handle":"JBiason","is_requester":true,"auto_approve":false}

63
python/ghost-gobble-arcade-game/HELP.md

@ -0,0 +1,63 @@
# Help
## Running the tests
You can run the included tests by typing `pytest <exercisename>_test.py` on the command line from within the exercise's directory.
You can also tell Python to run the pytest module on the command line from either within the exercise directory or with a path to the exercise directory.
`python -m pytest <exercisename>_test.py` from within the exercise directory.
`python -m pytest /fully/qualified/path/to/<exercisename>/` OR `python -m pytest realtive/path/to/<exercisename>` from a non-exercise directory.
Many IDE's and code editors also have built-in support for using PyTest to run tests.
- [Visual Studio Code](https://code.visualstudio.com/docs/python/testing)
- [PyCharm Professional & Community Editions](https://www.jetbrains.com/help/pycharm/pytest.html#create-pytest-test)
- [Atom](https://atom.io/packages/atom-python-test)
- [Spyder](https://www.spyder-ide.org/blog/introducing-unittest-plugin/)
- [Sublime](https://github.com/kaste/PyTest)
- [vim-test](https://github.com/vim-test/vim-test)
See the [Python tests page](https://github.com/exercism/python/blob/main/docs/TESTS.md) for more information.
### Common `pytest` options
- `-v` : enable verbose output.
- `-x` : stop running tests on first failure.
- `--ff` : run failures from previous test before running other test cases.
For other options, see `python -m pytest -h`. PyTest documentation can be found [here](https://docs.pytest.org/en/latest/getting-started.html).
## Submitting your solution
You can submit your solution using the `exercism submit arcade_game.py` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Python track's documentation](https://exercism.org/docs/tracks/python)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
Below are some resources for getting help if you run into trouble:
- [The PSF](https://www.python.org) hosts Python downloads, documentation, and community resources.
- [Python Community on Discord](https://pythondiscord.com/) is a very helpful and active community.
- [#python on Libera.chat](https://www.python.org/community/irc/) this is where the cored developers for the language hang out and get work done.
- [Exercism on Gitter](https://gitter.im/exercism/home) join the Python room for Python-related questions or problems.
- [/r/learnpython/](https://www.reddit.com/r/learnpython/) is a subreddit designed for Python learners.
- [Python Community Forums](https://discuss.python.org/)
- [Pythontutor](http://pythontutor.com/) for stepping through small code snippets visually.
Additionally, [StackOverflow](http://stackoverflow.com/questions/tagged/python) is a good spot to search for your problem/question to see if it has been answered already.
If not - you can always [ask](https://stackoverflow.com/help/how-to-ask) or [answer](https://stackoverflow.com/help/how-to-answer) someone else's question.

24
python/ghost-gobble-arcade-game/HINTS.md

@ -0,0 +1,24 @@
# Hints
## General
- Don't worry about how the arguments are _derived_, focus on combining the arguments to return the intended result.
## 1. Define if Pac-Man can eat a ghost
- You can use the [Boolean][boolean] [operators][Boolean-operators] to combine arguments for a result.
## 2. Define if Pac-Man scores
- You can use the [Boolean][boolean] [operator][Boolean-operators] to combine arguments for a result.
## 3. Define if Pac-Man loses
- You can use the [boolean][Boolean] [operator][Boolean-operators] to combine arguments for a result.
## 4. Define if Pac-Man wins
- You can use the [Boolean][boolean] [operator][Boolean-operators] to combine arguments for a result.
[boolean]: https://docs.python.org/3/library/stdtypes.html#truth
[Boolean-operators]: https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not

94
python/ghost-gobble-arcade-game/README.md

@ -0,0 +1,94 @@
# Ghost Gobble Arcade Game
Welcome to Ghost Gobble Arcade Game on Exercism's Python Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
## Introduction
Python represents true and false values with the `bool` type.
There are only two values in this type: `True` and `False`.
These values can be bound to a variable:
```python
>>> true_variable = True
>>> false_variable = False
```
We can evaluate Boolean expressions using the `and`, `or`, and `not` operators:
```python
>>> true_variable = True and True
>>> false_variable = True and False
>>> true_variable = False or True
>>> false_variable = False or False
>>> true_variable = not False
>>> false_variable = not True
```
## Instructions
In this exercise, you need to implement some rules from [Pac-Man][Pac-Man], the classic 1980s-era arcade-game.
You have four rules to implement, all related to the game states.
> _Do not worry about how the arguments are derived, just focus on combining the arguments to return the intended result._
## 1. Define if Pac-Man eats a ghost
Define the `eat_ghost()` function that takes two parameters (_if Pac-Man has a power pellet active_ and _if Pac-Man is touching a ghost_) and returns a Boolean value if Pac-Man is able to eat the ghost.
The function should return `True` only if Pac-Man has a power pellet active and is touching a ghost.
```python
>>> eat_ghost(False, True)
...
False
```
## 2. Define if Pac-Man scores
Define the `score()` function that takes two parameters (_if Pac-Man is touching a power pellet_ and _if Pac-Man is touching a dot_) and returns a Boolean value if Pac-Man scored.
The function should return `True` if Pac-Man is touching a power pellet or a dot.
```python
>>> score(True, True)
...
True
```
## 3. Define if Pac-Man loses
Define the `lose()` function that takes two parameters (_if Pac-Man has a power pellet active_ and _if Pac-Man is touching a ghost_) and returns a Boolean value if Pac-Man loses.
The function should return `True` if Pac-Man is touching a ghost and does not have a power pellet active.
```python
>>> lose(False, True)
...
True
```
## 4. Define if Pac-Man wins
Define the `win()` function that takes three parameters (_if Pac-Man has eaten all of the dots_, _if Pac-Man has a power pellet active_, and _if Pac-Man is touching a ghost_) and returns a Boolean value if Pac-Man wins.
The function should return `True` if Pac-Man has eaten all of the dots and has not lost based on the parameters defined in part 3.
```python
>>> win(False, True, False)
...
False
```
[Pac-Man]: https://en.wikipedia.org/wiki/Pac-Man
## Source
### Created by
- @neenjaw
### Contributed to by
- @cmccandless
- @bethanyg

40
python/ghost-gobble-arcade-game/arcade_game.py

@ -0,0 +1,40 @@
def eat_ghost(power_pellet_active: bool, touching_ghost: bool) -> bool:
"""Check if the user can eat a ghost.
:param power_pellet_active: does the player have an active power pellet?
:param touching_ghost: is the player touching a ghost?
:return: True if the user can eat the ghost.
"""
return power_pellet_active and touching_ghost
def score(touching_power_pellet: bool, touching_dot: bool) -> bool:
"""Check if the user scored a point.
:param touching_power_pellet: does the player have an active power pellet?
:param touching_dot: is the player touching a dot?
:return: bool
"""
return touching_power_pellet or touching_dot
def lose(power_pellet_active: bool, touching_ghost: bool):
"""Check if the user died for touching a ghost without the power pellet.
:param power_pellet_active: does the player have an active power pellet?
:param touching_ghost: is the player touching a ghost?
:return: bool
"""
return touching_ghost and not power_pellet_active
def win(has_eaten_all_dots: bool, power_pellet_active: bool,
touching_ghost: bool) -> bool:
"""Check if the user won the game.
: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
"""
return has_eaten_all_dots and not lose(power_pellet_active, touching_ghost)

110
python/ghost-gobble-arcade-game/arcade_game_test.py

@ -0,0 +1,110 @@
import unittest
import pytest
from arcade_game import eat_ghost, score, lose, win
class GhostGobbleGameTest(unittest.TestCase):
@pytest.mark.task(taskno=1)
def test_ghost_gets_eaten(self):
self.assertIs(
eat_ghost(True, True),
True,
msg="ghost should get eaten"
)
@pytest.mark.task(taskno=1)
def test_ghost_does_not_get_eaten_because_no_power_pellet_active(self):
self.assertIs(
eat_ghost(False, True),
False,
msg="ghost does not get eaten because no power pellet active"
)
@pytest.mark.task(taskno=1)
def test_ghost_does_not_get_eaten_because_not_touching_ghost(self):
self.assertIs(
eat_ghost(True, False),
False,
msg="ghost does not get eaten because not touching ghost"
)
@pytest.mark.task(taskno=2)
def test_score_when_eating_dot(self):
self.assertIs(
score(False, True),
True,
msg="score when eating dot"
)
@pytest.mark.task(taskno=2)
def test_score_when_eating_power_pellet(self):
self.assertIs(
score(True, False),
True,
msg="score when eating power pellet"
)
@pytest.mark.task(taskno=2)
def test_no_score_when_nothing_eaten(self):
self.assertIs(
score(False, False),
False,
msg="no score when nothing eaten"
)
@pytest.mark.task(taskno=3)
def test_lose_if_touching_a_ghost_without_a_power_pellet_active(self):
self.assertIs(
lose(False, True),
True,
msg="lose if touching a ghost without a power pellet active"
)
@pytest.mark.task(taskno=3)
def test_dont_lose_if_touching_a_ghost_with_a_power_pellet_active(self):
self.assertIs(
lose(True, True),
False,
msg="don't lose if touching a ghost with a power pellet active"
)
@pytest.mark.task(taskno=3)
def test_dont_lose_if_not_touching_a_ghost(self):
self.assertIs(
lose(True, False),
False,
msg="don't lose if not touching a ghost"
)
@pytest.mark.task(taskno=4)
def test_win_if_all_dots_eaten(self):
self.assertIs(
win(True, False, False),
True,
msg="win if all dots eaten"
)
@pytest.mark.task(taskno=4)
def test_dont_win_if_all_dots_eaten_but_touching_a_ghost(self):
self.assertIs(
win(True, False, True),
False,
msg="don't win if all dots eaten, but touching a ghost"
)
@pytest.mark.task(taskno=4)
def test_win_if_all_dots_eaten_and_touching_a_ghost_with_a_power_pellet_active(self):
self.assertIs(
win(True, True, True),
True,
msg="win if all dots eaten and touching a ghost with a power pellet active"
)
@pytest.mark.task(taskno=4)
def test_dont_win_if_not_all_dots_eaten(self):
self.assertIs(
win(False, True, True),
False,
msg="don't win if not all dots eaten and touching a ghost with a power pellet active"
)
Loading…
Cancel
Save