I’m working on a Hitori puzzle game in Python, and I’m facing an issue where if I double-click a cell, all the subsequent cells turn white (or clear). I’m using the boardgame library my professor gave us to create the game, and I’ve written custom code to handle the puzzle logic.
When I click on a cell, it triggers the play method, which updates the annotation for that cell, but, when i click twice (which serves as circling the number), instead of just updating that cell, all the other cells after it seem to reset to white or clear. The effect appears when clicking on any cell after the initial double-click.
The game works properly when there are just the annotations in place.
Can anyone help me understand why all the cells are turning white when I double-click on just one, and how I can fix this issue?
Here’s the relevant part of my code:
from boardgame import BoardGame
import csv
import g2d
class Hitori(BoardGame):
CLEAR = 0
BLACK = 1
CIRCLE = 2
def __init__(self):
self._w, self._h = 5, 5
self._numbers = []
try:
with open("5-easy.csv") as f:
reader = csv.reader(f)
for row in reader:
if len(row) != self._w:
raise ValueError(f"Each row must have exactly {self._w} numbers")
self._numbers.append(row)
except FileNotFoundError:
raise FileNotFoundError("File '5-easy.csv' not found")
self._annots = [Hitori.CLEAR] * (self._w * self._h)
def play(self, x, y, action):
pos = x + y * self._w
if action == "flag":
self.fill(x, y)
else:
self._annots[pos] = (self._annots[pos] + 1) % 3
def fill(self, x, y):
pos = x + y * self._w
if 0 <= x < self._w and 0 <= y < self._h and self._annots[pos] == Hitori.CLEAR:
self._annots[pos] = Hitori.BLACK
def read(self, x, y):
try:
cell_size = 40
px, py = x * cell_size, y * cell_size
txt = str(self._numbers[y][x])
pos = x + y * self._w
if self._annots[pos] == Hitori.BLACK:
txt += "#"
g2d.set_color((0, 0, 0))
g2d.draw_rect((px, py), (40, 40))
elif self._annots[pos] == Hitori.CIRCLE:
txt += "!"
g2d.set_color((0, 0, 0))
g2d.set_stroke(2)
g2d.draw_circle((px + 20, py + 20), 20)
return txt
except IndexError:
return ""
def finished(self):
return all(annot != Hitori.CLEAR for annot in self._annots)
from boardgamegui import gui_play
gui_play(Hitori())
I tried double-clicking a cell in my Hitori puzzle game, which triggers the play method and updates the state of the clicked cell. I expected only that cell to change its state (toggle between CLEAR, BLACK, and CIRCLE), and I also expected the visual display of the game to update accordingly.
When I double-click on a cell, I expected only that cell’s state to toggle (from CLEAR to BLACK or CIRCLE), and the cell to be rendered accordingly.
What actually happened: When I double-click a cell, all the cells after the clicked cell turn white (clear), even though I only intended to modify the clicked cell.
I added a debug print statement inside the play method to check the state of self._annots after each action, but the issue still occurs.
I reviewed the fill method, which is used to mark cells as black, but I don’t see any reason why it would affect other cells.
amaale is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.