diff options
-rw-r--r-- | app.py | 9 | ||||
-rw-r--r-- | board/__init__.py | 4 | ||||
-rw-r--r-- | board/board.py | 37 | ||||
-rw-r--r-- | board/static.py | 34 | ||||
-rw-r--r-- | bots/__init__.py | 4 | ||||
-rw-r--r-- | bots/bad.py | 5 | ||||
-rw-r--r-- | bots/bot.py | 2 | ||||
-rw-r--r-- | bots/idiot.py | 5 | ||||
-rw-r--r-- | game/__init__.py | 2 | ||||
-rw-r--r-- | game/game.py | 2 | ||||
-rw-r--r-- | tests/test_board.py | 2 | ||||
-rw-r--r-- | tui/__init__.py | 3 | ||||
-rw-r--r-- | tui/static.py | 3 | ||||
-rw-r--r-- | tui/tui.py | 4 |
14 files changed, 76 insertions, 40 deletions
@@ -1,9 +1,8 @@ """ app.py - pääohjelma """ -from board.board import Board, Level -from tui.tui import Tui -from game.game import Game -#from bots.idiot import IdiotBot -from bots.bad import BadBot +from board import Board, Level +from tui import Tui +from game import Game +from bots import BadBot # pylint: disable = too-few-public-methods class App: 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 ) +} diff --git a/bots/__init__.py b/bots/__init__.py new file mode 100644 index 0000000..f180c3a --- /dev/null +++ b/bots/__init__.py @@ -0,0 +1,4 @@ +""" bots - tämä alimoduli tarjoaa tekoälyn """ + +from .bot import Bot +from .bad import BadBot diff --git a/bots/bad.py b/bots/bad.py index 1419fc0..fb853f1 100644 --- a/bots/bad.py +++ b/bots/bad.py @@ -1,6 +1,7 @@ """ bots/bad.py - botti joka ehkä osaa merkata jonkun asian """ -from bots.bot import Bot -from tui.static import Action +from tui import Action +from .bot import Bot + class BadBot(Bot): """ IdiotBot - merkistsee kaikki turvallisiksi avata """ # pylint: disable = too-few-public-methods diff --git a/bots/bot.py b/bots/bot.py index 8466c51..a097a8d 100644 --- a/bots/bot.py +++ b/bots/bot.py @@ -1,5 +1,5 @@ """ bots/bot.py - bottien kantaisä """ -from tui.static import Action +from tui import Action class Bot(): """ Bot - perusluokka perittäväksi """ diff --git a/bots/idiot.py b/bots/idiot.py index 200d690..933eee9 100644 --- a/bots/idiot.py +++ b/bots/idiot.py @@ -1,6 +1,7 @@ """ bots/idiot.py - se ensimmäinen botti joka tekee kaiken väärin """ -from bots.bot import Bot -from tui.static import Action +from tui import Action +from .bot import Bot + class IdiotBot(Bot): """ IdiotBot - merkistsee kaikki turvallisiksi avata """ # pylint: disable = too-few-public-methods diff --git a/game/__init__.py b/game/__init__.py new file mode 100644 index 0000000..066e2e3 --- /dev/null +++ b/game/__init__.py @@ -0,0 +1,2 @@ +""" game - pelin kulkuun liittyvä logiikka """ +from .game import Game diff --git a/game/game.py b/game/game.py index 4420aed..76ee382 100644 --- a/game/game.py +++ b/game/game.py @@ -1,5 +1,5 @@ """ game/game.py - pelin etenemiseen liittyvä ohjaus """ -from tui.tui import Action +from tui import Action class Game: """ Game - peli """ diff --git a/tests/test_board.py b/tests/test_board.py index be4b9dc..904f73a 100644 --- a/tests/test_board.py +++ b/tests/test_board.py @@ -2,7 +2,7 @@ # pylint: disable = protected-access import unittest -from board.board import Board, Level, LevelSpecs +from board import Board, Level, LevelSpecs class TestBoardClass(unittest.TestCase): """ pelilauden testit""" diff --git a/tui/__init__.py b/tui/__init__.py new file mode 100644 index 0000000..c1c5b91 --- /dev/null +++ b/tui/__init__.py @@ -0,0 +1,3 @@ +""" tui - hoitaa käyttäjälle katseltavaa ja havaitsee syötteet """ +from .tui import Tui +from .static import Action diff --git a/tui/static.py b/tui/static.py index 391e3f1..267b061 100644 --- a/tui/static.py +++ b/tui/static.py @@ -1,6 +1,7 @@ """ tui/static.py - Staattiset määritykset tui:ssa tarvittaville jutuille. """ from enum import Enum from dataclasses import dataclass +from board import Tile class Action(Enum): """ tominnot, joita voidaan saada palautusrvona """ @@ -57,7 +58,7 @@ class TileType: TileTypes = { - 0: TileType( "[ ]", [(0x7,0), (0x7,0), (0x7,0)] ), + Tile.BLANK: TileType( "[ ]", [(0x7,0), (0x7,0), (0x7,0)] ), 1: TileType( "[1]", [(0xA,0), (0xA,0), (0xA,0)] ), 2: TileType( "[2]", [(0xB,0), (0xB,0), (0xB,0)] ), 3: TileType( "[3]", [(0xD,0), (0xD,0), (0xD,0)] ), @@ -2,8 +2,8 @@ # pylint: disable = multiple-imports import termios, fcntl, sys, os from time import sleep -from tui.static import Action, ActionKeys, TileTypes -from tui.ansi import Ansi +from .static import Action, ActionKeys, TileTypes +from .ansi import Ansi class Tui(): |