我需要使用 deap 玩蛇游戏的 ai 代理的完整代码

问题描述 投票:0回答:0

以下是贪吃蛇游戏的代码: 随机导入 导入时间 进口龟

XSIZE = YSIZE = 16 # 每个方向的网格单元数(不要改变这个)

无头=假

类显示游戏: def init(自我,XSIZE,YSIZE): # 屏幕 self.win = turtle.Screen() self.win.title("EVCO 贪吃蛇游戏") self.win.bgcolor("灰色") self.win.setup(宽度=(XSIZE20)+40,高度=(YSIZE20)+40) #self.win.screensize((XSIZE20)+20,(YSIZE20)+20) self.win.tracer(0)

    #Snake Head
    self.head = turtle.Turtle()
    self.head.shape("square")
    self.head.color("black")

    # Snake food
    self.food = turtle.Turtle()
    self.food.shape("circle")
    self.food.color("yellow")
    self.food.penup()
    self.food.shapesize(0.55, 0.55)
    self.segments = []

def reset(self, snake):
    self.segments = []
    self.head.penup()
    self.food.goto(-500, -500)
    self.head.goto(-500, -500)
    for i in range(len(snake)-1):
        self.add_snake_segment()
    self.update_segment_positions(snake)
   
def update_food(self,new_food):
    self.food.goto(((new_food[1]-9)*20)+20, (((9-new_food[0])*20)-10)-20)
    
def update_segment_positions(self, snake):
    self.head.goto(((snake[0][1]-9)*20)+20, (((9-snake[0][0])*20)-10)-20)
    for i in range(len(self.segments)):
        self.segments[i].goto(((snake[i+1][1]-9)*20)+20, (((9-snake[i+1][0])*20)-10)-20)

def add_snake_segment(self):
    self.new_segment = turtle.Turtle()
    self.new_segment.speed(0)
    self.new_segment.shape("square")
    self.new_segment.color(random.choice(["green",'black','red','blue']))
    self.new_segment.penup()
    self.segments.append(self.new_segment)

类蛇: def init(self, _XSIZE, _YSIZE): self.XSIZE = _XSIZE self.YSIZE = _YSIZE 自我重置()

def reset(self):
    self.snake = [[8,10], [8,9], [8,8], [8,7], [8,6], [8,5], [8,4], [8,3], [8,2], [8,1],[8,0] ]# Initial snake co-ordinates [ypos,xpos]    
    self.food = self.place_food()
    self.ahead = []
    self.snake_direction = "right"

def place_food(self):
    self.food = [random.randint(1, (YSIZE-2)), random.randint(1, (XSIZE-2))]
    while (self.food in self.snake):
        self.food = [random.randint(1, (YSIZE-2)), random.randint(1, (XSIZE-2))]
    return( self.food )

def update_snake_position(self):
    self.snake.insert(0, [self.snake[0][0] + (self.snake_direction == "down" and 1) + (self.snake_direction == "up" and -1), self.snake[0][1] + (self.snake_direction == "left" and -1) + (self.snake_direction == "right" and 1)])

def food_eaten(self):
    if self.snake[0] == self.food:                                            # When snake eats the food
        return True
    else:    
        last = self.snake.pop()  # [1] If it does not eat the food, it moves forward and so last tail item is removed
        return False
        
def snake_turns_into_self(self):
    if self.snake[0] in self.snake[1:]:
        return True
    else:
        return False

def snake_hit_wall(self):
    if self.snake[0][0] == 0 or self.snake[0][0] == (YSIZE-1) or self.snake[0][1] == 0 or self.snake[0][1] == (XSIZE-1):
        return True
    else:
        return False

# Example sensing functions
def getAheadLocation(self):
    self.ahead = [ self.snake[0][0] + (self.snake_direction == "down" and 1) + (self.snake_direction == "up" and -1), self.snake[0][1] + (self.snake_direction == "left" and -1) + (self.snake_direction == "right" and 1)] 
    
def sense_wall_ahead(self):
    self.getAheadLocation()
    return( self.ahead[0] == 0 or self.ahead[0] == (YSIZE-1) or self.ahead[1] == 0 or self.ahead[1] == (XSIZE-1) )
    
def sense_food_ahead(self):
    self.getAheadLocation()
    return self.food == self.ahead

def sense_tail_ahead(self):
    self.getAheadLocation()
    return self.ahead in self.snake

snake_game = snake(XSIZE,YSIZE) 如果不是无头的: 显示 = DisplayGame(XSIZE,YSIZE) def run_game(显示,snake_game,无头):

score = 0
snake_game.reset()
if not headless:
    display.reset(snake_game.snake)
    display.win.update()
snake_game.place_food()
game_over = False
snake_direction = "right"

flag = True
while not game_over:

    # ****YOUR AI BELOW HERE******************

    # Here is a very silly random snake controller. It moves with a correlated random walk, and the only sensible decision it makes is not to turn directly back on itself (possible in this game)
    # *** Replace this with your evolved controller here to decide on the direction the snake should take*
    # snake_direction = "down" / snake_direction = "up" / snake_direction = "left" / snake_direction = "right"
    #if random.random() < 1:
    if snake_direction == "left":
        new_snake_direction = random.choice(["left","up","down"])
    elif snake_direction == "right":
        new_snake_direction = random.choice(["right","up","down"])
    elif snake_direction == "up":
        new_snake_direction = random.choice(["left","up","right"])
    elif snake_direction == "down":
        new_snake_direction = random.choice(["left","down","right"])
    #snake_direction = new_snake_direction            
    #snake_game.snake_direction = snake_direction
    
    # Here is an example sensing function
    if snake_game.sense_tail_ahead():
        print("Tail ahead!!!!")
        time.sleep(0.5)
    
    # ****YOUR AI ABOVE HERE******************
        
    snake_game.update_snake_position()

    # Check if food is eaten
    if snake_game.food_eaten():
        snake_game.place_food()
        score += 1
        if not headless: display.add_snake_segment()

    # Game over if the snake runs over itself
    if snake_game.snake_turns_into_self():
        game_over = True
        print("Snake turned into itself!")

    # Game over if the snake goes through a wall
    if snake_game.snake_hit_wall():
        game_over = True
        print("Snake hit a wall!")

    if not headless:       
        display.update_food(snake_game.food)
        display.update_segment_positions(snake_game.snake)
        display.win.update()
        time.sleep(0.2) # Change this to modify the speed the game runs at when displayed.

print("\nFINAL score - " + str(score))
print()
if not headless: turtle.done()

run_game(display,snake_game, headless=HEADLESS)

我不知道该怎么做。我需要一个完整的 ai 代理代码,它只使用 deap 玩给定的贪吃蛇游戏。

python-3.x neural-network artificial-intelligence evolutionary-algorithm mlp
© www.soinside.com 2019 - 2024. All rights reserved.