def pre_process(self): ''' Pre-processing method Inputs: - Output: None ''' Spot.pre_process(self) # --- Check for disconnection # Check user in the database res = self.session.DB.single('SELECT * FROM Sessions WHERE sid=?', self.session.id) if res['user'] is not None: # Remove the user from the database self.session.DB.update('Sessions', 'user', None, 'sid=?', self.session.id) # Update the session self.session.user = None
def nextPossibleBoard(self, move): move = str(move) x1 = move[0] y1 = move[1] x2 = move[2] y2 = move[3] x1 = x1.upper() x2 = x2.upper() x1 = ord(x1) y1 = int(y1) x2 = ord(x2) y2 = int(y2) x1 = x1 - 65 y1 = 8 - y1 x2 = x2 - 65 y2 = 8 - y2 x1, y1 = y1, x1 x2, y2 = y2, x2 # print(self.spots[0][0].piece.name) ret_val1 = copy.deepcopy(self) ret_val1.spots[x2][y2] = Spot(x2, y2, self.spots[x1][y1].piece.name, True) ret_val1.spots[x1][y1] = Spot(x1, y1, '.') # ret_val1.printBoard() return ret_val1
def parse(self, data: bytes) -> Spot: data = " ".join(data.decode().strip().split()).split(" ") # check for bad data or useless headers if not data or data[0] == "Local": return None if not len(data) == 12: return None # Create a new Spot to return output = Spot() # Parse message output.callsign = data[2][:-3] output.frequency = float(int(data[3].replace(".", "")) / 10) output.spotter = data[4].split("/")[0] output.mode = data[5] output.decibels = int(data[6]) output.speed = int(data[8]) output.message = data[10] output.time = data[11][:-3] + ":" + data[11][-3:-1] return output
def pre_process(self): ''' Pre-processing method Inputs: - Output: None ''' Spot.pre_process(self) # --------------------------------------- # CREATE # --------------------------------------- if self.inputs.getvalue('action_create') is not None: email = self.inputs.getvalue('email') password = self.inputs.getvalue('password') firstname = self.inputs.getvalue('firstname') lastname = self.inputs.getvalue('lastname') if firstname is not None and lastname is not None and email is not None and password is not None: self.data.DB.insert('Users', email=email, password=password, firstname=firstname, lastname=lastname) # --------------------------------------- # DELETE # --------------------------------------- if self.inputs.getvalue('action_delete') is not None: self.data.DB.delete('Users', 'id=?', self.inputs.getvalue('id')) # --------------------------------------- # UPDATE # --------------------------------------- if self.inputs.getvalue('action_save') is not None: # User id uid = self.inputs.getvalue('id') # Firstname self.data.DB.update('Users', 'firstname', self.inputs.getvalue('firstname'), 'id=?', uid) # Lastname self.data.DB.update('Users', 'lastname', self.inputs.getvalue('lastname'), 'id=?', uid) # Email self.data.DB.update('Users', 'email', self.inputs.getvalue('email'), 'id=?', uid) # Password self.data.DB.update('Users', 'password', self.inputs.getvalue('password'), 'id=?', uid)
def __init__(self, *args): ''' The Spot constructor Inputs: *args Usually [settings, session, inputs] ''' # --- Parent constructor Spot.__init__(self, *args)
def make_spots(config): spots = [] for i in range(9): row = [] for j in range(9): const = True if config[i][j] != 0 else False row.append( Spot(50 * j, 50 * i, config[i][j], Spot.get_square(j, i), const)) spots.append(row) return spots
def init(self, color): if color == 'B': opponent_color = 'W' special = {0: 'rook', 1: 'knight', 2: 'bishop', 3: 'queen', 4: 'king', 5: 'bishop', 6: 'knight', 7: 'rook'} else: opponent_color = 'B' special = {0: 'rook', 1: 'knight', 2: 'bishop', 3: 'king', 4: 'queen', 5: 'bishop', 6: 'knight', 7: 'rook'} for i in range(self.h): self.spots[7][i] = Spot(0, i, opponent_color + special[i]) self.spots[6][i] = Spot(1, i, opponent_color + 'pawn') self.spots[0][i] = Spot(0, i, color + special[i]) self.spots[1][i] = Spot(1, i, color + 'pawn')
def getSpots(width, height, robots, obstacles, goal): # Set all spots in the room spots = [] for i in range(width): for j in range(height): spots.append(Spot(i, j, width, height)) # Set robot positions for robot in robots: x = robot[0] y = robot[1] spot_x, spot_y = spots[x*height + y].get_pos() if spot_x == x and spot_y == y: spots[x*height + y].set_robot(True) # Set obstacle positions for obstacle in obstacles: x = obstacle[0] y = obstacle[1] spot_x, spot_y = spots[x*height + y].get_pos() if spot_x == x and spot_y == y: spots[x*height + y].set_obstacle(True) # Set goal position x = goal[0] y = goal[1] spot_x, spot_y = spots[x*height + y].get_pos() if spot_x == x and spot_y == y: spots[x*height + y].set_goal(True) return spots
def main(): cols, rows = 10, 10 screen = Screen(500, 500, rows) screen.show() screen.makeGrid() grid = [[Spot(screen, i, j) for j in range(0, rows)] for i in range(0, cols)] for i in range(0, cols): for j in range(0, rows): grid[i][j].addNeigbors(grid, cols, rows) start = grid[0][0] start.obstacle = False stop = grid[cols - 1][rows - 1] stop.obstacle = False pathFinder = PathFinder(grid, start, stop) while True: for event in pygame.event.get(): if (event.type == pygame.QUIT): screen.close() result = pathFinder.findPath() pathFinder.showOpenSet() pathFinder.showPath() screen.update()
def __init__(self, maze_size, current_x=0, current_y=0, arrow=True, dead=False): self.knowledge = [] temp_list = [] # Create an empty maze that is the given size for y in range(maze_size): for x in range(maze_size): temp_list.append(Spot(x, y)) self.knowledge.append(temp_list) temp_list = [] self.current_x = current_x self.current_y = current_y self.arrow = arrow self.dead = dead self.has_gold = False self.wumpus_dead = False # Var with values 0 = wumpus location unknown and alive # 1 = Wumpus location known and alive # 2 = Wumpus location known and dead self.knows_wumpus = 0
def make_grid(rows, width): grid = [] gap = width // rows for i in range(rows): grid.append([]) for j in range(rows): spot = Spot(i, j, gap, rows) grid[i].append(spot) return grid
def __init__(self, p): self.current_puzzle = p.puzzle self.current_spot = Spot(self.current_puzzle) self.open = [self.current_spot] self.closed = [] self.h_option = input( "\nWhich heuristic do you want to use? (1,2,3)\n" "1. Hamming Distance (Number of incorrectly place tiles)\n" "2. Standard Manhattan distance\n" "3. Modified Manhattan distance\n")
def updateBoard(self, move): try: move = str(move) x1 = move[0] y1 = move[1] x2 = move[2] y2 = move[3] x1 = x1.upper() x2 = x2.upper() x1 = ord(x1) y1 = int(y1) x2 = ord(x2) y2 = int(y2) x1 = x1 - 65 y1 = 8 - y1 x2 = x2 - 65 y2 = 8 - y2 x1, y1 = y1, x1 x2, y2 = y2, x2 except IndexError: return # print(self.spots[0][0].piece.name) self.spots[x2][y2] = Spot(x2, y2, self.spots[x1][y1].piece.name, True) self.spots[x1][y1] = Spot(x1, y1, '.')
def random_walls(win, grid, rows, width): # creating a fresh grid grid = [] gap = width // rows for i in range(rows): grid.append([]) for j in range(rows): spot = Spot(i, j, gap, rows) # 'padding' the grid with random walls and weights if i % random.randint(1, 4) and j % random.randint(2, 4): rint = random.randint(1, 6) if rint < 6: spot.make_barrier() else: spot.make_weight() grid[i].append(spot) return grid
def __init__(self, window, parkinglot, cv2_img): self.window = window self.parkinglot = parkinglot #self.cv2_img = self.load_cv2_image(imgpath) self.cv2_img = cv2_img width, height, _ = self.cv2_img.shape self.dimensions = (height, width) tk.Canvas.__init__(self, master=window, width=self.dimensions[0], height=self.dimensions[1]) self.tk_img = self.get_imageTK_obj(self.cv2_img) self.create_image(0, 0, image=self.tk_img, anchor=tk.NW) self.image = self.tk_img self.current_points_list = [] # used for the box currently being drawn self.highlightedSpot = Spot('-1', [0, 0, 0, 0, 0, 0, 0, 0]) # bind canvas events # multiple functions can be bound to an event by using the 'add="+"' argument. TIL. self.bind('<B1-Motion>', self.draw_area) self.bind('<ButtonRelease-1>', self.create_rectangle, add="+") self.bind('<ButtonRelease-1>', self.update_all, add="+")
def __init__(self): self.spots = [[0 for x in range(8)] for y in range(8)] for i in range(self.h): for j in range(self.w): self.spots[i][j] = Spot(i, j, '.')
def init_maze(maze, size): temp_list = [] # Create an empty maze that is the given size for y in range(size): for x in range(size): temp_list.append(Spot(x, y)) maze.append(temp_list) temp_list = [] # First pick random spot for gold gold_x = 0 gold_y = 0 while gold_x == 0 and gold_y == 0: gold_x = randint(0, size - 1) gold_y = randint(0, size - 1) if gold_x != 0 or gold_y != 0: maze[gold_y][gold_x].gold = True # Next pick a spot for the wumpus wump_x = 0 wump_y = 0 while wump_x == 0 and wump_y == 0: wump_x = randint(0, size - 1) wump_y = randint(0, size - 1) if wump_x != 0 or wump_y != 0: maze[wump_y][wump_x].empty = False maze[wump_y][wump_x].wumpus = 2 # Set spots around wumpus to smelly if wump_x - 1 >= 0: maze[wump_y][wump_x - 1].stench = True if wump_y - 1 >= 0: maze[wump_y - 1][wump_x].stench = True if wump_x + 1 < len(maze): maze[wump_y][wump_x + 1].stench = True if wump_y + 1 < len(maze): maze[wump_y + 1][wump_x].stench = True # Now loop through each remaining spot and determine if it will be a pit for pit_y in range(size): for pit_x in range(size): if pit_y == 0 and pit_x == 0: continue test_int = randint(1, 10) # Test if the spot is within the 20% margin if test_int < 3 and maze[pit_y][ pit_x].empty and pit_x != gold_x and pit_y != gold_y: maze[pit_y][pit_x].empty = False maze[pit_y][pit_x].pit = 2 # Set spots around pit to breezy # Set spots around wumpus to smelly if pit_x - 1 >= 0: maze[pit_y][pit_x - 1].breeze = True if pit_y - 1 >= 0: maze[pit_y - 1][pit_x].breeze = True if pit_x + 1 < len(maze): maze[pit_y][pit_x + 1].breeze = True if pit_y + 1 < len(maze): maze[pit_y + 1][pit_x].breeze = True
def pre_process(self): ''' Pre-processing method Inputs: - Output: None ''' Spot.pre_process(self) # --------------------------------------- # CREATE # --------------------------------------- if self.inputs.getvalue('action_create') is not None: name = self.inputs.getvalue('name') OS_type = self.inputs.getvalue('OS_type') IP = self.inputs.getvalue('IP') path = self.inputs.getvalue('path') if all(x is not None for x in (name, OS_type, IP, path)): # Get location space # ... # Insert location self.data.DB.insert('Locations', name=name, OS_type=OS_type, IP=IP, path=path) # --------------------------------------- # MODIFY # --------------------------------------- if self.inputs.getvalue('action_modify') is not None: id = self.inputs.getvalue('id') name = self.inputs.getvalue('name') OS_type = self.inputs.getvalue('OS_type') IP = self.inputs.getvalue('IP') path = self.inputs.getvalue('path') if all(x is not None for x in (id, name, OS_type, IP, path)): # Update location self.data.DB.update('Locations', 'name', name, 'id=?', id) self.data.DB.update('Locations', 'IP', IP, 'id=?', id) self.data.DB.update('Locations', 'OS_type', OS_type, 'id=?', id) self.data.DB.update('Locations', 'path', path, 'id=?', id) # --------------------------------------- # DELETE # --------------------------------------- if self.inputs.getvalue('action_delete') is not None: id = self.inputs.getvalue('id') if id is not None: # Delete location self.data.DB.delete('Locations', 'id=?', id) # --------------------------------------- # UPDATE # --------------------------------------- if self.inputs.getvalue('action_update') is not None: pass
def setBoard(self): for row in range(8): self.board.append([]) self.board[row] = [None] * 8 # element 0, 0 of the board is A1 on the chess board and # element 8,8 of the board is H8 on the chess board self.board[0][0] = Spot(Rook("Rook", True), 0, 0) self.board[0][1] = Spot(Knight("Knight", True), 0, 1) self.board[0][2] = Spot(Bishop("Bishop", True), 0, 2) self.board[0][3] = Spot(Queen("Queen", True), 0, 3) self.board[0][4] = Spot(King("King", True), 0, 4) self.board[0][5] = Spot(Bishop("Bishop", True), 0, 5) self.board[0][6] = Spot(Knight("Knight", True), 0, 6) self.board[0][7] = Spot(Rook("Rook", True), 0, 7) for i in range(8): self.board[1][i] = Spot(Pawn("Pawn", True), 1, i) self.board[6][i] = Spot(Pawn("Pawn", False), 6, i) for emptyI in range(2, 6): for j in range(8): self.board[emptyI][j] = 0 self.board[7][0] = Spot(Rook("Rook", False), 7, 0) self.board[7][1] = Spot(Knight("Knight", False), 7, 1) self.board[7][2] = Spot(Bishop("Bishop", False), 7, 2) self.board[7][3] = Spot(Queen("Queen", False), 7, 3) self.board[7][4] = Spot(King("King", False), 7, 4) self.board[7][5] = Spot(Bishop("Bishop", False), 7, 5) self.board[7][6] = Spot(Knight("Knight", False), 7, 6) self.board[7][7] = Spot(Rook("Rook", False), 7, 7)
def search(self): """ Performs A* search on the puzzle """ while len(self.open) != 0: # If the puzzle is not the in the goal state if not Puzzle.is_puzzle_solved(self.current_puzzle): # Find the spot with the best F score winner_index = 0 for i, o in enumerate(self.open): if self.open[i].f < self.open[winner_index].f: winner_index = i # Current spot becomes the the winner self.current_spot = self.open[winner_index] self.current_puzzle = self.current_spot.puzzle # Remove from open current from open list self.remove_puzzle_from_spots_list(self.open, self.current_spot.puzzle) # Add to closed list self.closed.append(self.current_spot) # Write to txt file pos = self.current_puzzle.index(0) if self.h_option == "1": txt_output = a_star_h1_output elif self.h_option == "2": txt_output = a_star_h2_output elif self.h_option == "3": txt_output = a_star_h3_output else: raise Exception("Invalid heuristic option.") Puzzle.write_to_txt(txt_output, Puzzle.get_tile_letter(pos), self.current_puzzle) # Generate the possible moves possible_moves = Puzzle.get_possible_moves(self.current_puzzle) children = [] for move in possible_moves: child = Spot(Puzzle.move(move, self.current_puzzle)) children.append(child) # Remove child if it is in the open or closed list to_remove = [] for child in children: for o in self.open: if list(child.puzzle) == list(o.puzzle): to_remove.append(child) for c in self.closed: if list(child.puzzle) == list(c.puzzle): if child not in to_remove: to_remove.append(child) for r in to_remove: children.remove(r) # Loop through all the neighbors for neighbor in children: found_in_close = False for c in self.closed: if list(neighbor.puzzle) == list(c.puzzle): found_in_close = True # Continue to the next neighbor if not found_in_close: temp_g = self.current_spot.g + 1 # Increment g(n) found_in_open = False for o in self.open: if list(neighbor.puzzle) == list(o.puzzle): if temp_g < neighbor.g: # Give new g score, because I got there faster than the previous path neighbor.g = temp_g found_in_open = True if not found_in_open: neighbor.g = temp_g self.open.append(neighbor) # Calculate f(n) of neighbor neighbor.h = self.get_h(list(neighbor.puzzle)) neighbor.f = neighbor.g + neighbor.h else: return # Puzzle is solved print(self.current_spot.puzzle)
grid = [] openSet = [] closedSet = [] path = [] saved_path = [] w = width / cols h = height / rows for i in range(cols): grid.append([]) for i in range(cols): for j in range(rows): grid[i].append(Spot(i, j, w, h, rows, cols)) for i in range(cols): for j in range(rows): grid[i][j].add_neighbors(grid, allow_diagonals) start = grid[0][0] end = grid[cols - 1][rows - 1] start.wall = False end.wall = False # Show buttons randomize = Button(width + 25, 30 + 0 * 55, 150, 50, 'Random', (255, 0, 0)) clear = Button(width + 25, 30 + 1 * 55, 150, 50, 'Clear', (0, 0, 255)) start = Button(width + 25, 30 + 3 * 55, 150, 50, 'A*', (255, 5, 250)) start_bfs = Button(width + 25, 30 + 4 * 55, 150, 50, 'BFS', (255, 5, 250))
def fill_spots(self, spots, type, num_spot): num_spot += 1 row = int(round(num_spot / Level.SPOTS_PER_ROW)) spot = Spot(num_spot, self.no, row, type) spots[row][type].append(spot)