Пример #1
0
class LangtonsAnt:
    def __init__(
        self, initial_state: List[List[bool]], start_position: List[int]
    ) -> None:
        self._grid = Grid(initial_state)
        self._ant = Ant(start_position[0], start_position[1])

    def next(self) -> bool:
        current_ant_position_x, current_ant_position_y = self._ant.position

        if self._grid.get_color(current_ant_position_x, current_ant_position_y):
            self._grid.change_color(current_ant_position_x, current_ant_position_y)
            self._ant.turn(TurnDirection.RIGHT)
            self._ant.move()
        else:
            self._grid.change_color(current_ant_position_x, current_ant_position_y)
            self._ant.turn(TurnDirection.LEFT)
            self._ant.move()

        return self.validate_move()

    @property
    def state(self):
        return self._grid.state

    @property
    def ant_position(self):
        return self._ant.position

    def validate_move(self) -> bool:
        current_ant_position_x, current_ant_position_y = self._ant.position
        return self._grid.validate_point(current_ant_position_x, current_ant_position_y)

    def validate(self) -> bool:
        return self._grid.validate()
Пример #2
0
class Sim(object):
    def __init__(self):
        G.state = np.zeros((G.numBlocksX, G.numBlocksY), dtype=np.int)
        G.weight = np.ones((G.numBlocksX, G.numBlocksY))
        self.antId = 0
        if G.DeterministicAnts:
            self.ant = Pyramid(self.antId)
            self.y = self.ant.y
            self.oldy = self.y
        else:
            self.ant = Ant(self.antId)
        self.numAnts = 1
        self.maxHeight = 0

        G.jointData = np.zeros((G.numBlocksX * G.numBlocksY * 3))
        G.numJoints = 0
        G.jointRef = {}

    def step(self):
        if not self.ant.settled:
            try:
                self.ant.move()
            except Error as e:
                raise e
        else:
            if self.ant.y > self.maxHeight:
                self.maxHeight = self.ant.y
            if self.checkBridge():
                return False
            self.addJoints(self.ant)
            self.antId = self.antId + 1
            self.numAnts += 1
            if G.DeterministicAnts:
                self.ant = Pyramid(self.antId)
                self.oldy = self.y
                self.y = self.ant.y
                if self.y != self.oldy:
                    Physics.resetPhysics()
                    Physics.checkPhysics()
            else:
                self.ant = Ant(self.antId)
                Physics.resetPhysics()
                Physics.checkPhysics()
            self.updateShaking()
        return True

    def addJoints(self, ant):
        for adjacent in getAdjacent(ant.pos):
            if G.state[adjacent]:
                Joint(ant.pos, adjacent, -1)

            # attach anchor joints
        if ant.y == 0:
            Joint(ant.pos, (ant.x, ant.y - 1), -1)
            Joint(ant.pos, (ant.x - 1, ant.y - 1), -1)
            Joint(ant.pos, (ant.x + 1, ant.y - 1), -1)

    def getForces(self, (x, y)):
        return [f.force() for f in G.jointRef[(x, y)]]
Пример #3
0
def test_ant_do_move():
    ant = Ant(1, 1)
    x, y = ant.position
    assert x == 1
    assert y == 1
    ant.move()
    x, y = ant.position
    assert x == 1
    assert y == 0
    ant.turn(TurnDirection.RIGHT)
    ant.move()
    x, y = ant.position
    assert x == 2
    assert y == 0
Пример #4
0
def find_path(start_node, end_node):

    ant = Ant(start_node, end_node)

    while ant.current_node != ant.end_node:
        # append its current node to its path
        ant.construct_path()
        # update the state of its current node
        ant.update_node_state()
        # choose the next node from available nodes
        available_nodes = check_reachability(ant.previous_node, ant.current_node, ant.end_node)
        next_node = ant.choose_next_node(available_nodes)
        # move to the next node
        ant.move(next_node)
    else:
        ant.path.append(ant.current_node)

    return ant.path
Пример #5
0
def main():
    pygame.init()
    settings = Settings()
    screen = pygame.display.set_mode(
        (settings.screen_width, settings.screen_height))
    # icon = pygame.image.load('image/icon.png')
    # pygame.display.set_icon(icon)
    pygame.display.set_caption("水波")

    font1 = pygame.freetype.Font(r'C:\Windows\Fonts\msyh.ttc', 36)
    ant = Ant(screen, settings, (400, 300), 10)

    fps = 50
    fclock = pygame.time.Clock()

    # 开始游戏主循环
    while True:
        # 监听键盘和鼠标事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit(0)  # 此处必须带有参数0
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    sys.exit(0)
            elif event.type == pygame.MOUSEBUTTONDOWN:
                pos = event.pos
                w = Wave(screen, settings, pos)

        screen.fill(settings.bg_color)
        try:
            w.spread()
            w.blitme()
        except Exception as e:
            pass
        ant.move()
        ant.blitme()
        pygame.display.update()
        fclock.tick(fps)
Пример #6
0
		
		directions	= ant.getPossibleDirections()
		count = 0
		for direction in directions:
			possibleDirection 	= ant.DIRECTIONS[ant.DNAMES.index(directions[count])]
			possibleCoordinates = addCoordinates(ant.location, possibleDirection)
			if possibleCoordinates in antGrid.storedIndex:
				index = antGrid.storedIndex.index(possibleCoordinates)
				directions.remove(directions[count])
			count += 1
						
		rand = randint(0, (len(directions)- 1))
		
		antGrid.storePoint(ant.location, False, 0, 0)
		

		ant.move( ant.DIRECTIONS[ant.DNAMES.index(directions[rand])] )
		
		antGrid.storePoint(ant.location, True, 250, 0)
				
#	if iterations > 3:		# stop simulation after x iterations
#		running = False		# (for debugging purposes)

	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			running = False

	pygame.display.flip()
	
	iterations += 1
Пример #7
0
screen = pygame.display.set_mode((700, 700))
pygame.display.set_caption("langton's ant")
clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            break

    # Move the ant
    row, column = ant.position  # Get the current state of the cell
    if row >= 70 or column >= 70:
        continue
    else:
        ant.move(grid[row, column])
        # Update the cell state
        if grid[row, column] == 0:
            grid[row, column] = 1
        else:
            grid[row, column] = 0

    screen.fill(BLACK)

    # change color each frame
    for row in range(70):
        for column in range(70):
            if [row, column] == ant.position:
                color = RED
            elif grid[row, column] == 0:
                color = BLACK
Пример #8
0
from ant import Ant
import turtle

ant1 = Ant()
window = turtle.Screen()
window.bgcolor('white')
window.screensize(1000, 1000)
maps = {}

while True:
    ant1.move(maps)

Пример #9
0
        directions = ant.getPossibleDirections()
        count = 0
        for direction in directions:
            possibleDirection = ant.DIRECTIONS[ant.DNAMES.index(
                directions[count])]
            possibleCoordinates = addCoordinates(ant.location,
                                                 possibleDirection)
            if possibleCoordinates in antGrid.storedIndex:
                index = antGrid.storedIndex.index(possibleCoordinates)
                directions.remove(directions[count])
            count += 1

        rand = randint(0, (len(directions) - 1))

        antGrid.storePoint(ant.location, False, 0, 0)

        ant.move(ant.DIRECTIONS[ant.DNAMES.index(directions[rand])])

        antGrid.storePoint(ant.location, True, 250, 0)

#	if iterations > 3:		# stop simulation after x iterations
#		running = False		# (for debugging purposes)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    pygame.display.flip()

    iterations += 1