operators.py - class GameState"Creates the Game State and...

This preview shows page 1 - 3 out of 5 pages.

class GameState: """Creates the Game State and updates the board.""" def __init__(self, columns: int, rows: int, board_type: str) -> None: self._columns = columns self._rows = rows self._board_type = board_type self.board = self.create_board() def create_board(self): """Create the game board.""" game_board = [] for row in range(2): game_board.append([]) for col in range(self._columns): game_board[-1].append(' ') if self._board_type == 'EMPTY': for row in range(self._rows): game_board.append([]) for col in range(self._columns): game_board[-1].append(' ') elif self._board_type == 'CONTENTS': for row in range(self._rows): letters = list(input()) letters_formatted = [] for letter in letters: letters_formatted.append(' {} '.format(letter)) game_board.append(letters_formatted) return game_board def game_over(self) -> bool: """Check if block exceeds dimentions of board.""" for row in range(0, 2): for col in range(len(self.board[-1])): if self.board[row][col] != ' ': return False return True def remove_matches(self) -> [[]]: """Remove all matches on board""" for row in range(len(self.board)-1, 1, -1): for col in range(len(self.board[-1])-1,-1, -1): if self.board[row][col].startswith('*'): self.board[row][col] = ' ' return self.board def matches_present(self) -> bool: """Check if the matches are present and returns if there is a match ('*_*') or not.""" for row in range(len(self.board)-1, 1, -1): for col in range(len(self.board[-1])-1,-1, -1): if self.board[row][col].startswith('*'): return True def change_match_vertical(self) -> [[]]: """Changes vertical matches to stars."""
for row in range(len(self.board)-1, 2, -1): for col in range(len(self.board[-1])-2, -1, -1): letter = list(self.board[row][col]) if letter[1] == ' ': pass elif letter[1] == list(self.board[row-1][col])[1] and letter[1] == list(self.board[row-2][col])[1]: self.board[row-1][col] = '*{}*'.format(letter[1]) self.board[row-2][col] = '*{}*'.format(letter[1]) self.board[row][col] = '*{}*'.format(letter[1])

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture