def get_exit_port(index, rotate_degree, entry_port): tile_config = convert_representation(get_representation(index)) target_tile = Tile(tile_config) rotation_time = rotate_degree // 90 for i in range(rotation_time): target_tile = target_tile.rotate_90() exit_port = target_tile.get_path(convert_letter_to_int(entry_port)) return convert_int_to_letter(exit_port)
def get_grid(self): return_grid = [[0 for x in range(self.width)] for y in range(self.height)] for x in range(0, self.width): for y in range(0, self.height): return_grid[x][y] = Tile(self.get_tile(x, y).get_paths()) return return_grid
def generate_tile_dictionary(): tile_dict = {} tiles_list = tiles.splitlines() for i in range(len(tiles_list)): tile_letter_representation = json.loads(tiles_list[i])[1] tile_int_representation = convert_representation(tile_letter_representation) current_tile = Tile(tile_int_representation) tile_dict[i] = current_tile return tile_dict
def main(): # example render root = Tk() observer = Observer() grid = [[0 for x in range(10)] for y in range(10)] for x in range(0, 10): for y in range(0, 10): grid[x][y] = Tile([]) tile1 = Tile([[0, 1], [2, 3], [4, 5], [6, 7]]) tile2 = Tile([[0, 5], [1, 4], [2, 3], [6, 7]]) tile3 = Tile([[0, 3], [2, 1], [4, 5], [6, 7]]) grid[0][0] = tile1 grid[1][0] = tile2 grid[0][1] = tile3 board = Board() observer.render_observer(board, "marcos", [Position(0, 2, 1), 90], False, 0, [tile1]) root.geometry("750x500+100+100") root.mainloop()
def __init__(self, grid={}, player={}): if len(grid) == 0: # need to initialize to 10 x 10 grid self.grid = [[0 for x in range(10)] for y in range(10)] for x in range(0, 10): for y in range(0, 10): self.grid[x][y] = Tile([]) else: self.grid = grid self.width = len(self.grid) self.height = len(self.grid[0]) self.player_positions = player
def is_suicidal(self, board, tile, target_x, target_y, player_pos): # Make a copy of the board to test out the attempted move temp_board = Board(board.get_grid(), board.get_players()) temp_tile = Tile(tile.get_paths()) # Add chosen tile temp_board.add_tile(temp_tile, target_x, target_y) # Determine where is the end position after adding tile end_position = temp_board.get_path(player_pos) if not end_position: # Invalid player position return True # Add a placeholder avatar "rulechecker" to the end position temp_board.add_player("rulechecker", end_position) return self.has_lost(temp_board, "rulechecker")
def draw_tiles(self): hand = list() # draws three tiles if in initial round if self.current_round == 0: num_tiles = 3 # else draws two else: num_tiles = 2 for x in range(0, num_tiles): # gets a copy of the Tile from the deck hand.append(Tile(self.deck[self.current_tile_index].get_paths())) self.current_tile_index = (self.current_tile_index + 1) % len( self.deck) return hand
def setUp(self): self.b = Board() self.tile1 = Tile([[0, 1], [2, 3], [4, 5], [6, 7]]) self.tile2 = Tile([[1, 6], [0, 2], [3, 7], [4, 5]]) # Same orientation with connections in different order self.tile3 = Tile([[4, 5], [6, 1], [2, 0], [7, 3]]) # Same tile rotated 90 degree self.tile4 = Tile([[3, 0], [2, 4], [5, 1], [6, 7]]) # Same tile rotated 180 degree self.tile5 = Tile([[5, 2], [4, 6], [7, 3], [0, 1]]) # Rotated 270 self.tile6 = Tile([[7, 4], [6, 0], [1, 5], [2, 3]])
letter_port = convert_int_to_letter(end_position[2]) print(json.dumps([color, tile_index, tile_rotation, letter_port, end_position[0], end_position[1]])) print("-------------------------------") if __name__ == "__main__": game_board = Board() rule_checker = RuleChecker() input_content = ''.join(sys.stdin.readlines()) json_array = json.loads(input_content) for placement_json in json_array[:-1]: if not parse_move(game_board, placement_json): raise Exception("Invalid placement.") player_final_move = json_array[-1] color = player_final_move[0][0] chosen_tile_index = player_final_move[0][1] rotation = player_final_move[0][2] target_x = player_final_move[0][3] target_y = player_final_move[0][4] choice_one = player_final_move[1] choice_two = player_final_move[2] if color not in valid_colors: raise Exception("Invalid color") chosen_tile = Tile(convert_representation(get_representation(chosen_tile_index))).rotate_tile(rotation) choice_one_tile = Tile(convert_representation(get_representation(choice_one))) choice_two_tile = Tile(convert_representation(get_representation(choice_two))) player_pos = game_board.get_player(color) check = rule_checker.valid_move(game_board, target_x, target_y, chosen_tile, [choice_one_tile, choice_two_tile], player_pos) print("legal" if check else "illegal")
if not parse_move(game_board, placement_json): raise Exception("Invalid placement.") player_final_move = json_array[-1] if len(player_final_move) != 4 and len(player_final_move) != 3: parse_move(game_board, player_final_move) observer.render_observer(game_board, "jack", list(), False, 0, list()) else: given_tiles = player_final_move[1:] index = given_tiles.index(player_final_move[0][1]) hand = list() for tile in given_tiles: temp_tile = Tile(convert_representation(get_representation(tile))) hand.append(temp_tile) if len(player_final_move) == 4: player_port = player_final_move[0][5] elif len(player_final_move) == 3: player_port = 0 placement = Position(player_final_move[0][3], player_final_move[0][4], player_port) observer.render_observer(game_board, player_final_move[0][0], [placement, player_final_move[0][2]], len(player_final_move) == 4, index, hand) root.geometry("750x500+100+100")
def create_tile(self, tile_index, rotation): result = Tile( self.convert_representation(self.get_representation(tile_index))) return result.rotate_tile(rotation)