summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi>2024-01-25 14:45:01 +0200
committerAineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi>2024-01-25 14:45:01 +0200
commit84ff4aa8fc7714b5841097e0dd800085982321e4 (patch)
treeefff225e0312447de2d8282a61ea50b9eb29a140
parent16c470e7782487db34c5af8dba3d4c427ff9e434 (diff)
Implements levels to Board
-rw-r--r--app.py4
-rw-r--r--board/board.py61
-rw-r--r--tests/test_board.py42
3 files changed, 72 insertions, 35 deletions
diff --git a/app.py b/app.py
index 9b9d3d0..3f77341 100644
--- a/app.py
+++ b/app.py
@@ -1,5 +1,5 @@
""" app.py - pääohjelma """
-from board.board import Board
+from board.board import Board, Level
from tui.tui import Tui
from game.game import Game
#from bots.idiot import IdiotBot
@@ -9,7 +9,7 @@ from bots.bad import BadBot
class App:
""" App - Luokka pääohjelmalle"""
def __init__(self):
- self.board = Board(30,16,99)
+ self.board = Board(level=Level.BEGINNER)
self.bot = BadBot()
self.ui = Tui(self.bot)
self.game = Game(self.board,self.ui)
diff --git a/board/board.py b/board/board.py
index 3cf0144..00dcd10 100644
--- a/board/board.py
+++ b/board/board.py
@@ -2,30 +2,44 @@
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 )
+}
+
class Board():
""" Board - Luokka joka pitää huolen pelilaudasta ja siihen kohdistuvista
siirroista.
"""
- def __init__(self, width = 9, height = 9, bombs = 10):
- # 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
- width = 2 if width < 2 else width
- width = 50 if width > 50 else width
-
- height = 2 if height < 2 else height
- height = 50 if height > 50 else height
-
- # Pommeja pitää olla vähintään yksi, kuten tyhjiäkin
- #bombs = size*size*size//100 if bombs < 1 else bombs
- bombs = width*height-1 if bombs>=width*height else bombs
- bombs = 1 if bombs == 0 else bombs
-
- self.__width = width
- self.__height = height
- self.__bombs = bombs
+ def __init__(self, **opts):
+
+ self.__level = opts["level"] if "level" in opts else Level.BEGINNER
+ self.__width, self.__height, self.__bombs = LevelSpecs[self.__level]
+
+ self.__width = opts["width"] if "width" in opts else self.__width
+ self.__height = opts["height"] if "height" in opts else self.__height
+ self.__bombs = opts["bombs"] if "bombs" in opts else self.__bombs
+
+ if self.__width not in range(2,51):
+ self.__width = LevelSpecs[self.__level][0]
+ if self.__height not in range(2,51):
+ self.__height = LevelSpecs[self.__level][0]
+
+ if self.__bombs not in range(1,self.__width*self.__height):
+ self.__bombs = self.__width
+
self.__tiles = None
self.__masked = None
self.__initialize_tiles()
@@ -185,3 +199,12 @@ class Board():
def get_height(self):
""" palauttaa laudan korkeuden """
return self.__height
+
+ def get_bombs(self):
+ """ palauttaa pommien määrän """
+ return self.__bombs
+
+ def get_level(self):
+ """ palauttaa vaikesutason """
+ return self.__level if (self.__width, self.__height, self.__bombs) \
+ == LevelSpecs[self.__level] else None
diff --git a/tests/test_board.py b/tests/test_board.py
index 19795b9..be4b9dc 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
+from board.board import Board, Level, LevelSpecs
class TestBoardClass(unittest.TestCase):
""" pelilauden testit"""
@@ -11,15 +11,23 @@ class TestBoardClass(unittest.TestCase):
b = Board()
self.assertTrue(b.get_width()>0)
- def test_init_with_size(self):
- """ olion luominen onnistuu tietyllä koolla"""
- b = Board(30, 15)
- self.assertEqual(b.get_width(), 30)
- self.assertEqual(b.get_height(), 15)
+ def test_init_with_level(self):
+ """ olion luominen onnistuu vaikeustasolla"""
+ b = Board(level=Level.EXPERT)
+ self.assertEqual(b.get_width(), LevelSpecs[Level.EXPERT][0])
+ self.assertEqual(b.get_height(), LevelSpecs[Level.EXPERT][1])
+ self.assertEqual(b.get_bombs(), LevelSpecs[Level.EXPERT][2])
+
+ def test_init_with_incorect_dimensions(self):
+ """ luominen ei saa onnitua mahdottomilla mitoilla """
+ b = Board(width=1, height=999)
+ self.assertEqual(b.get_width(), LevelSpecs[Level.BEGINNER][0])
+ self.assertEqual(b.get_height(), LevelSpecs[Level.BEGINNER][1])
+ self.assertEqual(b.get_bombs(), LevelSpecs[Level.BEGINNER][2])
def test_get_view_and_guess(self):
""" laudan näkymä on oikein senkin jälkeen kun on arvattu"""
- b = Board(3,3)
+ b = Board(width=3, height=3)
b._Board__tiles=[[0,0,0],[0,1,1],[0,1,9]]
v = b.get_view()
@@ -37,7 +45,7 @@ class TestBoardClass(unittest.TestCase):
def test_is_winning(self):
""" toimiiko voittotilanteen tunnistus """
- b = Board(2,2)
+ b = Board(width=2, height=2)
b._Board__tiles=[[1,9],[9,9]]
b._Board__masked=[[12,12],[12,12]]
self.assertFalse(b.is_winning())
@@ -48,7 +56,7 @@ class TestBoardClass(unittest.TestCase):
def test_error_conditions_in_guess(self):
""" ruudun avaus alueen ulkopuolelta tai avatussa ruudussa ei onnistu"""
- b = Board(2,2)
+ b = Board(width=2, height=2)
b._Board__tiles=[[1,9],[9,9]]
self.assertFalse(b.guess(2,2))
self.assertTrue(b.guess(0,0))
@@ -56,13 +64,13 @@ class TestBoardClass(unittest.TestCase):
def test_get_mask(self):
""" maski annetaan oikein """
- b = Board(2,2)
+ b = Board(width=2, height=2)
b._Board__tiles=[[1,9],[9,9]]
self.assertEqual(b.get_mask(0,0), 12)
def test_flag(self):
""" ruudun liputus toimii """
- b = Board(2,2)
+ b = Board(width=2, height=2)
b._Board__tiles=[[1,9],[9,9]]
self.assertEqual(b.get_mask(0,0), 12)
self.assertTrue(b.flag(0,0))
@@ -78,7 +86,7 @@ class TestBoardClass(unittest.TestCase):
def test_flag_error_conditions(self):
""" liputus ei onnistu jos avattu, alueen ulkopuolella, outo arvo """
- b = Board(2,2)
+ b = Board(width=2, height=2)
b._Board__tiles=[[1,9],[9,9]]
b._Board__masked[0][0]=6
self.assertFalse(b.flag(0,0))
@@ -90,10 +98,16 @@ class TestBoardClass(unittest.TestCase):
def test_reveal(self):
""" paljastuksen jälkeen näkyy laatat sellaisenaan """
- b = Board(2,2)
+ b = Board(width=2, height=2)
b.reveal()
t = b._Board__tiles
v = b.get_view()
for i in range(2):
self.assertEqual(v[i],t[i])
- \ No newline at end of file
+
+ def test_get_level(self):
+ """ Testataan että nykyinen vaikeustaso palautuu oikein """
+ b = Board(level=Level.INTERMEDIATE)
+ self.assertEqual(b.get_level(), Level.INTERMEDIATE)
+ b = Board(level=Level.INTERMEDIATE, width=25)
+ self.assertEqual(b.get_level(), None)