Exemplo n.º 1
0
    def move_less_one(self, state):
        game = Game(state)
        us = None
        ourPos = (0, 0)
        for it in game.heroes:
            if it.name == "Rimouski City Gang":
                us = it
                ourPos = (it.pos["x"], it.pos["y"])
                break

        dist = float('infinity')
        min = None
        min_pos = None
        i = 0
        id = game.board.tiles[ourPos[0]][ourPos[1]].id
        for x, y in game.customers_locs:
            customer = game.customers[i]
            pos = (x, y)
            tmp = ((pos[0] - ourPos[0])**2) + ((pos[1] - ourPos[1])**2)
            if dist > tmp:
                min = customer
                dist = tmp
                min_pos = pos
        friesOk = us.fries >= min.french_fries
        burgerOk = us.burger >= min.burger
        if friesOk and burgerOk:
            self.going = min_pos
        else:
            minF = None
            minB = None
            distF = float('infinity')
            distB = float('infinity')
            if not friesOk:
                for x, y in game.fries_locs:
                    fries = game.board.tiles[x][y]
                    tmp = ((x - ourPos[0])**2) + ((y - ourPos[1])**2)
                    print(fries.hero_id, id, tmp, distF)
                    if fries.hero_id != id and tmp < distF:
                        distF = tmp
                        minF = (x, y)
            if not burgerOk:
                for x, y in game.burger_locs:
                    burger = game.board.tiles[x][y]
                    tmp = ((x - ourPos[0])**2) + ((y - ourPos[1])**2)
                    if burger.hero_id != id and tmp < distB:
                        distB = tmp
                        minB = (x, y)
            self.going = (minF if distF < distB else minB)

        if self.going is None:
            print("AAA")
            return random_choice()

        ret = pathfinding(state['game']['board']['tiles'], game.board, ourPos,
                          self.going)
        if ret is None:
            print("BBB")
            return random_choice()
        res = AIM[ret]
        if res[0] + ourPos[0] == self.going[
                0] and res[1] + ourPos[1] == self.going[1]:
            self.going = None
        print(ret)
        return ret
Exemplo n.º 2
0
	
#Same building, different level (only up to 1 level difference)
elif (src_building == dest_building and 
		src_level != dest_level):
	print ("TODO")
	
#Different building, same level
elif (src_building != dest_building and 
		src_level == dest_level):
	print ("TODO")
	
#Different building, different level
else:
	print ("TODO")
		
print ("Shortest path: ", shortest_path)

#generate the list of nodes of the shortest path
nodes = []
for node in shortest_path:
	nodes.append(get_node_by_id(maps['COM1']['2']['map'], node))

while (1):
	currX = int(input("CurrX: "))
	currY = int(input("CurrY: "))
	heading = int(input("Heading: "))
	
	rv = pathfinding(nodes, int(maps['COM1']['2']['info']['northAt']), currX, currY, heading)
	
	print ("Dist: ", rv[0])
	print ("Turning angle: ", rv[1])
Exemplo n.º 3
0
# Randomly choose day of the week
day = random.choice(list(collection_schedule))

# Classify garbage and collect it
print("Day of the week: {}, collecting {}".format(day, str(collection_schedule[day]).strip('[]')))
print("List of classifications for garbage: ")
for house in temp_houses_set:
    spot_visited += 1
    classification = IC.read_model(house.garbage)
    print(classification)
    if classification.split(' ', 1)[0] in collection_schedule[day]:
        collected_garbage.append("Collected {} with certaintity of {} for picture {} for node of coordinates x: {}  y: {}".format(classification.split(' ', 1)[0], classification.split(' ', 1)[1], house.garbage, house.x, house.y))

if len(collected_garbage) != 0:
    print("List of collected garbage: ")
    print(*collected_garbage,sep='\n')
    print("Collected {}\n".format(len(collected_garbage)))
else:
    print("Unfortunately today we did not collect any garbage")

print("Total number of visited houses: {}\nTotal fuel consumption: {}L".format(spot_visited,int(total_fuel_consumption)))

# Generate pygame graphics for given grid and path generated by Astar
# astar_path = Astar(start, target, _grid)
PF.pathfinding(_grid, astar_path)



#TODO osobne okienko z info o obrazkach, zbieranych smieciach danego dnia oraz lista zebranych smieci

#template: Nazwa pliku + co zwrocilo + z jaka pewnoscia + coord wierzcholka?
			places.update({k : v})
			
building = places["Source"][0]
level = places["Source"][1]
srcNode = places["Source"][2]
destNode = places["Destination"][2]

adj_list = myMM.generate_adj_list(building,level)
if bool(adj_list):
	shortest_path = dijkstra.dijkstra(adj_list, srcNode, destNode)
	print (shortest_path)

	#Only for the prototype testing
	if TEST_TYPE == 2:
		nodes = []
		northAt = int(myMM.get_map(building, level)["info"]["northAt"])

		#Get details of all nodes in the path
		for node in shortest_path:
			nodes.append(myMM.get_node(building, level, node))

		while (1):
			currX = int(input("Current X: "))
			currY = int(input("Current Y: "))
			heading = int(input("Heading: "))
			
			rv = pf.pathfinding(nodes, northAt, currX, currY, heading)
			
			print ("Direction: (%.1f, %.1f)" %  (rv[1], rv[0]))
else:
	print("No path found.")
Exemplo n.º 5
0
	def main(self):
		#print('GAME!')
		self.sprites = maps.Map_Handler.loadImgs(self)

		clock = pygame.time.Clock()

		mapList, rectPos = self.handler.loadMap('testmap.txt')
		#print(mapList)
		#print(rectPos)

		pygame.display.flip()

		playerPosX = 1
		playerPosY = 1
		playerDir = 'D'

		slimePosX = 10
		slimePosY = 10
		slimeDir = 'D'

		color_dict = {}
		colors = [['red', (255, 0, 0)], ['green', (0, 255, 0)], ['blue', (0, 0, 255)], ['black', (0, 0, 0, 255)]]

		for color in colors:
			color_dict[color[0]] = color[1]

		while True:
			limit = clock.tick(150)
			#print(limit)
			self.screen.fill((255, 255, 255))
			mouse_posx, mouse_posy = pygame.mouse.get_pos()
			mouse_posx = mouse_posx // self.tile_size * self.tile_size
			mouse_posy = mouse_posy // self.tile_size * self.tile_size

			tile_center_x = mouse_posx + self.tile_size // 2
			tile_center_y = mouse_posy + self.tile_size // 2

			tile_pos_x = mouse_posx // self.tile_size
			tile_pos_y = mouse_posy // self.tile_size
			
			tile = pygame.Rect(mouse_posx, mouse_posy, self.tile_size, self.tile_size)

			event = pygame.event.poll()

			for rects in rectPos:
				if rects[2] == 'X':
					pygame.draw.rect(self.screen, color_dict['black'], rects[0])
				elif rects[2] == 'S':
					pygame.draw.rect(self.screen, color_dict['red'], rects[0])
				elif rects[2] == 'O':
					pygame.draw.rect(self.screen, color_dict['green'], rects[0])
				elif rects[2] == 'G':
					self.screen.blit(self.sprites['G'], rects[0])
			#print(slimePosX, slimePosY)
			self.slime.draw(slimeDir, slimePosX, slimePosY)
			self.character.draw(playerDir, playerPosX, playerPosY)

			if event.type == pygame.QUIT:
				self.screen.fill((255,255,255))
				return True

			elif event.type == pygame.MOUSEBUTTONDOWN:
				if pygame.mouse.get_pressed()[0]:
					#print('pressed', tile_pos_x, tile_pos_y)
					if mapList[tile_pos_y][tile_pos_x] != 'X':
						#for i in mapList:
						#	print(i)
						path = pathf.pathfinding(mapList, (playerPosX, playerPosY), (tile_pos_x, tile_pos_y))
						#print(len(path), path)
						'''for step in range(1, len(path)):
							if step < len(path):
								print('Current pos:', slimePosX, slimePosY, ';', 'Next step:',path[step])
								if slimePosX > path[step][0]:
									slimeDir = 'L'
									slimePosX -= 1
									print(slimeDir)
								elif slimePosX < path[step][0]:
									slimeDir = 'R'
									slimePosX += 1
									print(slimeDir)
								elif slimePosY > path[step][1]:
									slimeDir = 'U'
									slimePosY -= 1
									print(slimeDir)
								elif slimePosY < path[step][1]:
									slimeDir = 'D'
									slimePosY += 1
									print(slimeDir)'''

						for step in range(1, len(path)):
							if step < len(path):
								#print('Current pos:', playerPosX, playerPosY, ';', 'Next step:',path[step])
								if playerPosX > path[step][0]:
									playerDir = 'L'
									playerPosX -= 1

									#print(playerDir)
								elif playerPosX < path[step][0]:
									playerDir = 'R'
									playerPosX += 1
									#print(playerDir)
								elif playerPosY > path[step][1]:
									playerDir = 'U'
									playerPosY -= 1
									#print(playerDir)
								elif playerPosY < path[step][1]:
									playerDir = 'D'
									playerPosY += 1
									#print(playerDir)										
									

									print(playerDir)
								elif playerPosX < path[step][0]:
									playerDir = 'R'
									playerPosX += 1
									print(playerDir)
								elif playerPosY > path[step][1]:
									playerDir = 'U'
									playerPosY -= 1
									print(playerDir)
								elif playerPosY < path[step][1]:
									playerDir = 'D'
									playerPosY += 1
									print(playerDir)										

							#self.slime.draw(slimeDir, slimePosX, slimePosY)

							self.character.draw(playerDir, playerPosX, playerPosY)
							pygame.display.update()
							pygame.time.delay(20)

			elif event.type == pygame.KEYDOWN:
				if event.key == pygame.K_ESCAPE:
					self.screen.fill((255, 255, 255))
					return 1

			pygame.display.update()
Exemplo n.º 6
0
run = True
while run:
    mouse.position = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        #left click
        if pygame.mouse.get_pressed()[0]:
            mouse.left_click(grid.cells, buttons)
        #right click
        if pygame.mouse.get_pressed()[2]:
            mouse.right_click(grid.cells)

    #start visualisation
    if mouse.state >= 10:
        pathfinding(mouse, grid)
        mouse.state = CellState["SEARCH_FINISHED"]

    #clear grid
    if mouse.state == CellState["CLEAR"]:
        grid.clear()
    #drawing goes below here
    #draw cell grid
    for row in grid.cells:
        for cell in row:
            cell.Draw(screen)
    border.Draw(screen)

    #draw buttons
    for button in buttons:
        button.Draw(screen)