Exemplo n.º 1
0
    def resetFoodPosition(self):
        whereFood = np.where(self.matrix == FOOD)

        self.matrix[whereFood[0][0], whereFood[1][0]] = 0

        xFood, yFood = self.randomizeXY()
        foodCartesianState = states.CartesianState(xFood, yFood)

        isOverlapped = False

        for eachBodyPart in self.snake.getBody():
            if self.isOverlapped(foodCartesianState, eachBodyPart):
                isOverlapped = True

        if isOverlapped:
            while isOverlapped:
                xFood, yFood = self.randomizeXY()
                foodCartesianState = states.CartesianState(xFood, yFood)
                isOverlapped = False
                for eachBodyPart in self.snake.getBody():
                    if self.isOverlapped(foodCartesianState, eachBodyPart):
                        isOverlapped = True

        self.updateFoodState(foodCartesianState)

        return foodCartesianState, xFood, yFood
Exemplo n.º 2
0
    def updateSnakeState(self, xHead, yHead, xFood, yFood):
        snakeCartesianState = states.CartesianState(xHead, yHead)
        direction_ = self.getDirectionOfMovementOfSnake()
        polarStateSnakeRelatedToFood = geometry.generatePolarState2(
            xHead, yHead, xFood, yFood, direction_)

        lineOfSightState = geometry.getLineOfSight(self.gridWidth,self.gridHeight,\
        self.strengthLineOfSight,\
        snakeCartesianState,direction_,self.matrix,self.rangeLineOfSight)

        stateSnake = (polarStateSnakeRelatedToFood, lineOfSightState)
        self.snake.setState(stateSnake)

        return stateSnake
Exemplo n.º 3
0
    def resetPositions(self, snakeInitialLength):
        self.matrix.fill(0)
        self.snake.resetBody()
        direction = self.randomizeDirection()

        for index in range(snakeInitialLength):
            if index == 0:
                #set head of the snake
                xHead, yHead = self.randomizeXY()
                headCartesianState = states.CartesianState(xHead, yHead)
                self.matrix[yHead][xHead] = SNAKE
                self.snake.setHeadPosition(headCartesianState)

                #if direction is not valid, choose another one randomly until it is ok
                if self.verifyValidGrowingDirections(
                        direction, headCartesianState,
                        snakeInitialLength) == False:
                    while self.verifyValidGrowingDirections(
                            direction, headCartesianState,
                            snakeInitialLength) == False:
                        direction = self.randomizeDirection()
            else:
                lastBodyPart = index - 1
                #set other body parts
                if direction == 'left':
                    xBodyPart = self.snake.getBody()[lastBodyPart].getPosition(
                    ).getX() - 1
                    bodyCartesianState = states.CartesianState(
                        xBodyPart, yHead)
                    self.matrix[yHead][xBodyPart] = SNAKE
                    self.snake.addNewBodyPart(bodyCartesianState)

                elif direction == 'up':
                    yBodyPart = self.snake.getBody()[lastBodyPart].getPosition(
                    ).getY() - 1
                    bodyCartesianState = states.CartesianState(
                        xHead, yBodyPart)
                    self.matrix[yBodyPart][xHead] = SNAKE
                    self.snake.addNewBodyPart(bodyCartesianState)

                elif direction == 'right':
                    xBodyPart = self.snake.getBody()[lastBodyPart].getPosition(
                    ).getX() + 1
                    bodyCartesianState = states.CartesianState(
                        xBodyPart, yHead)
                    self.matrix[yHead][xBodyPart] = SNAKE
                    self.snake.addNewBodyPart(bodyCartesianState)

                else:
                    yBodyPart = self.snake.getBody()[lastBodyPart].getPosition(
                    ).getY() + 1
                    bodyCartesianState = states.CartesianState(
                        xHead, yBodyPart)
                    self.matrix[yBodyPart][xHead] = SNAKE
                    self.snake.addNewBodyPart(bodyCartesianState)

                xFood, yFood = self.randomizeXY()
                foodCartesianState = states.CartesianState(xFood, yFood)

                isOverlapped = False

                for eachBodyPart in self.snake.getBody():
                    if self.isOverlapped(foodCartesianState, eachBodyPart):
                        isOverlapped = True

                if isOverlapped:
                    while isOverlapped:
                        xFood, yFood = self.randomizeXY()
                        foodCartesianState = states.CartesianState(
                            xFood, yFood)
                        isOverlapped = False
                        for eachBodyPart in self.snake.getBody():
                            if self.isOverlapped(foodCartesianState,
                                                 eachBodyPart):
                                isOverlapped = True

                foodCartesianState = states.CartesianState(xFood, yFood)
                self.matrix[yFood, xFood] = FOOD
                #snake.updateBody(self.snake)
                #set food
                #foodCartesianState,xFood,yFood = self.resetFoodPosition()
                self.updateFoodState(foodCartesianState)
                self.updateSnakeState(xHead, yHead, xFood, yFood)
Exemplo n.º 4
0
	def __init__(self,x,y):
		self.cartesianState = states.CartesianState(x,y)
Exemplo n.º 5
0
	def grow(self,direction):
		self.body.append(states.CartesianState(self.lastTail.getX(),self.lastTail.getY()))
		self.lastTail = self.body[-1]
Exemplo n.º 6
0
	def moveDown(self):
		x = self.getHead().getX()
		y = self.getHead().getY()
		headNewState = states.CartesianState(x,y+1)
		self.lastTail = self.body[-1]
		self.updateBody(headNewState)
Exemplo n.º 7
0
	def moveRight(self):
		x = self.getHead().getX()
		y = self.getHead().getY()
		headNewState = states.CartesianState(x+1,y)
		self.lastTail = self.body[-1]
		self.updateBody(headNewState)