def fill(cave, row, col, color): """Fill a chamber of the cave with water, starting at row, col. Water can spread in all four cardinal directions, but cannot penetrate stone. No effect if row or col are not inside the cave. Attempting to pour water where there is already WATER or STONE has no effect. Attempting to pour water outside the cavern has no effect. Attempting to pour water in a cell containing AIR not only places colored water in that cell, but also spreads it in all directions by causing it to be poured in the cell to the left and right and above and below. Args: cave: A matrix (list of lists) representing the cavern. Each cell in the cave may hold AIR, STONE, or WATER. row: Starting row of the grid cell where we pour water col: Starting column of the grid cell where we pour water color: color of the water we try to pour in. Returns: Boolean value, False value until full cave is filled with recursion function at which point True is assigned (used to count chambers) """ if row > (len(cave)-1) or col > (len(cave[0])-1) or row < 0 or col < 0: return False if cave[row][col] == AIR: cave[row][col] = WATER ## labels square as WATER grid.fill_cell(row, col, color) fill(cave,row,col+1,color) # 4 fill functions, one of each direction 1 square fill(cave,row+1,col,color) fill(cave,row,col-1,color) fill(cave,row-1,col,color) return True
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 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.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 fill(cave, row, col, color): """Fill a chamber of the cave with water, starting at row, col. Water can spread in all four cardinal directions, but cannot penetrate stone. No effect if row or col are not inside the cave. Attempting to pour water where there is already WATER or STONE has no effect. Attempting to pour water outside the cavern has no effect. Attempting to pour water in a cell containing AIR not only places colored water in that cell, but also spreads it in all directions by causing it to be poured in the cell to the left and right and above and below. Args: cave: A matrix (list of lists) representing the cavern. Each cell in the cave may hold AIR, STONE, or WATER. row: Starting row of the grid cell where we pour water col: Starting column of the grid cell where we pour water color: color of the water we try to pour in. """ if 0 <= row < len(cave) and 0 <= col < len(cave[0]): if cave[row][col] == AIR: cave[row][col] = WATER grid.fill_cell(row, col, color) fill(cave, row, col-1, color) fill(cave, row, col+1, color) fill(cave, row-1, col, color) fill(cave, row+1, col, color) else: return
def fill(cave, row, col, color): """Fill a chamber of the cave with water, starting at row, col. Water can spread in all four cardinal directions, but cannot penetrate stone. No effect if row or col are not inside the cave. Attempting to pour water where there is already WATER or STONE has no effect. Attempting to pour water outside the cavern has no effect. Attempting to pour water in a cell containing AIR not only places colored water in that cell, but also spreads it in all directions by causing it to be poured in the cell to the left and right and above and below. Args: cave: A matrix (list of lists) representing the cavern. Each cell in the cave may hold AIR, STONE, or WATER. row: Starting row of the grid cell where we pour water col: Starting column of the grid cell where we pour water color: color of the water we try to pour in. """ if (row > len(cave) - 1 or row < 0) or (col > len(cave[0]) - 1 or col < 0) or (cave[row][col] == STONE or cave[row][col] == WATER): return else: grid.fill_cell(row, col, color) cave[row][col] = WATER fill(cave, row + 1, col, color) fill(cave, row - 1, col, color) fill(cave, row, col + 1, color) fill(cave, row, col - 1, color)
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 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(cave): """Create a graphical representation of cave using the grid. This graphical representation can be further manipulated (e.g., filling cave cells with water of various colors) using the fill_cell method of module grid. Args: cave: A matrix (list of lists) representing the cavern Returns: Nothing, but has the side effect of creating a graphical grid representation of the cavern in a 500x500 pixel window. """ nrows = len(cave) ncols = len(cave[0]) grid.make(nrows, ncols, 500, 500) for row in range(nrows): for col in range(ncols): if cave[row][col] == STONE : grid.fill_cell(row, col, grid.black)
def display(cave): """Create a graphical representation of cave using the grid. This graphical representation can be further manipulated (e.g., filling cave cells with water of various colors) using the fill_cell method of module grid. Args: cave: A matrix (list of lists) representing the cavern Returns: Nothing, but has the side effect of creating a graphical grid representation of the cavern in a 500x500 pixel window. """ nrows = len(cave) ncols = len(cave[0]) grid.make(nrows, ncols, 500, 500) for row in range(nrows): for col in range(ncols): if cave[row][col] == STONE: grid.fill_cell(row, col, grid.black)
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!