primeiro commit com exercicios de Exercism realizados até esta data
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"authors": [
|
||||
"neenjaw"
|
||||
],
|
||||
"contributors": [
|
||||
"cmccandless",
|
||||
"BethanyG"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"arcade_game.py"
|
||||
],
|
||||
"test": [
|
||||
"arcade_game_test.py"
|
||||
],
|
||||
"exemplar": [
|
||||
".meta/exemplar.py"
|
||||
]
|
||||
},
|
||||
"language_versions": ">=3.5",
|
||||
"forked_from": [
|
||||
"elixir/pacman-rules"
|
||||
],
|
||||
"icon": "matching-brackets",
|
||||
"blurb": "Learn about bools by setting up the rules for the Ghost Gobble arcade game."
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{"track":"python","exercise":"ghost-gobble-arcade-game","id":"c2797792a9de49279f09181e1fb96eda","url":"https://exercism.org/tracks/python/exercises/ghost-gobble-arcade-game","handle":"sanctushck","is_requester":true,"auto_approve":false}
|
||||
@@ -0,0 +1,130 @@
|
||||
# Help
|
||||
|
||||
## Running the tests
|
||||
|
||||
We use [pytest][pytest: Getting Started Guide] as our website test runner.
|
||||
You will need to install `pytest` on your development machine if you want to run tests for the Python track locally.
|
||||
You should also install the following `pytest` plugins:
|
||||
|
||||
- [pytest-cache][pytest-cache]
|
||||
- [pytest-subtests][pytest-subtests]
|
||||
|
||||
Extended information can be found in our website [Python testing guide][Python track tests page].
|
||||
|
||||
|
||||
### Running Tests
|
||||
|
||||
To run the included tests, navigate to the folder where the exercise is stored using `cd` in your terminal (_replace `{exercise-folder-location}` below with your path_).
|
||||
Test files usually end in `_test.py`, and are the same tests that run on the website when a solution is uploaded.
|
||||
|
||||
Linux/MacOS
|
||||
```bash
|
||||
$ cd {path/to/exercise-folder-location}
|
||||
```
|
||||
|
||||
Windows
|
||||
```powershell
|
||||
PS C:\Users\foobar> cd {path\to\exercise-folder-location}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
Next, run the `pytest` command in your terminal, replacing `{exercise_test.py}` with the name of the test file:
|
||||
|
||||
Linux/MacOS
|
||||
```bash
|
||||
$ python3 -m pytest -o markers=task {exercise_test.py}
|
||||
==================== 7 passed in 0.08s ====================
|
||||
```
|
||||
|
||||
Windows
|
||||
```powershell
|
||||
PS C:\Users\foobar> py -m pytest -o markers=task {exercise_test.py}
|
||||
==================== 7 passed in 0.08s ====================
|
||||
```
|
||||
|
||||
|
||||
### Common options
|
||||
- `-o` : override default `pytest.ini` (_you can use this to avoid marker warnings_)
|
||||
- `-v` : enable verbose output.
|
||||
- `-x` : stop running tests on first failure.
|
||||
- `--ff` : run failures from previous test before running other test cases.
|
||||
|
||||
For additional options, use `python3 -m pytest -h` or `py -m pytest -h`.
|
||||
|
||||
|
||||
### Fixing warnings
|
||||
|
||||
If you do not use `pytest -o markers=task` when invoking `pytest`, you might receive a `PytestUnknownMarkWarning` for tests that use our new syntax:
|
||||
|
||||
```bash
|
||||
PytestUnknownMarkWarning: Unknown pytest.mark.task - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
|
||||
```
|
||||
|
||||
To avoid typing `pytest -o markers=task` for every test you run, you can use a `pytest.ini` configuration file.
|
||||
We have made one that can be downloaded from the top level of the Python track directory: [pytest.ini][pytest.ini].
|
||||
|
||||
You can also create your own `pytest.ini` file with the following content:
|
||||
|
||||
```ini
|
||||
[pytest]
|
||||
markers =
|
||||
task: A concept exercise task.
|
||||
```
|
||||
|
||||
Placing the `pytest.ini` file in the _root_ or _working_ directory for your Python track exercises will register the marks and stop the warnings.
|
||||
More information on pytest marks can be found in the `pytest` documentation on [marking test functions][pytest: marking test functions with attributes] and the `pytest` documentation on [working with custom markers][pytest: working with custom markers].
|
||||
|
||||
Information on customizing pytest configurations can be found in the `pytest` documentation on [configuration file formats][pytest: configuration file formats].
|
||||
|
||||
|
||||
### Extending your IDE or Code Editor
|
||||
|
||||
Many IDEs and code editors have built-in support for using `pytest` and other code quality tools.
|
||||
Some community-sourced options can be found on our [Python track tools page][Python track tools page].
|
||||
|
||||
[Pytest: Getting Started Guide]: https://docs.pytest.org/en/latest/getting-started.html
|
||||
[Python track tools page]: https://exercism.org/docs/tracks/python/tools
|
||||
[Python track tests page]: https://exercism.org/docs/tracks/python/tests
|
||||
[pytest-cache]:http://pythonhosted.org/pytest-cache/
|
||||
[pytest-subtests]:https://github.com/pytest-dev/pytest-subtests
|
||||
[pytest.ini]: https://github.com/exercism/python/blob/main/pytest.ini
|
||||
[pytest: configuration file formats]: https://docs.pytest.org/en/6.2.x/customize.html#configuration-file-formats
|
||||
[pytest: marking test functions with attributes]: https://docs.pytest.org/en/6.2.x/mark.html#raising-errors-on-unknown-marks
|
||||
[pytest: working with custom markers]: https://docs.pytest.org/en/6.2.x/example/markers.html#working-with-custom-markers
|
||||
|
||||
## 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)
|
||||
- The [Python track's programming category on the forum](https://forum.exercism.org/c/programming/python)
|
||||
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||
- 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.
|
||||
- [The Exercism Community on Discord](https://exercism.org/r/discord)
|
||||
- [Python Community on Discord](https://pythondiscord.com/) is a very helpful and active community.
|
||||
- [/r/learnpython/](https://www.reddit.com/r/learnpython/) is a subreddit designed for Python learners.
|
||||
- [#python on Libera.chat](https://www.python.org/community/irc/) this is where the core developers for the language hang out and get work done.
|
||||
- [Python Community Forums](https://discuss.python.org/)
|
||||
- [Free Code Camp Community Forums](https://forum.freecodecamp.org/)
|
||||
- [CodeNewbie Community Help Tag](https://community.codenewbie.org/t/help)
|
||||
- [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.
|
||||
@@ -0,0 +1,26 @@
|
||||
# Hints
|
||||
|
||||
## General
|
||||
|
||||
- For an overview, this section of the Python documentation: [Truth Value Testing][stdlib-bools] might help.
|
||||
- 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] [operators][Boolean-operators] to combine arguments for a result.
|
||||
|
||||
## 3. Define if Pac-Man loses
|
||||
|
||||
- You can use the [boolean][Boolean] [operators][Boolean-operators] to combine arguments for a result.
|
||||
|
||||
## 4. Define if Pac-Man wins
|
||||
|
||||
- You can use the [Boolean][boolean] [operators][Boolean-operators] to combine arguments for a result.
|
||||
|
||||
[Boolean-operators]: https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not
|
||||
[boolean]: https://docs.python.org/3/library/stdtypes.html#truth
|
||||
[stdlib-bools]: https://docs.python.org/3/library/stdtypes.html#truth-value-testing
|
||||
@@ -0,0 +1,96 @@
|
||||
# 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`][bools] type, which is a subtype of `int`.
|
||||
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
|
||||
```
|
||||
|
||||
[bools]: https://docs.python.org/3/library/stdtypes.html#typebool
|
||||
|
||||
## 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 a 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
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,42 @@
|
||||
"""Functions for implementing the rules of the classic arcade game Pac-Man."""
|
||||
|
||||
|
||||
def eat_ghost(power_pellet_active: bool, touching_ghost: bool) -> bool:
|
||||
"""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: bool, touching_dot: bool) -> bool:
|
||||
"""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) -> bool:
|
||||
"""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 not power_pellet_active and touching_ghost
|
||||
|
||||
|
||||
def win(has_eaten_all_dots, power_pellet_active, touching_ghost) -> bool:
|
||||
"""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 lose(power_pellet_active, touching_ghost)
|
||||
@@ -0,0 +1,144 @@
|
||||
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):
|
||||
actual_result = eat_ghost(True, True)
|
||||
error_message = ('Called eat_ghost(True, True).'
|
||||
f'The function returned {actual_result}, but the '
|
||||
f'tests expected that the ghost gets eaten (True).')
|
||||
|
||||
self.assertIs(actual_result, True, msg=error_message)
|
||||
|
||||
@pytest.mark.task(taskno=1)
|
||||
def test_ghost_does_not_get_eaten_because_no_power_pellet_active(self):
|
||||
actual_result = eat_ghost(False, True)
|
||||
error_message = ('Called eat_ghost(False, True).'
|
||||
f'The function returned {actual_result}, but the '
|
||||
f'tests expected that the '
|
||||
'ghost **does not** get eaten because '
|
||||
'no power pellet was active.')
|
||||
|
||||
self.assertIs(actual_result, False, msg=error_message)
|
||||
|
||||
@pytest.mark.task(taskno=1)
|
||||
def test_ghost_does_not_get_eaten_because_not_touching_ghost(self):
|
||||
actual_result = eat_ghost(True, False)
|
||||
error_message = ('Called eat_ghost(True, False).'
|
||||
f'The function returned {actual_result}, but the '
|
||||
f'tests expected that the '
|
||||
'ghost **does not** get eaten because '
|
||||
'the player was not touching the ghost.')
|
||||
|
||||
self.assertIs(actual_result, False, msg=error_message)
|
||||
|
||||
@pytest.mark.task(taskno=2)
|
||||
def test_score_when_eating_dot(self):
|
||||
actual_result = score(False, True)
|
||||
error_message = ('Called score(False, True).'
|
||||
f'The function returned {actual_result}, but the '
|
||||
f'tests expected that the '
|
||||
'player scores because they were touching a dot.')
|
||||
|
||||
self.assertIs(actual_result, True, msg=error_message)
|
||||
|
||||
@pytest.mark.task(taskno=2)
|
||||
def test_score_when_eating_power_pellet(self):
|
||||
actual_result = score(True, False)
|
||||
error_message = ('Called score(True, False).'
|
||||
f'The function returned {actual_result}, but the '
|
||||
f'tests expected that the '
|
||||
'player scores because they '
|
||||
'were touching a power pellet.')
|
||||
|
||||
self.assertIs(actual_result,True,msg=error_message)
|
||||
|
||||
@pytest.mark.task(taskno=2)
|
||||
def test_no_score_when_nothing_eaten(self):
|
||||
actual_result = score(False, False)
|
||||
error_message = ('Called score(False, False).'
|
||||
f'The function returned {actual_result}, but the '
|
||||
f'tests expected that the '
|
||||
'player **does not** score because they '
|
||||
'were not touching anything.')
|
||||
self.assertIs(actual_result, False,msg=error_message)
|
||||
|
||||
@pytest.mark.task(taskno=3)
|
||||
def test_lose_if_touching_a_ghost_without_a_power_pellet_active(self):
|
||||
actual_result = lose(False, True)
|
||||
error_message = ('Called lose(False, True).'
|
||||
f'The function returned {actual_result}, but the '
|
||||
f'tests expected that the '
|
||||
'player loses because they touched a '
|
||||
'ghost without a power pellet activated.')
|
||||
self.assertIs(
|
||||
actual_result, True, msg=error_message)
|
||||
|
||||
@pytest.mark.task(taskno=3)
|
||||
def test_dont_lose_if_touching_a_ghost_with_a_power_pellet_active(self):
|
||||
actual_result = lose(True, True)
|
||||
error_message = ('Called lose(True, True).'
|
||||
f'The function returned {actual_result}, but the '
|
||||
f'tests expected that the '
|
||||
'player **does not** lose because when they touched a '
|
||||
'ghost, a power pellet was active.')
|
||||
|
||||
self.assertIs(actual_result, False, msg=error_message)
|
||||
|
||||
@pytest.mark.task(taskno=3)
|
||||
def test_dont_lose_if_not_touching_a_ghost(self):
|
||||
actual_result = lose(True, False)
|
||||
error_message = ('Called lose(True, False).'
|
||||
f'The function returned {actual_result}, but the '
|
||||
f'tests expected that the '
|
||||
'player **does not** lose because they were '
|
||||
'not touching a ghost.')
|
||||
|
||||
self.assertIs(actual_result, False, msg=error_message)
|
||||
|
||||
@pytest.mark.task(taskno=4)
|
||||
def test_win_if_all_dots_eaten(self):
|
||||
actual_result = win(True, False, False)
|
||||
error_message = ('Called win(True, False, False).'
|
||||
f'The function returned {actual_result}, but the '
|
||||
f'tests expected that the '
|
||||
'player wins because all the dots were eaten.')
|
||||
|
||||
self.assertIs(actual_result, True, msg=error_message)
|
||||
|
||||
@pytest.mark.task(taskno=4)
|
||||
def test_dont_win_if_all_dots_eaten_but_touching_a_ghost(self):
|
||||
actual_result = win(True, False, True)
|
||||
error_message = ('Called win(True, False, True).'
|
||||
f'The function returned {actual_result}, but the '
|
||||
f'tests expected that the '
|
||||
'player **does not** win, because '
|
||||
'the player was touching a ghost.')
|
||||
|
||||
self.assertIs(actual_result, False, msg=error_message)
|
||||
|
||||
@pytest.mark.task(taskno=4)
|
||||
def test_win_if_all_dots_eaten_and_touching_a_ghost_with_a_power_pellet_active(self):
|
||||
actual_result = win(True, True, True)
|
||||
error_message = ('Called win(True, True, True).'
|
||||
f'The function returned {actual_result}, but the '
|
||||
f'tests expected that the player wins, '
|
||||
f'because a power pellet was active when they '
|
||||
f'touched a ghost.')
|
||||
|
||||
self.assertIs(actual_result, True, msg=error_message)
|
||||
|
||||
@pytest.mark.task(taskno=4)
|
||||
def test_dont_win_if_not_all_dots_eaten(self):
|
||||
actual_result = win(False, True, True)
|
||||
error_message = ('Called win(False, True, True).'
|
||||
f'The function returned {actual_result}, but the '
|
||||
f'tests expected that the player **does not** win, '
|
||||
f'because the player did not eat all of the dots.')
|
||||
|
||||
self.assertIs(actual_result, False, msg=error_message)
|
||||
|
||||
Reference in New Issue
Block a user