summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi>2024-01-13 17:43:17 +0200
committerAineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi>2024-01-13 17:43:17 +0200
commit227870a64f1eacbc13950256a05a2fab5c8c2a25 (patch)
tree1c3cae99e4c16fa8ae2fca6073a38bbf11104ac3
parent9f7086628c58392b7bdc45f9f0994bfbd64b99c6 (diff)
Linting and docstringing board/board.py
-rw-r--r--app.py10
-rw-r--r--board/board.py74
-rw-r--r--pyproject.toml8
-rw-r--r--tui/tui.py44
4 files changed, 68 insertions, 68 deletions
diff --git a/app.py b/app.py
index 25dedeb..2e2f538 100644
--- a/app.py
+++ b/app.py
@@ -12,17 +12,17 @@ while True:
action, x, y = t.matrix_selector(b.get_view(), x, y)
match action:
case Action.QUIT:
-# t.draw_matrix(b.get_view(),-1,-1)
+ # t.draw_matrix(b.get_view(),-1,-1)
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)
+ if b.get_mask(x, y) and not b.make_guess(x, y):
+ t.draw_matrix(b.get_view(), -1, -1)
print("KUOLEMA!")
break
elif b.is_winning():
- t.draw_matrix(b.get_view(),-1,-1)
+ t.draw_matrix(b.get_view(), -1, -1)
print("VOITTO!")
break
case Action.FLAG:
- b.flag_tile(x,y) \ No newline at end of file
+ b.flag_tile(x, y)
diff --git a/board/board.py b/board/board.py
index dbec117..069b568 100644
--- a/board/board.py
+++ b/board/board.py
@@ -1,14 +1,18 @@
+""" board/board.py - Pelilaudan käsittelyyn tarkoitetut jutut """
from random import randrange
from sys import stderr
from copy import deepcopy
class Board():
+ """ Board - Luokka joka pitää huolen pelilaudasta ja siihen kohdistuvista
+ siirroista.
+ """
def __init__(self, size = 10, bombs = 0):
# Lauta pitää olla vähintään 2x2, jotta on jotain pelattavaa
size = 2 if size < 2 else size
size = 50 if size > 50 else size
self.size = size
-
+
# Pommeja pitää olla vähintään yksi, kuten tyhjiäkin
bombs = size*size*size//100 if bombs < 1 else bombs
bombs = size*size-1 if bombs>=size*size else bombs
@@ -20,14 +24,16 @@ class Board():
self.initialize_tiles( size )
self.randomize_bombs( bombs )
self.calculate_neighbours()
-
+
def initialize_tiles(self, size):
+ """ alustaa pelilaudan matriisit """
self.tiles = [[0 for _ in range(size)] for _ in range(size)]
self.masked = [[10 for _ in range(size)] for _ in range(size)]
-
+
def randomize_bombs(self, bomb_count):
+ """ arpoo pelilaudalle pommit """
for _ in range(bomb_count):
while True:
x, y = randrange(0,self.size), randrange(0,self.size)
@@ -35,9 +41,10 @@ class Board():
continue
self.tiles[x][y]=9
break
-
+
def calculate_neighbours(self):
+ """ laskee naapurissa olevien pommien määrät valmiiksi laudalle """
for y in range(self.size):
for x in range(self.size):
if self.tiles[x][y] == 9:
@@ -47,13 +54,15 @@ class Board():
if self.tiles[nx][ny] == 9:
neighbouring_bombs += 1
self.tiles[x][y] = neighbouring_bombs
-
+
def invalid_coordinates(self, x, y):
+ """ onko koordinaatit pelilaudan ulkopuolella """
return x < 0 or x >= self.size or y < 0 or y >= self.size
-
+
def get_neighbours_coords(self, x, y, include_home = False):
+ """ antaa listan naapureiden koordinaateista """
offsets = (
(-1,-1), (0,-1), (1,-1),
(-1, 0), (0, 0), (1, 0),
@@ -68,28 +77,31 @@ class Board():
if not self.invalid_coordinates(x+dx, y+dy):
coordinates.append( (x+dx, y+dy) )
return coordinates
-
+
def get_view(self):
+ """ antaa matriisin nykyisestä pelinäkymästä """
view = deepcopy(self.masked)
for y in range(self.size):
for x in range(self.size):
if not view[x][y]:
view[x][y]=self.tiles[x][y]
return view
-
+
def is_winning(self):
+ """ tarkistaa onko peli voitettu """
for y in range(self.size):
for x in range(self.size):
if self.tiles[x][y] != 9 and self.masked[x][y]:
return False
return True
-
- def collect_area(self, x, y, area=set()):
- if not area:
- area.add((x,y))
+
+ def collect_area(self, x, y, area = None):
+ """ tunnustelee ja palauttaa tyhjän alueen koordinaatit """
+ if area is None:
+ area = {(x,y)}
to_test = []
for nx, ny in self.get_neighbours_coords(x, y):
if self.tiles[nx][ny] == 0 and (nx,ny) not in area:
@@ -98,21 +110,23 @@ class Board():
for tx, ty in to_test:
area=area.union(self.collect_area(tx, ty, area))
return area
-
+
def get_mask(self, x, y):
+ """ onko ruutu vielä piilossa """
return self.masked[x][y]
def flag_tile(self, x, y):
+ """ aseta lippu peitetylle ruudulle"""
if self.invalid_coordinates(x, y):
print("Koordinaatit on pelilaudan ulkopuolella", file=stderr)
return False
-
+
if self.masked[x][y] == 0:
print("Ei voi liputtaa avattua ruutua", file=stderr)
return False
-
+
match self.masked[x][y]:
case 0:
print("Ei voi liputtaa avattua ruutua", file=stderr)
@@ -123,43 +137,29 @@ class Board():
case 12:
self.masked[x][y]=10
return True
-
+
print("Ruudulla odottamaton lippu", file=stderr)
return False
-
+
def make_guess(self, x, y):
+ """ tee arvaus """
if self.invalid_coordinates(x, y):
print("Koordinaatit on pelilaudan ulkopuolella", file=stderr)
return False
-
+
if self.masked[x][y] == 0:
print("Ruutu on jo avattu", file=stderr)
return False
-
+
self.masked[x][y] = 0
-
+
if self.tiles[x][y] == 9:
return False
-
+
if self.tiles[x][y] == 0:
for cx, cy in self.collect_area( x, y ):
for nx, ny in self.get_neighbours_coords(cx, cy, True):
self.masked[nx][ny] = 0
-
+
return True
-
-
-
-if __name__ == "__main__":
- def print_matrix(m):
- for y in range(len(m)):
- for x in range(len(m[0])):
- print(end=f"[{m[x][y]:x}]")
- print()
-
- b = Board()
- b.make_guess(5,5)
- print_matrix(b.get_view())
-
- \ No newline at end of file
diff --git a/pyproject.toml b/pyproject.toml
index 90d7851..a471c2b 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -12,7 +12,15 @@ python = "^3.10"
[tool.poetry.group.dev.dependencies]
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 = "./"
+
+[tool.pylint.basic]
+docstring-min-length = 0
diff --git a/tui/tui.py b/tui/tui.py
index 1262825..66cc687 100644
--- a/tui/tui.py
+++ b/tui/tui.py
@@ -2,6 +2,7 @@ import termios, fcntl, sys, os
from time import sleep
from tui.static import Action, ActionKeys, ActionEscKeys, Colors, TileTypes
+
class Tui():
def __init__(self):
# Vaatii hieman terminaaliasetusten muokkaamista jotta yksittäiset
@@ -9,39 +10,33 @@ class Tui():
# https://stackoverflow.com/questions/983354/how-do-i-wait-for-a-pressed-key
fd = sys.stdin.fileno()
self.oldterm = termios.tcgetattr(fd)
-
+
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
-
+
self.oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, self.oldflags | os.O_NONBLOCK)
-
def __del__(self):
# palautetaan terminaali takaisin alkupetäiseen uskoon
fd = sys.stdin.fileno()
termios.tcsetattr(fd, termios.TCSAFLUSH, self.oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, self.oldflags)
-
def set_color(self, color):
- if color>=0 and color<16:
+ if color >= 0 and color < 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:
+ if color >= 0 and color < 8:
print(end=f"\033[4{color}m")
-
def cursor_up(self, lines):
print(end=f"\033[{lines}F")
-
def reset_color(self):
print(end="\033[0m")
-
def draw_tile(self, tile, hilighted):
for i in range(len(TileTypes[tile].text)):
@@ -50,23 +45,21 @@ class Tui():
self.set_bg(Colors.CYAN if hilighted else bg)
print(end=TileTypes[tile].text[i])
self.reset_color()
-
- def draw_matrix(self, matrix, hx, hy ):
+ def draw_matrix(self, matrix, hx, hy):
self.cursor_up(len(matrix[0]))
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):
escape = 0
while True:
try:
# Ehkä riittää jos näppäimiä luetaan 200x sekunnissa
- sleep(0.005)
+ sleep(0.005)
c = sys.stdin.read(1)
except:
continue
@@ -84,23 +77,22 @@ class Tui():
if c in ActionKeys:
return ActionKeys[c]
-
- def matrix_selector(self, matrix, x, y ):
+ def matrix_selector(self, matrix, x, y):
self.draw_matrix(matrix, x, y)
while True:
action = self.read_action()
match action:
case Action.QUIT:
- return (action,x,y)
+ return (action, x, y)
case Action.OPEN | Action.FLAG:
- if matrix[x][y]>=10:
- return (action,x,y)
+ if matrix[x][y] >= 10:
+ return (action, x, y)
case Action.UP:
- y = y-1 if y>0 else 0
+ y = y-1 if y > 0 else 0
case Action.LEFT:
- x = x-1 if x>0 else 0
+ x = x-1 if x > 0 else 0
case Action.DOWN:
- y = y+1 if y<len(matrix[0])-1 else y
+ y = y+1 if y < len(matrix[0])-1 else y
case Action.RIGHT:
- x = x+1 if x<len(matrix)-1 else x
+ x = x+1 if x < len(matrix)-1 else x
self.draw_matrix(matrix, x, y)