From e56c2153add98361ea7909789b195d3a702f85bb Mon Sep 17 00:00:00 2001 From: Aineopintojen-harjoitustyo-Algoritmit-j Date: Sun, 14 Jan 2024 13:47:35 +0200 Subject: Moving things around and adding fouth flag option for bot suggestions. --- board/board.py | 8 ++++---- game/game.py | 4 ++-- tests/test_board.py | 32 +++++++++++++++++--------------- tui/ansi.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ tui/static.py | 24 +++--------------------- tui/tui.py | 40 +++++++++------------------------------- 6 files changed, 81 insertions(+), 73 deletions(-) create mode 100644 tui/ansi.py diff --git a/board/board.py b/board/board.py index 82e6835..04d81d1 100644 --- a/board/board.py +++ b/board/board.py @@ -121,7 +121,7 @@ class Board(): return self.masked[x][y] - def flag_tile(self, x, y): + def flag(self, x, y): """ aseta lippu peitetylle ruudulle""" if self.invalid_coordinates(x, y): print("Koordinaatit on pelilaudan ulkopuolella", file=stderr) @@ -131,10 +131,10 @@ class Board(): case 0: print("Ei voi liputtaa avattua ruutua", file=stderr) return False - case 10 | 11: + case 10 | 11 | 12: self.masked[x][y]+=1 return True - case 12: + case 13: self.masked[x][y]=10 return True @@ -142,7 +142,7 @@ class Board(): return False - def make_guess(self, x, y): + def guess(self, x, y): """ tee arvaus """ if self.invalid_coordinates(x, y): print("Koordinaatit on pelilaudan ulkopuolella", file=stderr) diff --git a/game/game.py b/game/game.py index 91c240f..d46d97b 100644 --- a/game/game.py +++ b/game/game.py @@ -24,7 +24,7 @@ class Game: return False case Action.OPEN: if self.board.get_mask(self.x, self.y): - if not self.board.make_guess(self.x, self.y): + if not self.board.guess(self.x, self.y): self.ui.game_over( self.board.get_view(), self.x, self.y ) @@ -33,5 +33,5 @@ class Game: self.ui.game_win(self.board.get_view(), self.x, self.y) return False case Action.FLAG: - self.board.flag_tile(self.x, self.y) + self.board.flag(self.x, self.y) return True diff --git a/tests/test_board.py b/tests/test_board.py index d87e825..5fb36ad 100644 --- a/tests/test_board.py +++ b/tests/test_board.py @@ -15,7 +15,7 @@ class TestBoardClass(unittest.TestCase): b = Board(15) self.assertEqual(b.size, 15) - def test_get_view_and_make_guess(self): + def test_get_view_and_guess(self): """ laudan näkymä on oikein senkin jälkeen kun on arvattu""" b = Board(3) b.tiles=[[0,0,0],[0,1,1],[0,1,9]] @@ -25,13 +25,13 @@ class TestBoardClass(unittest.TestCase): for i in range(3): self.assertEqual(v[i],t[i]) - self.assertTrue(b.make_guess(0,0)) + self.assertTrue(b.guess(0,0)) v = b.get_view() t = [[0,0,0],[0,1,1],[0,1,10]] for i in range(3): self.assertEqual(v[i],t[i]) - self.assertFalse(b.make_guess(2,2)) + self.assertFalse(b.guess(2,2)) def test_is_winning(self): """ toimiiko voittotilanteen tunnistus """ @@ -44,13 +44,13 @@ class TestBoardClass(unittest.TestCase): b.masked=[[0,0],[10,10]] self.assertFalse(b.is_winning()) - def test_error_conditions_in_make_guess(self): + def test_error_conditions_in_guess(self): """ ruudun avaus alueen ulkopuolelta tai avatussa ruudussa ei onnistu""" b = Board(2) b.tiles=[[1,9],[9,9]] - self.assertFalse(b.make_guess(2,2)) - self.assertTrue(b.make_guess(0,0)) - self.assertFalse(b.make_guess(0,0)) + self.assertFalse(b.guess(2,2)) + self.assertTrue(b.guess(0,0)) + self.assertFalse(b.guess(0,0)) def test_get_mask(self): """ maski annetaan oikein """ @@ -58,27 +58,29 @@ class TestBoardClass(unittest.TestCase): b.tiles=[[1,9],[9,9]] self.assertEqual(b.get_mask(0,0), 10) - def test_flag_tile(self): + def test_flag(self): """ ruudun liputus toimii """ b = Board(2) b.tiles=[[1,9],[9,9]] self.assertEqual(b.get_mask(0,0), 10) - self.assertTrue(b.flag_tile(0,0)) + self.assertTrue(b.flag(0,0)) self.assertEqual(b.get_mask(0,0), 11) - self.assertTrue(b.flag_tile(0,0)) + self.assertTrue(b.flag(0,0)) self.assertEqual(b.get_mask(0,0), 12) - self.assertTrue(b.flag_tile(0,0)) + self.assertTrue(b.flag(0,0)) + self.assertEqual(b.get_mask(0,0), 13) + self.assertTrue(b.flag(0,0)) self.assertEqual(b.get_mask(0,0), 10) - def test_flag_tile_error_conditions(self): + def test_flag_error_conditions(self): """ liputus ei onnistu jos avattu, alueen ulkopuolella, outo arvo """ b = Board(2) b.tiles=[[1,9],[9,9]] b.masked[0][0]=14 - self.assertFalse(b.flag_tile(0,0)) + self.assertFalse(b.flag(0,0)) b.masked[0][0]=0 - self.assertFalse(b.flag_tile(0,0)) - self.assertFalse(b.flag_tile(2,2)) + self.assertFalse(b.flag(0,0)) + self.assertFalse(b.flag(2,2)) def test_reveal(self): """ paljastuksen jälkeen näkyy laatat sellaisenaan """ diff --git a/tui/ansi.py b/tui/ansi.py new file mode 100644 index 0000000..c25ff6c --- /dev/null +++ b/tui/ansi.py @@ -0,0 +1,46 @@ +""" ansi.py - ansi ohjauskomentoja. värit jne """ + +class Ansi: + """ Ansi - Luokallinen staattisia metodeja ansi komennoille """ + + BLACK = 0 + RED = 1 + GREEN = 2 + YELLOW = 3 + BLUE = 4 + MAGENTA = 5 + CYAN = 6 + WHITE = 7 + GRAY = 8 + BRIGHT_RED = 9 + BRIGHT_GREEN = 0xA + BRIGHT_YELLOW = 0xB + BRIGHT_BLUE = 0xC + BRIGHT_MAGENTA = 0xD + BRIGHT_CYAN = 0xE + BRIGHT_WHITE = 0xF + + @staticmethod + def color(color): + """ asettaa tekstin värin """ + if color in range(16): + print(end=f"\033[{'1;' if color//8 else ''}3{color%8}m") + + + @staticmethod + def bg(color): + """ asettaa tekstin taustan värin""" + if color in range(8): + print(end=f"\033[4{color}m") + + + @staticmethod + def cup(lines): + """ liikuttaa kursoria ylöspäin""" + print(end=f"\033[{lines}F") + + + @staticmethod + def reset(): + """ resetoi tekstin värin ja muut attribuutit perusarvoille """ + print(end="\033[0m") diff --git a/tui/static.py b/tui/static.py index ecd80b9..7bdfa12 100644 --- a/tui/static.py +++ b/tui/static.py @@ -1,5 +1,5 @@ """ tui/static.py - Staattiset määritykset tui:ssa tarvittaville jutuille. """ -from enum import Enum, IntEnum +from enum import Enum from dataclasses import dataclass class Action(Enum): @@ -50,24 +50,6 @@ TileTypes = { 9: TileType( "[@]", [(0xF,1), (0xF,1), (0xF,1)] ), 10: TileType( "[#]", [(0x8,7), (0x8,7), (0x8,7)] ), 11: TileType( "[B]", [(0x8,7), (0x1,7), (0x8,7)] ), - 12: TileType( "[?]", [(0x8,7), (0x3,7), (0x8,7)] ) + 12: TileType( "[ ]", [(0x8,7), (0x3,7), (0x8,7)] ), + 13: TileType( "[?]", [(0x8,7), (0x0,7), (0x8,7)] ) } - -class Colors(IntEnum): - """ ANSI värejä vastaavat lukuarvot """ - BLACK = 0 - RED = 1 - GREEN = 2 - YELLOW = 3 - BLUE = 4 - MAGENTA = 5 - CYAN = 6 - WHITE = 7 - GRAY = 8 - BRIGHT_RED = 9 - BRIGHT_GREEN = 0xA - BRIGHT_YELLOW = 0xB - BRIGHT_BLUE = 0xC - BRIGHT_MAGENTA = 0xD - BRIGHT_CYAN = 0xE - BRIGHT_WHITE = 0xF diff --git a/tui/tui.py b/tui/tui.py index 06a5c6e..5b15d5f 100644 --- a/tui/tui.py +++ b/tui/tui.py @@ -2,7 +2,8 @@ # pylint: disable = multiple-imports import termios, fcntl, sys, os from time import sleep -from tui.static import Action, ActionKeys, Colors, TileTypes +from tui.static import Action, ActionKeys, TileTypes +from tui.ansi import Ansi class Tui(): @@ -30,41 +31,19 @@ class Tui(): print() - def set_color(self, color): - """ asettaa tekstin värin """ - if color in range(16): - print(end=f"\033[{'1;' if color//8 else ''}3{color%8}m") - - - def set_bg(self, color): - """ asettaa tekstin taustan värin""" - if color in range(8): - print(end=f"\033[4{color}m") - - - def cursor_up(self, lines): - """ liikuttaa kursoria ylöspäin""" - print(end=f"\033[{lines}F") - - - def reset_color(self): - """ resetoi tekstin värin ja muut attribuutit perusarvoille """ - print(end="\033[0m") - - def draw_tile(self, tile, hilighted): """ "piirtää" yhden ruudun """ for ch, colors in zip(TileTypes[tile].text, TileTypes[tile].colors): color, bg = colors - self.set_color(Colors.BLACK if hilighted else color) - self.set_bg(Colors.CYAN if hilighted else bg) + Ansi.color(Ansi.BLACK if hilighted else color) + Ansi.bg(Ansi.CYAN if hilighted else bg) print(end=ch) - self.reset_color() + Ansi.reset() def draw_matrix(self, matrix, hx, hy): """ "piirtää" ruudukon """ - self.cursor_up(len(matrix[0])) + Ansi.cup(len(matrix[0])) # pylint: disable=consider-using-enumerate for y in range(len(matrix[0])): for x in range(len(matrix)): @@ -113,15 +92,14 @@ class Tui(): """ näyttää laudan, tekstin alla ja jää odottelemaan nappia """ self.draw_matrix(matrix, x, y) print(text) - self.cursor_up(1) + Ansi.cup(1) self.read_action() def game_begin(self, size): """ ruudun alustus ja lähtökoordinaatien määritys """ - for _ in range(size+1): - print() - self.cursor_up(1) + print(end="\n"*(size+1)) + Ansi.cup(1) return size//2, size//2 -- cgit v1.2.3