@@ 11,7 11,7 @@ class State:
"""Represents game state at any point in time.
Properties:
- size: size of the board,
+ size: size of the square board
near[x, y]: how many bombs are near `(x, y)`
bomb[x, y]: 1 if `(x, y)` is a bomb
revealed[x, y]: 1 if `(x, y)` has been revealed
@@ 26,7 26,7 @@ class State:
bombs: float = 0.0,
bomb_positions: Optional[List[Tuple[int, int]]] = None,
):
- """Create a new game state.
+ """Create fresh game state.
Args:
size: board size
@@ 49,10 49,9 @@ class State:
bombs = np.max([bombs, 0.])
bombs = np.min([bombs, 1.])
# Choose from all the possible positions
- possible = [
- (x, y) for x in range(self.size) for y in range(self.size)
- ]
- bomb_positions = sample(possible, k=round(self.size**2 * bombs))
+ bomb_positions = sample(
+ [(x, y) for x in range(self.size) for y in range(self.size)],
+ k=round(self.size**2 * bombs))
# Place bombs
for x, y in bomb_positions:
@@ 63,12 62,12 @@ class State:
@property
def safe(self):
"""The inverse of `self.bombs`."""
- return (self.bomb + 1) % 2
+ return self.bomb ^ 1
@property
def hidden(self):
"""The inverse of `self.revealed`."""
- return (self.revealed + 1) % 2
+ return self.revealed ^ 1
@property
def revealed_n(self):
@@ 90,6 89,16 @@ class State:
"""Total number of safe tiles."""
return self.size**2 - self.bomb_n
+ @property
+ def score(self):
+ """Calcualte the score as a simple revealed tiles / safe tiles ratio."""
+ if self.won is not False:
+ # We have not lost yet
+ return self.revealed_n / self.safe_n
+ # If we lost then bombs got revealed too but they should not count
+ # towards the score
+ return (self.revealed_n - self.bomb_n) / self.safe_n
+
def click(self, x: int, y: int):
"""Simulate a click on the `(x, y)` position.
@@ 138,13 147,3 @@ class State:
def flag(self, x: int, y: int):
"""Mark `(x, y)` as flagged."""
self.flagged[x, y] = 1
-
- @property
- def score(self):
- """Calcualte the score as a simple revealed tiles / safe tiles ratio."""
- if self.won is not False:
- # We have not lost yet
- return self.revealed_n / self.safe_n
- # If we lost then bombs got revealed too but they should not count
- # towards the score
- return (self.revealed_n - self.bomb_n) / self.safe_n