diff options
-rw-r--r-- | board/board.py | 29 | ||||
-rw-r--r-- | tests/test_board.py | 6 |
2 files changed, 20 insertions, 15 deletions
diff --git a/board/board.py b/board/board.py index 04d81d1..0b4c3a2 100644 --- a/board/board.py +++ b/board/board.py @@ -121,25 +121,26 @@ class Board(): return self.masked[x][y] - def flag(self, x, y): + def flag(self, x, y, flag=-1): """ aseta lippu peitetylle ruudulle""" if self.invalid_coordinates(x, y): print("Koordinaatit on pelilaudan ulkopuolella", file=stderr) return False - match self.masked[x][y]: - case 0: - print("Ei voi liputtaa avattua ruutua", file=stderr) - return False - case 10 | 11 | 12: - self.masked[x][y]+=1 - return True - case 13: - self.masked[x][y]=10 - return True - - print("Ruudulla odottamaton lippu", file=stderr) - return False + if self.masked[x][y] not in range(10,14): + print("Ruudulla odottamaton lippu tai se on avattu", file=stderr) + return False + + if flag == -1: + self.masked[x][y] += 1 if self.masked[x][y] < 13 else -3 + return True + + if flag not in range(10,14): + print("Lippua jota asetat ei ole olemassa", file=stderr) + return False + + self.masked[x][y]=flag + return True def guess(self, x, y): diff --git a/tests/test_board.py b/tests/test_board.py index 5fb36ad..82a2015 100644 --- a/tests/test_board.py +++ b/tests/test_board.py @@ -71,13 +71,17 @@ class TestBoardClass(unittest.TestCase): self.assertEqual(b.get_mask(0,0), 13) self.assertTrue(b.flag(0,0)) self.assertEqual(b.get_mask(0,0), 10) + self.assertTrue(b.flag(0,0,13)) + self.assertEqual(b.get_mask(0,0), 13) 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 + b.masked[0][0]=6 self.assertFalse(b.flag(0,0)) + b.masked[0][0]=10 + self.assertFalse(b.flag(0,0,4)) b.masked[0][0]=0 self.assertFalse(b.flag(0,0)) self.assertFalse(b.flag(2,2)) |