diff options
-rw-r--r-- | app.py | 33 | ||||
-rw-r--r-- | board/board.py | 57 | ||||
-rw-r--r-- | tui/tui.py | 31 |
3 files changed, 82 insertions, 39 deletions
@@ -1,5 +1,5 @@ from board.board import Board -from tui.tui import Tui +from tui.tui import Tui, Action b = Board(10) t = Tui() @@ -9,17 +9,20 @@ for _ in range(b.size): print() while True: - x, y = t.matrix_selector(b.get_view(), x, y) - if x == -1: - t.draw_matrix(b.get_view(),-1,-1) - print("LOPETUS!") - break - 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 - + action, x, y = t.matrix_selector(b.get_view(), x, y) + match action: + case Action.QUIT: +# 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) + print("KUOLEMA!") + break + elif b.is_winning(): + 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 diff --git a/board/board.py b/board/board.py index 2d3fa1f..dbec117 100644 --- a/board/board.py +++ b/board/board.py @@ -21,10 +21,12 @@ class Board(): self.randomize_bombs( bombs ) self.calculate_neighbours() + def initialize_tiles(self, size): 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): for _ in range(bomb_count): while True: @@ -34,9 +36,23 @@ class Board(): self.tiles[x][y]=9 break + + def calculate_neighbours(self): + for y in range(self.size): + for x in range(self.size): + if self.tiles[x][y] == 9: + continue + neighbouring_bombs = 0 + for nx, ny in self.get_neighbours_coords(x,y): + if self.tiles[nx][ny] == 9: + neighbouring_bombs += 1 + self.tiles[x][y] = neighbouring_bombs + + def invalid_coordinates(self, x, y): return x < 0 or x >= self.size or y < 0 or y >= self.size + def get_neighbours_coords(self, x, y, include_home = False): offsets = ( (-1,-1), (0,-1), (1,-1), @@ -53,17 +69,7 @@ class Board(): coordinates.append( (x+dx, y+dy) ) return coordinates - def calculate_neighbours(self): - for y in range(self.size): - for x in range(self.size): - if self.tiles[x][y] == 9: - continue - neighbouring_bombs = 0 - for nx, ny in self.get_neighbours_coords(x,y): - if self.tiles[nx][ny] == 9: - neighbouring_bombs += 1 - self.tiles[x][y] = neighbouring_bombs - + def get_view(self): view = deepcopy(self.masked) for y in range(self.size): @@ -72,6 +78,7 @@ class Board(): view[x][y]=self.tiles[x][y] return view + def is_winning(self): for y in range(self.size): for x in range(self.size): @@ -79,7 +86,7 @@ class Board(): return False return True - + def collect_area(self, x, y, area=set()): if not area: area.add((x,y)) @@ -92,8 +99,34 @@ class Board(): area=area.union(self.collect_area(tx, ty, area)) return area + def get_mask(self, x, y): return self.masked[x][y] + + + def flag_tile(self, x, y): + 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) + return False + case 10 | 11: + self.masked[x][y]+=1 + return True + case 12: + self.masked[x][y]=10 + return True + + print("Ruudulla odottamaton lippu", file=stderr) + return False + def make_guess(self, x, y): if self.invalid_coordinates(x, y): @@ -1,4 +1,10 @@ import termios, fcntl, sys, os +from enum import Enum + +class Action(Enum): + QUIT = 0 + OPEN = 1 + FLAG = 2 class Tui(): def __init__(self): @@ -40,7 +46,8 @@ class Tui(): (' ', 7, 0), ('1', 10, 0), ('2', 11, 0), ('3', 13, 0), ('4', 9, 0), ('5', 9, 0), ('6', 9, 0), ('7', 9, 0), ('8', 9, 0), - ('¤', 15, 1), ('#', 8, 7) + ('¤', 15, 1), ('#', 8, 7), ('B', 8, 7), + ('?', 8, 7) ) if hilighted: @@ -61,7 +68,7 @@ class Tui(): x == hx and y == hy ) print() - def matrix_selector(self, matrix, x,y ): + def matrix_selector(self, matrix, x, y ): self.draw_matrix(matrix, x, y) while True: try: @@ -69,20 +76,20 @@ class Tui(): except: continue match c: - case 'A': + case 'A' | 'w': y-=1 - case 'B': + case 'D' | 'a': + x-=1 + case 'B' | 's': y+=1 - case 'C': + case 'C' | 'd': x+=1 - case 'D': - x-=1 - case '': - continue case 'q': - return (-1,-1) - case ' ': - return (x,y) + return (Action.QUIT,-1,-1) + case ' ' | '\n': + return (Action.OPEN, x, y) + case 'f' | 'm': + return (Action.FLAG, x, y) case _: continue x = 0 if x < 0 else x |