Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianbernhart committed Jul 3, 2024
0 parents commit 56120be
Show file tree
Hide file tree
Showing 10 changed files with 491 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/python
{
"name": "Python 3",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/python:1-3.11-bullseye",
"features": {
"ghcr.io/devcontainers-contrib/features/poetry:2": {}
}

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "pip3 install --user -r requirements.txt",

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for more information:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
# https://containers.dev/guide/dependabot

version: 2
updates:
- package-ecosystem: "devcontainers"
directory: "/"
schedule:
interval: weekly
37 changes: 37 additions & 0 deletions .github/workflows/rock-paper-siccors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: CI
on:
push:
branches:
- master
pull_request:

jobs:
setup:
runs-on: ubuntu-latest
defaults:
run:
working-directory: src/machine-learning-with-python/rock-paper-scissors
steps:
- uses: actions/checkout@v4
- name: Install Python
uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Install poetry
with:
poetry-version: latest
uses: abatilo/actions-poetry@v2
- name: Setup a local virtual environment (if no poetry.toml file)
run: |
poetry config virtualenvs.create true --local
poetry config virtualenvs.in-project true --local
- uses: actions/cache@v3
name: Define a cache for the virtual environment based on the dependencies lock file
with:
path: ./.venv
key: venv-${{ hashFiles('poetry.lock') }}
- name: Install the project dependencies
run: poetry install
- name: run pytest
run: poetry run pytest -v

124 changes: 124 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# Distribution / packaging
.Python
env/
venv/
ENV/
env.bak/
venv.bak/
.build/
dist/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# Poetry specific
poetry.lock
.cache/
.pdm.toml
.pdm-python/
.pdm.lock

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
nosetests.xml
coverage/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.env.*
.venv
.venv/
.env/
env/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# Profiling data
.prof

# pytype static type analyzer
.pytype/

# PyCharm
.idea/

# Visual Studio Code
.vscode/

# Local history for IntelliJ-based IDEs
.idea

# macOS
.DS_Store

# Windows
Thumbs.db
ehthumbs.db

# Windows 10 upgrade
$WINDOWS.~BT/
$RECYCLE.BIN/

# Windows 10 set-up files
*.cab
*.msu
*.regtrans-ms
*.jrs
*.apx

# Linux
*.lock
*~

# Logs
logs/
*.log

# Temp files
*.tmp
*.temp
tmp/
temp/
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
### Future Plans

- [ ] Do it in a better version with sklearn.


### Assignment

For this challenge, you will create a program to play Rock, Paper, Scissors. A program that picks at random will usually win 50% of the time. To pass this challenge your program must play matches against four different bots, winning at least 60% of the games in each match.

In the file `RPS.py` you are provided with a function called `player`. The function takes an argument that is a string describing the last move of the opponent ("R", "P", or "S"). The function should return a string representing the next move for it to play ("R", "P", or "S").

A player function will receive an empty string as an argument for the first game in a match since there is no previous play.

The file `RPS.py` shows an example function that you will need to update. The example function is defined with two arguments (`player(prev_play, opponent_history = [])`). The function is never called with a second argument so that one is completely optional. The reason why the example function contains a second argument (`opponent_history = []`) is because that is the only way to save state between consecutive calls of the `player` function. You only need the `opponent_history` argument if you want to keep track of the opponent_history.

*Hint: To defeat all four opponents, your program may need to have multiple strategies that change depending on the plays of the opponent.*

### Development

Do not modify `RPS_game.py`. Write all your code in `RPS.py`. For development, you can use `main.py` to test your code.

`main.py` imports the game function and bots from `RPS_game.py`.

To test your code, play a game with the `play` function. The `play` function takes four arguments:
- two players to play against each other (the players are actually functions)
- the number of games to play in the match
- an optional argument to see a log of each game. Set it to `True` to see these messages.

```py
play(player1, player2, num_games[, verbose])
```
For example, here is how you would call the function if you want `player` and `quincy` to play 1000 games against each other and you want to see the results of each game:
```py
play(player, quincy, 1000, verbose=True)
```

Click the "run" button and `main.py` will run.

### Testing

The unit tests for this project are in `test_module.py`. We imported the tests from `test_module.py` to `main.py` for your convenience. If you uncomment the last line in `main.py`, the tests will run automatically whenever you hit the "run" button.

### Submitting

Copy your project's URL and submit it to freeCodeCamp.
45 changes: 45 additions & 0 deletions RPS.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
def player(prev_play, opponent_history=[]):
opponent_history.append(prev_play)
guess: str = 'S'
if not prev_play:
opponent_history.clear()
return guess

rock_count: int = 0
paper_count: int = 0
scissors_count: int = 0
for i in range(len(opponent_history) - 1):
if opponent_history[i] == prev_play:
next = opponent_history[i + 1]
if next == 'R':
rock_count += 1
if next == 'P':
paper_count += 1
if next == 'S':
scissors_count += 1

rock_guess: int = scissors_count
paper_guess: int = rock_count
scissors_guess: int = paper_count

max_guess = max(rock_guess, paper_guess, scissors_guess)
if max_guess == rock_guess:
guess = 'R'
if max_guess == paper_guess:
guess = 'P'
if max_guess == scissors_guess:
guess = 'S'

big_brain = {
'P': 'R',
'R': 'S',
'S': 'P',
}
if len(opponent_history) > 2:
if opponent_history[-1] == opponent_history[-2] != opponent_history[-3]:
guess = big_brain[prev_play]

if rock_count == paper_count == scissors_count == 0:
guess = prev_play

return guess
Loading

0 comments on commit 56120be

Please sign in to comment.