def makeDisplay(alphabetLabels, upCardLabels): decisionIndicators = dict() letterWidth = alphabetLabels[0].getWidth() letterHeight = alphabetLabels[0].getHeight() XMARGIN = 50 offset = (GWINDOW_WIDTH - XMARGIN - (len(alphabetLabels) * letterWidth)) / (len(alphabetLabels) + 1) for (i, label) in enumerate(alphabetLabels): x = XMARGIN + (offset + letterWidth) * i + offset gw.add(label, x, GWINDOW_HEIGHT - LETTER_BASE) YMARGIN = 100 upCardOffset = (GWINDOW_HEIGHT - YMARGIN - (len(upCardLabels) * letterHeight)) / (len(upCardLabels) + 1) for (i, label) in enumerate(upCardLabels): y = YMARGIN + (upCardOffset + letterHeight) * i + upCardOffset gw.add(label, LETTER_BASE, y) for (i, upCardLabel) in enumerate(upCardLabels): y = YMARGIN + (upCardOffset + letterHeight) * i + upCardOffset - letterHeight for (j, totalLabel) in enumerate(alphabetLabels): x = XMARGIN + (offset + letterWidth) * j + offset decisionIndicator = GRect(x, y, letterHeight, letterWidth) decisionIndicator.setFillColor("black") decisionIndicator.setFilled(True) gw.add(decisionIndicator) decisionIndicators[(str(int(totalLabel.getLabel())), str(int(upCardLabel.getLabel())))] = decisionIndicator return decisionIndicators
def createFilledRect(x, y, width, height, fill='black', border=None): rect = GRect(x, y, width, height) rect.setFilled(True) if border is None: rect.setColor(fill) else: rect.setColor(border) rect.setFillColor(fill) return rect
def __init__(self): GCompound.__init__(self) bar = GRect(GWINDOW_WIDTH, MENU_BAR_HEIGHT) bar.setFilled(True) bar.setColor(CELL_BORDER_COLOR) bar.setFillColor(MENU_BAR_BGCOLOR) self.add(bar, 0, 0) self.label = GLabel("Y O U D O K U") self.label.setFont(MENU_TITLE_FONT) self.label.setColor(MENU_TITLE_COLOR) self.add(self.label, GWINDOW_WIDTH//2 - self.label.getWidth()//2, MENU_BAR_HEIGHT//2 + self.label.getAscent()//2 - 5)
def create_filled_rect(x, y, width, height, fill="Black", border=None): """ Creates a GRect filled with the specified fill color. If border is specified, the border appears in that color. """ rect = GRect(x - width / 2, y - height / 2, width, height) rect.setFilled(True) if border is None: rect.setColor(fill) else: rect.setColor(border) rect.setFillColor(fill) return rect
def __init__(self, digit): GCompound.__init__(self) self.digit = str(digit) cell = GRect(0, 0, SUBCELL_WIDTH, SUBCELL_WIDTH) cell.setColor(CELL_BORDER_COLOR) cell.setFillColor(SUBCELL_FILL_COLOR) cell.setFilled(True) self.add(cell, 0, 0) self.label = GLabel(digit) self.label.setFont(SUBCELL_FONT) self.label.setColor(SUBCELL_TEXT_COLOR) self.add(self.label, SUBCELL_WIDTH//2 - self.label.getWidth()//2, SUBCELL_WIDTH//2 + self.label.getAscent()//2 - 3)
class SudokuCell(GCompound): def __init__(self, digit): GCompound.__init__(self) if digit != 0: self.digit = str(digit) else: self.digit = None self.cell = GRect(0, 0, CELL_WIDTH, CELL_WIDTH) self.cell.setColor(CELL_BORDER_COLOR) self.cell.setFillColor(CELL_GOOD_COLOR) self.cell.setFilled(True) self.add(self.cell, 0, 0) self.label = None self.only_a_suggestion = True self.render_label() self.selector = None def get_digit(self): if self.digit == None or self.digit == '' or self.only_a_suggestion: return 0 else: return int(self.digit) def set_background_color(self, color): self.cell.setFillColor(color) def suggest_solution(self, digit): if self.only_a_suggestion: self.digit = str(digit) self.render_label() def render_label(self): if self.label is not None: self.remove(self.label) if self.digit is not None and self.digit != "0": self.label = GLabel(self.digit) else: self.label = GLabel("") self.label.setFont(CELL_FONT) if self.only_a_suggestion: self.label.setColor(SUGGESTION_TEXT_COLOR) else: self.label.setColor(CELL_TEXT_COLOR) self.add(self.label, CELL_WIDTH//2 - self.label.getWidth()//2, CELL_WIDTH//2 + self.label.getAscent()//2 - 7) def mousedown(self): self.selector = SudokuDigitSelector() self.add(self.selector, 0, 0) def mouseup(self, x, y): if self.selector is not None: digit = self.selector.mouseup(x, y) if digit is not None: self.digit = digit if str(self.digit) == "0" or self.digit == "": self.digit = "" self.only_a_suggestion = True elif self.digit != "": self.only_a_suggestion = False self.render_label() self.remove(self.selector) self.selector = None