def unmark_taken(self, row, col): """ Marks the tile at row,col as no longer in use. Tile at row,col must be in use when this function is called. Args: self: this board row: row of board, 0..3 col: col of board, 0..3 Returns: nothing Requires: Tile must be marked in use. mark_taken and unmark_taken must strictly alternate. Proper sequence is - check position for availability - get character - mark taken - further exploration from this position - unmark taken """ assert row >= 0 and row < len(self.content) assert col >= 0 and col < len(self.content[0]) assert self.in_use[row][col] self.in_use[row][col] = False grid.fill_cell(row, col, grid.white) # Hack alert! grid.label_cell(row, col, self.content[row][col]) # Hack alert!
def __init__(self, tiles): """Create a boggle board and its graphical depiction from a string of 16 characters. Args: self: the board (to be initialized) tiles: A string of exactly 16 characters, each representing one tile. Most characters represent a tile containing that character, but 'q' represents the pair 'qu' on a tile. Returns: Nothing. The board is encapsulated in this module. """ assert(len(tiles) == 16) self.content = [] self.results = [] # Added by Cole to reduce recursion load self.in_use = [] grid.make(4, 4, 500, 500) # Hack alert! for row in range(4): self.content.append([]) self.in_use.append([]) for col in range(4): char = tiles[4*row + col] if char == "q": char = "qu" self.content[row].append(char) self.in_use[row].append(False) grid.fill_cell(row, col, grid.white) # Hack alert! grid.label_cell(row, col, char) # Hack alert!
def handle_events(tile, event): """Handle an event announced by a tile. Arguments: tile: The sdkboard.Tile object announcing the event event: String describing the event. The only currently known event type is "duplicate", for when a tile has been discovered to be a duplicate of another tile in the same row, column, or square. """ if event == "duplicate": grid.fill_cell(tile.row, tile.col, color=grid.red) grid.label_cell(tile.row, tile.col, tile.symbol, color=grid.white) elif event == "determined": grid.fill_cell(tile.row, tile.col, color=grid.green) grid.label_cell(tile.row, tile.col, tile.symbol, color=grid.white) elif event == "constrained": grid.fill_cell(tile.row,tile.col, color=grid.green) grid.fill_cell(tile.row,tile.col, color=grid.white) candidates = [[ '1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']] for subrow in range(3): for subcol in range(3): candidate = candidates[subrow][subcol] if candidate in tile.possible: grid.sub_label_cell(tile.row, tile.col, subrow, subcol, candidate)
def __init__(self, tiles): """ Create a boggle board and its graphical depiction from a string of 16 characters. Args: self: the board (to be initialized) tiles: A string of exactly 16 characters, each representing one tile. Most characters represent a tile containing that character, but 'q' represents the pair 'qu' on a tile. Returns: Nothing. The board is encapsulated in this module. """ assert (len(tiles) == 16) self.content = [] self.in_use = [] grid.make(4, 4, 500, 500) # Hack alert! for row in range(4): self.content.append([]) self.in_use.append([]) for col in range(4): char = tiles[4 * row + col] if char == "q": char = "qu" self.content[row].append(char) self.in_use[row].append(False) grid.fill_cell(row, col, grid.white) # Hack alert! grid.label_cell(row, col, char) # Hack alert!
def mark_taken(self, row, col): """ Marks the tile at row,col as currently in use Args: self: this board row: row of board, 0..3 col: col of board, 0..3 Returns: nothing """ assert row >= 0 and row < len(self.content) assert col >= 0 and col < len(self.content[0]) assert not self.in_use[row][col] self.in_use[row][col] = True grid.fill_cell(row,col,grid.green) grid.label_cell(row,col,self.content[row][col])
def unmark_taken(self, row, col): """ Marks the tile at row,col as no longer in use. Tile at row,col must be in use when this function is called. Args: self: this board row: row of board, 0..3 col: col of board, 0..3 Returns: nothing """ assert row >= 0 and row < len(self.content) assert col >= 0 and col < len(self.content[0]) assert self.in_use[row][col] self.in_use[row][col] = False grid.fill_cell(row,col,grid.white) grid.label_cell(row,col,self.content[row][col])
def display(board): """Create a display of a Sudoku board. Arguments: board: an sdkboard.Board object Note an event handler (handle_events) will be attached as a listener to each tile in the board. """ grid.make(9,9,500,500) grid.sub_grid_dim(3,3) for row in range(9): for col in range(9): # Direct access to tile of sdkboard is more Pythonic # than an access method. # In Java and some other languages we'd need a getter. tile = board.tiles[row][col] tile.register(handle_events) grid.fill_cell(row, col, grid.white) if (tile.symbol != sdkboard.OPEN): grid.label_cell(row, col, tile.symbol)
def mark_taken(self, row, col): """ Marks the tile at row,col as currently in use Args: self: this board row: row of board, 0..3 col: col of board, 0..3 Returns: nothing Requires: Tile must not already be in use. mark_taken and unmark_taken must strictly alternate. Proper sequence is - check position for availability - get character - mark taken - further exploration from this position - unmark taken """ assert row >= 0 and row < len(self.content) assert col >= 0 and col < len(self.content[0]) assert not self.in_use[row][col] self.in_use[row][col] = True grid.fill_cell(row, col, grid.green) grid.label_cell(row, col, self.content[row][col])
def __init__(self, tiles): """ Create a boggle board and its graphical depiction from a string of characters. Args: self: the board (to be initialized) tiles: A string of characters, each representing one tile. Most characters represent a tile containing that character, but 'q' represents the pair 'qu' on a tile. Returns: Nothing. The board is encapsulated in this module. """ # To maintain desired functionality from the command line the board must # square. To ensure this and do math on the board square root is used # frequently. assert(len(tiles)**.5 == int(len(tiles)**.5)) # To ensure board is quare. self.content = [ ] self.in_use = [ ] self.board_height = int(len(tiles)**.5) self.board_width = int(len(tiles)**.5) grid.make(self.board_height,self.board_width,500,500) # Hack alert! for row in range(self.board_height): self.content.append([ ]) self.in_use.append([ ]) for col in range(self.board_width): char = tiles[int(len(tiles)**.5)*row + col] if char == "q" : char = "qu" self.content[row].append(char) self.in_use[row].append( False ) grid.fill_cell(row,col,grid.white) # Hack alert! grid.label_cell(row,col,char) # Hack alert!