Subject Computer Science Python Programming

Question

See Game1010.pdf

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

import Position
import Block

def make_board(dimension=10, positions_to_fill=frozenset()):
    """
       Return a new board of the given dimension for which all cells at the
       given positions are already filled.
       ASSUMPTIONS
       - The given dimension is a positive integer number.
       - The filled positions is a collection of proper positions. Positions
          outside the boundaries of the new board have no impact on the content
          of the new board.
    """
    board = dict()
    for i in range(1, dimension + 1):
       board[i] = set()
    if positions_to_fill != frozenset():
       for position in positions_to_fill:
            if Position.is_proper_position(position) and Position.is_proper_position_for_board(dimension,position):
                (column,row) = position
                board[row].add(column)
    return board



def copy_board(board):
    """
       Return a copy of the given board.
       ASSUMPTIONS
       - The given board is a proper board.
    """
    return dict.copy(board)



def is_proper_board(board):
    """
       Check wether the given board is a proper board.
       - ...
       ASSUMPTIONS
       - None
       NOTE
       - You need to complete the conditions
       (as they depend on the internal representation you have chosen for the board)
    """
    if board is None:
       return False
    if not isinstance(board, dict):
       return False

    dim = dimension(board)
    for key in board.keys():
       if key < 1 or key > dim:
            return False
       for column in board[key]:
            if column< 1 or column > dim:
                return False
    return True




def dimension(board):
    """
       Return the dimension of the given board.
       - The function returns the number of rows (== number of columns) of
          the given board.
       ASSUMPTIONS
       - The given board is a proper board.
    """
    return len(board.keys())



def get_all_filled_positions(board):
    """
       Return a set of all the positions of filled cells on the given board.
       ASSUMPTIONS
       - The given board is a proper board.
    """
    filled_positions = set()
    for key in board.keys():
       for column in board[key]:
            filled_positions.add((column,key))
    return filled_positions




def is_filled_at(board, position):
    """
       Return a boolean indicating whether or not the cell at the given position
       on the given board is filled.
       - Returns false if the given position is outside the boundaries of the
          given board.
       ASSUMPTIONS
       - The given board is a proper board.
       - The given position is a proper position.
    """
    (column, row) = position
    if not Position.is_proper_position_for_board(dimension(board), position):
       return False
    if column in board[row]:
       return True
    else:
       return False





def is_filled_row(board, row):
    """
       Return a boolean indicating whether or not all the cells of the given
       row on the given board are filled.
       - Returns false if the given row is not an integer number or if it is
          outside the boundaries of the given board.
       ASSUMPTIONS
       - The given board is a proper board.
       NOTE
       - You are not allowed to use for statements in the body of this function.
    """
    if not isinstance(row, int):
       return False
    dim = dimension(board)
    if row < 1 or row > dim:
       return False
    for i in range (1, dim + 1):
       if i not in board[row]:
            return False
    return True



def is_filled_column(board, column):
    """
       Return a boolean indicating whether or not all the cells of the given
       column on the given board are filled.
       - Returns false if the given column is not an integer number or if it is
          outside the boundaries of the given board.
       ASSUMPTIONS
       - The given board is a proper board.
       NOTE
       - You are not allowed to use while statements in the body of this function.
    """
    dim = dimension(board)
    for i in range (1, dim + 1):
       try:
            if column not in board[i]:
                return False
       except:
            return False
    return True




def get_all_filled_rows(board):
    """
       Return all the rows on the given board that are completely filled.
       - The function returns a list of the numbers in ascending order of
          all the rows that are completely filled.
       ASSUMPTIONS
       - The given board is a proper board.
       NOTE
       - You are not allowed to use for statements in the body of this function.
    """
    rows = []
    for i in range(1, dimension(board) + 1):
       if is_filled_row(board,i):
            rows.append(i)
    return rows



def get_all_filled_columns(board):
    """
       Return all the columns on the given board that are completely filled.
       - The function returns a tuple of the numbers in descending order of
          all the columns that are completely filled.
       ASSUMPTIONS
       - The given board is a proper board.
       NOTE
       - You are not allowed to use while statements in the body of this function.
    """
    columns = []
    for i in range(dimension(board), 0, -1):
       if is_filled_column(board,i):
            columns.append(i)
    return tuple(columns)



def fill_cell(board, position):
    """
       Fill the cell at the given position on the given board.
       - Nothing happens if the given position is outside the
          boundaries of the given board or if the given cell is
          already filled.
       ASSUMPTIONS
       - The given board is a proper board.
       - The given position is a proper position.
    """
    if Position.is_proper_position(position) and Position.is_proper_position_for_board(dimension(board), position):
       (column, row) = position
       board[row].add(column)



def fill_all_cells(board, positions):
    """
       Fill all the cells at each position in the given collection of
       positions on the given board.
       - Positions outside the boundaries of the given board are ignored.
       - Positions that are already filled are left untouched.
       ASSUMPTIONS
       - The given board is a proper board.
       - Each position in the collection of positions is a proper position.
    """
    for position in positions:
       fill_cell(board, position)




def free_cell(board, position):
    """
       Free the cell at the given position of the given board.
       - Nothing happens if the cell is already free or if the given
          position is outside the boundaries of the given board.
       ASSUMPTIONS
       - The given board is a proper board.
       - The given position is a proper position.
    """
    if Position.is_proper_position(position) and Position.is_proper_position_for_board(dimension(board), position):
       (column, row) = position
       board[row].discard(column)




def free_all_cells(board, positions):
    """
       Fill all the cells at each position in the tuple of positions on
       the given board.
       - Positions outside the boundaries of the given board are ignored.
       - Positions that are already filled are left untouched.
       ASSUMPTIONS
       - The given board is a proper board.
       - Each position in the tuple of positions is a proper position.
       NOTE
       - This function must be worked out in a recursive way.
    """
    for position in positions:
       free_cell(board, position)




def free_row(board, row):
    """
       Free all the cells of the given row on the given board.
       - Nothing happens if the given row is not an integer number or if
          it is outside the boundaries of the given board.
       ASSUMPTIONS
       - The given board is a proper board.
    """
    if isinstance(row, int):
       if row > 0 and row < dimension(board)+1:
            board[row].clear()



def free_column(board, column):
    """
       Free all the cells of the given column on the given board.
       - Nothing happens if the given column is not an integer number or if
          it is outside the boundaries of the given board.
       ASSUMPTIONS
       - The given board is a proper board.
    """
    if isinstance(column, int):
       if column > 0 and column < dimension(board)+1:
            for i in range (1, dimension(board) + 1):
                board[i].discard(column)





def can_be_dropped_at(board, block, position):
    """
       Check whether the given block can be dropped at the given position.
       - The given position determines the position for the anchor of the
          given block.
       - True if and only if for each of the dot positions D of the given block
          there is a FREE cell at a position within the boundaries of the given
          board and at the same horizontal- and vertical distance from the
          given position as the horizontal- and vertical distance of the dot
          position D from the anchor of the given block.
       ASSUMPTIONS
       - The given board is a proper board.
       - The given block is a proper block.
       - The given position is a proper position.
    """
    for block_part in block:
       (delta_x, delta_y) = block_part
       translated_pos = Position.translate_over(position, delta_x, delta_y)
       if Position.is_proper_position(translated_pos) and Position.is_proper_position_for_board(dimension(board), translated_pos):
            if is_filled_at(board, translated_pos):
                return False
       else:
            return False

    return True




def get_droppable_positions(board, block):
    """
       Return a list of all positions at which the given block can be dropped
       on the given board.
       - The positions in the resulting list are in ascending order.
       ASSUMPTIONS
       - The given board is a proper board.
       - The given block is a proper block.
       NOTE
       - The function should only examine positions at which the given block
          fully fits within the boundaries of the given board.
    """
    drop_list = []
    for i in range (-2 * dimension(board), 2 * dimension(board)):
       for j in range (-2 * dimension(board), 2 * dimension(board)):
            position = (i,j)
            if can_be_dropped_at(board, block, position):
                drop_list.append(position)

    return drop_list


def drop_at(board, block, position):
    """
       Drop the given block at the given position on the given board.
       - Each of the cells on the given board at a position with the same
          horizontal- and vertical distance from the given position as a dot
          position of the given block from the block's anchor, is filled.
       - Nothing happens if the given block can not be dropped at the given
          position on the given board.
       ASSUMPTIONS
       - The given board is a proper board.
       - The given position is a proper position.
       - The given block is a proper block.
    """
    if can_be_dropped_at(board, block, position):
       for block_part in block:
            (delta_x, delta_y) = block_part
            translated_pos = Position.translate_over(position, delta_x, delta_y)
            fill_cell(board, translated_pos)



def clear_full_rows_and_columns(board):
    """
       Clear all full rows and all full columns on the given board.
       ASSUMPTIONS
       - The given board is a proper board.
    """
    rows = get_all_filled_rows(board)
    columns = get_all_filled_columns(board)
    for row in rows:
       free_row(board, row)
    for column in columns:
       free_column(board, column)



def are_chainable(board, positions, to_check = frozenset(), checked = frozenset(), chained_pos = frozenset()):
    """
       Check whether the given collection of positions is chained on the
       given board.
       - True if and only if at least one collection of chained positions exists
          on the given board that includes all given positions and for which all
          the cells in that collection are either all filled or all empty.
       ASSUMPTIONS
       - The given board is a proper board.
       - Each of the given positions is a proper position for the given board.
       - All the cells on the given board at the given positions all have the
          same state, i.e. they are all filled or all empty.
       NOTE
       - This function should be worked out in a recursive way
    """
    if len(positions) == 0 or len(positions) == 1:
       return True

    if isinstance(positions, tuple) or isinstance(positions, list):
       positions = set(positions)

    if to_check == frozenset() and chained_pos == frozenset() and checked == frozenset():
       position = positions.pop()
       positions.add(position)
       chained_pos = set()
       chained_pos.add(position)
       to_check = set()
       to_check.add(position)
       checked = set()

    if chained_pos.intersection(positions) == positions:
       return True
    if len(to_check)==0:
       return False

    next = to_check.pop()
    checked.add(next)
    filled = is_filled_at(board, next)
    adj_position = Position.get_adjacent_positions(next, dimension(board))
    for adj_pos in adj_position:
       if is_filled_at(board, adj_pos) == filled:
            if not adj_pos in checked:
                to_check.add(adj_pos)
                chained_pos.add(adj_pos)

    return are_chainable(board, positions, to_check, checked, chained_pos)



def print_board(board):
    """
       Print the given board on the standard output stream.
       ASSUMPTIONS
       - The given board is a proper board.
    """
    for row in range(dimension(board), 0, -1):
       print('{:02d}'.format(row), end=" ")
       for column in range(1, dimension(board) + 1):
            if is_filled_at(board, (column, row)):
                print("\u25A9 ", end=" ")
            else:
                print(" ", end=" ")
       print()
    print("   ", end="")
    print('', end=" ")
    for column in range(1, dimension(board) + 1):
       print('{:02d}'.format(column), end=" ")
    print()...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Assisting Tutor

Related Homework Solutions

Marsenne Primes Using Python
Homework Solution
$75.00
Programming
Computer Science
Python
Prime Numbers
Mathematics
Scaling Factors
Utilities
Graphs
Statements
Variables
Functions
Algorithms
Time Complexity
Data Collection Script Using Python
Homework Solution
$63.00
Computer Science
Programming
Python
Windows
DOS Shell Command
Scripts
Data Collection
Algorithms
Statements
Variables
Methods
SSID
MAC Address
Signal Strength
Information
Tables
Databases
Conway's Game of Life
Homework Solution
$85.00
Computer
Science
Python
Programming
Conway's Game of Life
Lists
Nested Loops
Get help from a qualified tutor
Live Chats