Skip to content

Commit

Permalink
Floor class
Browse files Browse the repository at this point in the history
Floor class

Create pylint.yml

Fix formatting

Remove lint

Create python-app.yml

Delete unused stuff
  • Loading branch information
relatko committed Oct 26, 2023
1 parent 149d057 commit adaf75b
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python application

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install mypy
- name: Test and checks
run: |
make
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.mypy_cache
__pycache__
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test: FORCE
mypy azul --strict
mypy test --strict
python3 -m unittest

FORCE: ;
32 changes: 32 additions & 0 deletions azul/Floor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from __future__ import annotations
from azul.UsedTilesGiveInterface import UsedTilesGiveInterface
from azul.Tile import Tile, compressTileList
from azul.Points import Points
from typing import List
from itertools import chain, repeat, islice


class Floor:
_pointPattern: List[Points]
_usedTiles: UsedTilesGiveInterface
_tiles: List[Tile]

def __init__(self, pointPattern: List[Points], _usedTiles: UsedTilesGiveInterface):
self._pointPattern = pointPattern.copy()
self._usedTiles = _usedTiles
self._tiles = []

def put(self, tiles: List[Tile]) -> None:
self._tiles.extend(tiles)

def finishRound(self) -> Points:
extendedPattern = chain(
self._pointPattern, repeat(self._pointPattern[-1]))
points: Points = Points.sum(
list(islice(extendedPattern, 0, len(self._tiles))))
self._usedTiles.give(self._tiles.copy())
self._tiles = []
return points

def state(self) -> str:
return compressTileList(self._tiles)
16 changes: 16 additions & 0 deletions azul/Points.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import annotations
from typing import List


class Points:
_value: int

def __init__(self, value: int):
self._value = value

@staticmethod
def sum(pointsList: List[Points]) -> Points:
return Points(sum([x._value for x in pointsList]))

def __str__(self) -> str:
return str(self._value)
25 changes: 25 additions & 0 deletions azul/Tile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from __future__ import annotations
from enum import Enum
from typing import List


class Tile:
_representation: str

def __init__(self, representation: str):
self._representation = representation

def __str__(self) -> str:
return self._representation


STARTING_PLAYER: Tile = Tile("S")
RED: Tile = Tile("R")
BLUE: Tile = Tile("B")
YELLOW: Tile = Tile("Y")
GREEN: Tile = Tile("G")
BLACK: Tile = Tile("L")


def compressTileList(tiles: List[Tile]) -> str:
return "".join([str(x) for x in tiles])
8 changes: 8 additions & 0 deletions azul/UsedTilesGiveInterface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import annotations
from azul.Tile import Tile
from typing import List


class UsedTilesGiveInterface:
def give(self, tiles: List[Tile]) -> None:
pass
Empty file added azul/__init__.py
Empty file.
Empty file added test/__init__.py
Empty file.
35 changes: 35 additions & 0 deletions test/test_floor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from __future__ import annotations
import unittest
from azul.UsedTilesGiveInterface import UsedTilesGiveInterface
from azul.Tile import Tile, STARTING_PLAYER, RED, GREEN
from azul.Floor import Floor
from azul.Points import Points
from typing import List


class FakeUsedTiles(UsedTilesGiveInterface):
tilesGiven: List[Tile]

def __init__(self) -> None:
self.tilesGiven = []

def give(self, tiles: List[Tile]) -> None:
self.tilesGiven.extend(tiles)


class TestFloor(unittest.TestCase):
def setUp(self) -> None:
self.usedTiles: FakeUsedTiles = FakeUsedTiles()
self.floor: Floor = Floor(
[Points(1), Points(2), Points(2)], self.usedTiles)

def test_many_tiles(self) -> None:
tiles = [STARTING_PLAYER, RED, GREEN, RED]
self.floor.put(tiles)
points: Points = self.floor.finishRound()
self.assertEquals(str(points), "7")
self.assertCountEqual(tiles, self.usedTiles.tilesGiven)


if __name__ == '__main__':
unittest.main()

0 comments on commit adaf75b

Please sign in to comment.