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 cc60f11
Show file tree
Hide file tree
Showing 10 changed files with 163 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__
2 changes: 2 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[MASTER]
disable = missing-module-docstring, missing-class-docstring, missing-function-docstring, too-few-public-methods
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test: FORCE
mypy azul --strict
mypy test --strict
python3 -m unittest

format: FORCE
autopep8 -i azul/*.py
autopep8 -i test/*.py
FORCE: ;
Empty file added azul/__init__.py
Empty file.
31 changes: 31 additions & 0 deletions azul/floor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from __future__ import annotations
from typing import List
from itertools import chain, repeat, islice
from azul.interfaces import UsedTilesGiveInterface
from azul.simple_types import Tile, compress_tile_list, Points


class Floor:
_point_pattern: List[Points]
_used_tiles: UsedTilesGiveInterface
_tiles: List[Tile]

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

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

def finish_round(self) -> Points:
extended_pattern = chain(
self._point_pattern, repeat(self._point_pattern[-1]))
points: Points = Points.sum(
list(islice(extended_pattern, 0, len(self._tiles))))
self._used_tiles.give(self._tiles.copy())
self._tiles = []
return points

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


class UsedTilesGiveInterface:
def give(self, tiles: List[Tile]) -> None:
pass
42 changes: 42 additions & 0 deletions azul/simple_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from __future__ import annotations
from typing import List


class Points:
_value: int

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

@property
def value(self) -> int:
return self._value

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

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


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 compress_tile_list(tiles: List[Tile]) -> str:
return "".join([str(x) for x in tiles])
Empty file added test/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions test/test_floor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from __future__ import annotations
import unittest
from typing import List
from azul.interfaces import UsedTilesGiveInterface
from azul.simple_types import Tile, STARTING_PLAYER, RED, GREEN, Points
from azul.floor import Floor


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

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

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


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

def test_many_tiles(self) -> None:
tiles = [STARTING_PLAYER, RED, GREEN, RED]
self.assertCountEqual(self.floor.state(), "")
self.floor.put(tiles)
self.assertCountEqual(self.floor.state(), "SRRG")
points: Points = self.floor.finish_round()
self.assertEqual(str(points), "7")
self.assertCountEqual(tiles, self.used_tiles.tiles_given)
self.assertCountEqual(self.floor.state(), "")


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

0 comments on commit cc60f11

Please sign in to comment.