diff options
-rw-r--r-- | .github/workflows/auto.yml | 5 | ||||
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | __init__.py | 0 | ||||
-rw-r--r-- | __main__.py | 5 | ||||
-rw-r--r-- | app.py | 51 | ||||
-rw-r--r-- | doc/muistilista.txt | 2 | ||||
-rw-r--r-- | pyproject.toml | 4 | ||||
-rw-r--r-- | tests/test_board.py | 15 | ||||
-rw-r--r-- | tests/test_noop.py | 2 | ||||
-rw-r--r-- | tui/tui.py | 41 |
10 files changed, 78 insertions, 51 deletions
diff --git a/.github/workflows/auto.yml b/.github/workflows/auto.yml index b360e1c..daa060d 100644 --- a/.github/workflows/auto.yml +++ b/.github/workflows/auto.yml @@ -46,4 +46,7 @@ jobs: uses: codecov/codecov-action@v3 env: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} - + + # pylint + - name: Run pylint against the code + run: poetry run python3 -m pylint -v . @@ -9,6 +9,4 @@ Miinaharava ratkaisijalla ## Ohjeet: `git clone https://github.com/Aineopintojen-harjoitustyo-Algoritmit-j/miinaharava` -`cd miinaharava` - -`python3 app.py` +`python3 miinaharava` diff --git a/__init__.py b/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/__init__.py +++ /dev/null diff --git a/__main__.py b/__main__.py new file mode 100644 index 0000000..0eceaa0 --- /dev/null +++ b/__main__.py @@ -0,0 +1,5 @@ +""" __main__.py - Tästä suoritus alkaa """ +from app import App + +app = App() +app.run() @@ -2,27 +2,34 @@ from board.board import Board from tui.tui import Tui, Action -b = Board(13) -t = Tui() -x, y = 0, 0 +# pylint: disable = too-few-public-methods +class App: + """ App - Luokka pääohjelmalle""" + def __init__(self): + self.b = Board(13) + self.t = Tui() -for _ in range(b.size): - print() + def run(self): + """ käynnistää pääohjelman """ + x, y = 0, 0 + # Printataan tyhjää tilaa, jotta pelalauta mahtuu ruudulle + for _ in range(self.b.size): + print() -while True: - action, x, y = t.matrix_selector(b.get_view(), x, y) - match action: - case Action.QUIT: - print("LOPETUS!") - break - case Action.OPEN: - if b.get_mask(x, y) and not b.make_guess(x, y): - t.draw_matrix(b.get_view(), -1, -1) - print("KUOLEMA!") - break - if b.is_winning(): - t.draw_matrix(b.get_view(), -1, -1) - print("VOITTO!") - break - case Action.FLAG: - b.flag_tile(x, y) + while True: + action, x, y = self.t.matrix_selector(self.b.get_view(), x, y) + match action: + case Action.QUIT: + print("LOPETUS!") + break + case Action.OPEN: + if self.b.get_mask(x, y) and not self.b.make_guess(x, y): + self.t.draw_matrix(self.b.get_view(), -1, -1) + print("KUOLEMA!") + break + if self.b.is_winning(): + self.t.draw_matrix(self.b.get_view(), -1, -1) + print("VOITTO!") + break + case Action.FLAG: + self.b.flag_tile(x, y) diff --git a/doc/muistilista.txt b/doc/muistilista.txt index e6b8a2b..19e5778 100644 --- a/doc/muistilista.txt +++ b/doc/muistilista.txt @@ -3,3 +3,5 @@ MUISTILISTA! Generoi haarakattavuusraportti: poetry run python3 -m coverage run --branch -m pytest && poetry run python3 -m coverage html && firefox htmlcov/index.html +Linttaus +poetry run python3 -m pylint -v .
\ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index a471c2b..b270154 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,14 +13,14 @@ python = "^3.10" pytest = "^7.4.4" coverage = "^7.4.0" pylint = "^3.0.3" -autopep8 = "^2.0.4" [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" [tool.pylint.main] -source-roots = "./" +recursive = true +source-roots = ["./"] [tool.pylint.basic] docstring-min-length = 0 diff --git a/tests/test_board.py b/tests/test_board.py index 8fa2fa3..9acc39f 100644 --- a/tests/test_board.py +++ b/tests/test_board.py @@ -1,16 +1,22 @@ +"""test_board.py - Testit pelilaudalle""" + import unittest from board.board import Board class TestBoardClass(unittest.TestCase): + """ pelilauden testit""" def test_init(self): + """ olion luominen onnistuu """ b = Board() self.assertTrue(b.size>0) - + def test_init_with_size(self): + """ olion luominen onnistuu tietyllä koolla""" b = Board(15) self.assertEqual(b.size, 15) - + def test_get_view_and_make_guess(self): + """ laudan näkymä on oikein senkin jälkeen kun on arvattu""" b = Board(3) b.tiles=[[0,0,0],[0,1,1],[0,1,9]] @@ -18,13 +24,12 @@ class TestBoardClass(unittest.TestCase): t = [[10,10,10],[10,10,10],[10,10,10]] for i in range(3): self.assertEqual(v[i],t[i]) - + self.assertTrue(b.make_guess(0,0)) v = b.get_view() t = [[0,0,0],[0,1,1],[0,1,10]] for i in range(3): self.assertEqual(v[i],t[i]) - + self.assertFalse(b.make_guess(2,2)) -
\ No newline at end of file diff --git a/tests/test_noop.py b/tests/test_noop.py deleted file mode 100644 index 61de3a2..0000000 --- a/tests/test_noop.py +++ /dev/null @@ -1,2 +0,0 @@ -def test_noop(): - pass @@ -1,9 +1,12 @@ +""" tui/tui.py - teksikäyttöliittymä """ +# pylint: disable = multiple-imports import termios, fcntl, sys, os from time import sleep from tui.static import Action, ActionKeys, ActionEscKeys, Colors, TileTypes class Tui(): + """ Tui - Luokka käyttäjän interaktiota varten """ def __init__(self): # Vaatii hieman terminaaliasetusten muokkaamista jotta yksittäiset # napin painallukset voidaan lukea @@ -25,44 +28,51 @@ class Tui(): fcntl.fcntl(fd, fcntl.F_SETFL, self.oldflags) def set_color(self, color): - if color >= 0 and color < 16: + """ asettaa tekstin värin """ + if color in range(16): print(end=f"\033[{'1;' if color//8 else ''}3{color%8}m") def set_bg(self, color): - if color >= 0 and color < 8: + """ asettaa tekstin taustan värin""" + if color in range(8): print(end=f"\033[4{color}m") def cursor_up(self, lines): + """ liikuttaa kursoria ylöspäin""" print(end=f"\033[{lines}F") def reset_color(self): + """ resetoi tekstin värin ja muut attribuutit perusarvoille """ print(end="\033[0m") def draw_tile(self, tile, hilighted): - for i in range(len(TileTypes[tile].text)): - color, bg = TileTypes[tile].colors[i] + """ "piirtää" yhden ruudun """ + for ch, colors in zip(TileTypes[tile].text, TileTypes[tile].colors): + color, bg = colors self.set_color(Colors.BLACK if hilighted else color) self.set_bg(Colors.CYAN if hilighted else bg) - print(end=TileTypes[tile].text[i]) + print(end=ch) self.reset_color() def draw_matrix(self, matrix, hx, hy): + """ "piirtää" ruudukon """ self.cursor_up(len(matrix[0])) + # pylint: disable=consider-using-enumerate for y in range(len(matrix[0])): for x in range(len(matrix)): - self.draw_tile(matrix[x][y], - x == hx and y == hy) + self.draw_tile(matrix[x][y], x == hx and y == hy) print() def read_action(self): + """ lukee näppäimistölä käyttäjän toiminnon """ escape = 0 while True: try: - # Ehkä riittää jos näppäimiä luetaan 200x sekunnissa + # Ehkä riittää jos näppäimiä luetaan 200x sekunnissa sleep(0.005) c = sys.stdin.read(1) - except: - continue + except KeyboardInterrupt: + return Action.QUIT if escape: if c in "[0123456789": continue @@ -70,14 +80,13 @@ class Tui(): return ActionEscKeys[c] escape = 0 continue - else: - if c == '\033': - escape = 1 - continue - if c in ActionKeys: - return ActionKeys[c] + if c in ActionKeys: + return ActionKeys[c] + if c == '\033': + escape = 1 def matrix_selector(self, matrix, x, y): + """ piirtää ruudukon ja antaa käyttäjän valita nuolinäppäimillä """ self.draw_matrix(matrix, x, y) while True: action = self.read_action() |