From 27057a601c43ab5a6dcaa5b396c6b3d891c921b9 Mon Sep 17 00:00:00 2001 From: Aineopintojen-harjoitustyo-Algoritmit-j Date: Thu, 25 Jan 2024 15:52:28 +0200 Subject: Sorting out import situation and making enum names for tiles for readability. --- board/__init__.py | 4 ++++ board/board.py | 37 ++++++++++++------------------------- board/static.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 25 deletions(-) create mode 100644 board/__init__.py create mode 100644 board/static.py (limited to 'board') diff --git a/board/__init__.py b/board/__init__.py new file mode 100644 index 0000000..ba87812 --- /dev/null +++ b/board/__init__.py @@ -0,0 +1,4 @@ +""" board - tämä alimoduli hoitaa pelilaudan käsittelyn sääntöjen mukaan """ + +from .board import Board +from .static import Level, LevelSpecs, Tile diff --git a/board/board.py b/board/board.py index 00dcd10..625e670 100644 --- a/board/board.py +++ b/board/board.py @@ -2,21 +2,8 @@ from random import randrange from sys import stderr from copy import deepcopy -from enum import Enum - -class Level(Enum): - """ vaikeustasot """ - BEGINNER = 0 - INTERMEDIATE = 1 - EXPERT = 2 - - -LevelSpecs = { - Level.BEGINNER: ( 9, 9, 10 ), - Level.INTERMEDIATE: ( 15, 15, 40 ), - Level.EXPERT: ( 30, 16, 99 ) -} +from .static import Level, Tile, LevelSpecs class Board(): @@ -50,8 +37,8 @@ class Board(): def __initialize_tiles(self): """ alustaa pelilaudan matriisit """ w, h = self.__width, self.__height - self.__tiles = [[0 for _ in range(h)] for _ in range(w)] - self.__masked = [[12 for _ in range(h)] for _ in range(w)] + self.__tiles = [[Tile.BLANK for _ in range(h)] for _ in range(w)] + self.__masked = [[Tile.UNOPENED for _ in range(h)] for _ in range(w)] def __randomize_bombs(self): @@ -59,9 +46,9 @@ class Board(): for _ in range(self.__bombs): while True: x, y = randrange(0,self.__width), randrange(0,self.__height) - if self.__tiles[x][y] != 0: + if self.__tiles[x][y] != Tile.BLANK: continue - self.__tiles[x][y]=9 + self.__tiles[x][y] = Tile.BOMB break @@ -69,11 +56,11 @@ class Board(): """ laskee naapurissa olevien pommien määrät valmiiksi laudalle """ for y in range(self.__height): for x in range(self.__width): - if self.__tiles[x][y] == 9: + if self.__tiles[x][y] == Tile.BOMB: continue neighbouring_bombs = 0 for nx, ny in self.get_neighbours_coords(x,y): - if self.__tiles[nx][ny] == 9: + if self.__tiles[nx][ny] == Tile.BOMB: neighbouring_bombs += 1 self.__tiles[x][y] = neighbouring_bombs @@ -115,7 +102,7 @@ class Board(): """ tarkistaa onko peli voitettu """ for y in range(self.__height): for x in range(self.__width): - if self.__tiles[x][y] == 9: + if self.__tiles[x][y] == Tile.BOMB: if not self.__masked[x][y]: return False else: @@ -130,7 +117,7 @@ class Board(): 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: + if self.__tiles[nx][ny] == Tile.BLANK and (nx,ny) not in area: to_test.append((nx, ny)) area.add((nx, ny)) for tx, ty in to_test: @@ -171,16 +158,16 @@ class Board(): print("Koordinaatit on pelilaudan ulkopuolella", file=stderr) return False - if self.__masked[x][y] == 0: + if not self.__masked[x][y]: print("Ruutu on jo avattu", file=stderr) return False self.__masked[x][y] = 0 - if self.__tiles[x][y] == 9: + if self.__tiles[x][y] == Tile.BOMB: return False - if self.__tiles[x][y] == 0: + if self.__tiles[x][y] == Tile.BLANK: 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 diff --git a/board/static.py b/board/static.py new file mode 100644 index 0000000..eea30fd --- /dev/null +++ b/board/static.py @@ -0,0 +1,34 @@ +""" board/static.py - määrittelyjä pelilaudan muuttumattomille asoille """ + +from enum import Enum, IntEnum + +class Level(Enum): + """ vaikeustasot """ + BEGINNER = 0 + INTERMEDIATE = 1 + EXPERT = 2 + + +class Tile(IntEnum): + """ alueiden selitteet """ + BLANK = 0 + ONE = 1 + TWO = 2 + THREE = 3 + FOUR = 4 + FIVE = 5 + SIX = 6 + SEVEN = 7 + EIGHT = 8 + BOMB = 9 + FLAG_BOMB = 10 + FLAG_FREE = 11 + UNOPENED = 12 + FLAG_UNKNOWN = 13 + + +LevelSpecs = { + Level.BEGINNER: ( 9, 9, 10 ), + Level.INTERMEDIATE: ( 15, 15, 40 ), + Level.EXPERT: ( 30, 16, 99 ) +} -- cgit v1.2.3