Ejemplo n.º 1
0
 def add_block(self, position=None):
     block = Figure(self.board)
     block.bindStrategy(FlagStrategy())
     if position != None:
         block.strategy.placeIt(y=position[0], x=position[1], soft=True)
     else:
         block.strategy.placeIt(soft=True)
     block.color = -1
     return block
Ejemplo n.º 2
0
 def setup(self):
     for n in range(self.amount):
         figure = Figure(self.board)
         figure.bindStrategy(self.figureStrategyFactory())
         figure.strategy.placeIt()
     self.board.figures[0].color = 1
Ejemplo n.º 3
0
class NaviGame(BoardGame):
    def __init__(self,
                 height=4,
                 width=4,
                 goal=None,
                 obstacles=0,
                 moving_target=False,
                 goal_idle=3,
                 display_str="NaviGame"):
        self.board = Board(height, width)
        if goal == None:
            self.goal = (int(height / 2), int(width / 2))
        else:
            self.goal = goal
        self.obstacle_count = obstacles
        self.board.obstacles = []
        self.moving_target = moving_target
        self.goal_idle = goal_idle
        self.display_str_base = display_str
        self.score = 0

    def setup(self, s=None):
        # setup obstacles
        for i in range(self.obstacle_count):
            obs = self.add_block()
            self.board.obstacles.append(obs)
        # setup flag
        self.Flag = Figure(self.board)
        self.Flag.bindStrategy(FlagStrategy())
        if self.goal == None:
            self.Flag.strategy.placeIt()
            self.goal = self.Flag.position()
        else:
            self.Flag.strategy.placeIt(y=self.goal[0], x=self.goal[1])
        self.Flag.color = 2
        # setup navigator
        self.Navigator = Figure(self.board)
        if s == None: s = NaviStrategy(goal=(self.goal[0], self.goal[1]))
        self.Navigator.bindStrategy(s)
        self.Navigator.strategy.placeIt()
        self.Navigator.color = 3
        _, dist_test = self.Navigator.strategy.get_input()
        if dist_test < self.board.height - 1:
            self.reset()

    def reset(self):
        self.shift_obstacles()
        self.shift_goal()
        self.shift_figure(self.Navigator)
        self.score = 0
        _, dist_test = self.Navigator.strategy.get_input()
        if dist_test < 3:
            self.reset()

    def shift_goal(self, goal=None, figure=None):
        if goal == None:
            goal = (randint(0, self.board.height - 1),
                    randint(0, self.board.width - 1))
        try:
            self.goal = goal
            self.Flag.move(y=goal[0], x=goal[1], relative=False)
            if figure == None:
                self.Navigator.strategy.goal = goal
            else:
                figure.strategy.goal = goal
        except:
            self.shift_goal()

    def shift_obstacles(self):
        for obs in self.board.obstacles:
            self.shift_figure(obs)

    def shift_figure(self, figure):
        new_pos_x = randint(0, self.board.width - 1)
        new_pos_y = randint(0, self.board.height - 1)
        try:
            figure.move(y=new_pos_y, x=new_pos_x, relative=False)
        except:
            self.shift_figure(figure)

    def step(self):
        for figure in self.board.figures:
            figure.strategy.step()
        if (self.Navigator.strategy.at_goal > self.goal_idle) \
                        and (self.moving_target == True):
            self.shift_goal()
            self.Navigator.strategy.at_goal = 0
        if (self.Navigator.strategy.at_goal > 0):
            self.score += 1
        else:
            self.score += -1
        self.display_str = self.display_str_base + ", Score: " + "{0:.2f}".format(
            self.score)

    def add_block(self, position=None):
        block = Figure(self.board)
        block.bindStrategy(FlagStrategy())
        if position != None:
            block.strategy.placeIt(y=position[0], x=position[1], soft=True)
        else:
            block.strategy.placeIt(soft=True)
        block.color = -1
        return block

    def add_wall(self, start=None, length=5, step=None):
        Wall = []
        if step == None:
            step = (0, 0)
            while step == (0, 0):
                step = (randint(-2, 2), randint(-2, 2))
        if start == None:
            xstart = randint(1, self.board.width)
            ystart = randint(1, self.board.height)
            start = (ystart, xstart)
        for i in range(length):
            pos = (start[0] + i * step[0], start[1] + i * step[1])
            if (pos[0] < self.board.height) and (pos[1] < self.board.width):
                # there is just one error here - board taken exception
                # we can skip that segment
                try:
                    Wall.append(self.add_block(pos))
                except:
                    pass
        return Wall
Ejemplo n.º 4
0
 def setup(self):
     for n in range(self.amount):
         figure = Figure(self.board)
         figure.bindStrategy(self.figureStrategyFactory())
         figure.strategy.placeIt()
     self.board.figures[0].color = 1