Example #1
0
    def performance(self):
        saving_thread = threading.Thread(target=save_map, args=(self.map, ))

        while True:
            log.debug('Thinking...')
            i, nc, na, stage, force = art_step(self.map)

            if i is not None:
                s = self.map.stones[i]

                if nc is None:
                    nc = s.center

                if na is None:
                    na = s.angle

                log.debug('Placing stone {} from {} to {}'.format(
                    i, s.center, nc))

                if self._move_stone(s.center, s.angle, nc,
                                    na):  # Pickup worked
                    if saving_thread.is_alive():
                        saving_thread.join(
                        )  # wait until save is completed if still being done
                    s.center = nc
                    s.angle = na
                    self.map.stage = stage  # Commit stage
                    log.info('Placement worked')
                else:  # Fail, flag
                    if saving_thread.is_alive():
                        saving_thread.join(
                        )  # wait until save is completed if still being done
                    s.flag = True
                    log.info('Placement failed')

                saving_thread = threading.Thread(target=save_map,
                                                 args=(self.map, ))
                saving_thread.start()  # async call of self.save_map

            elif force:  # Art wants us to advance anyhow
                if saving_thread.is_alive():
                    saving_thread.join(
                    )  # wait until save is completed if still being done
                self.map.stage = stage  # Commit stage

                saving_thread = threading.Thread(target=save_map,
                                                 args=(self.map, ))
                saving_thread.start()  # async call of self.save_map
            else:
                if saving_thread.is_alive():
                    saving_thread.join(
                    )  # wait until save is completed if still being done
                time.sleep(1)

        if saving_thread.is_alive():
            saving_thread.join(
            )  # wait until save is completed if still being done
Example #2
0
    def performance(self):
        saving_thread = threading.Thread(target=save_map, args=(self.map, ))

        while True:
            log.debug('Thinking...')
            i, nc, na, stage, force = art_step(self.map)

            if i is not None:
                s = self.map.stones[i]

                if nc is None:
                    nc = s.center

                if na is None:
                    na = s.angle

                log.debug('Placing stone {} from {} to {}'.format(i, s.center, nc))

                if self._move_stone(s.center, s.angle, nc, na):   # Pickup worked
                    if saving_thread.is_alive(): saving_thread.join() # wait until save is completed if still being done
                    s.center = nc
                    s.angle = na
                    self.map.stage = stage  # Commit stage
                    log.info('Placement worked')
                else:  # Fail, flag
                    if saving_thread.is_alive(): saving_thread.join() # wait until save is completed if still being done
                    s.flag = True
                    log.info('Placement failed')

                saving_thread = threading.Thread(target=save_map, args=(self.map, ))
                saving_thread.start() # async call of self.save_map

            elif force:  # Art wants us to advance anyhow
                if saving_thread.is_alive(): saving_thread.join() # wait until save is completed if still being done
                self.map.stage = stage  # Commit stage

                saving_thread = threading.Thread(target=save_map, args=(self.map, ))
                saving_thread.start() # async call of self.save_map
            else:
                if saving_thread.is_alive(): saving_thread.join() # wait until save is completed if still being done
                time.sleep(1)

        if saving_thread.is_alive(): saving_thread.join() # wait until save is completed if still being done
Example #3
0
if __name__ == '__main__':
    map = StoneMap('stonemap')
    map.stage = (0, 0, None, None)  # NOTE: use this to override the current state
    map.save(meta=True)

    if len(map.stones) == 0:
        # map.randomize()
        print "No STONES!"

    while True:
        img_map = np.zeros((map.size[1] / 2, map.size[0] / 2, 3), np.uint8)
        map.image(img_map, 2)
        cv2.imshow('map', img_map)
        if cv2.waitKey(1) == ord('q'):
            break
        i, nc, na, stage, force = art_step(map)

        do_fail = False               # Never fail
        # do_fail = random() < 0.05   # Simulates that 5% of stones cannot be picked up

        if i is not None and not do_fail:
            stone = map.stones[i]

            map.holes.append(StoneHole(map.stones[i]))

            if nc is not None and na is not None and not do_fail:
                log.debug('Placing stone {} from {} to {}'.format(i, stone.center, nc))
                stone.center = nc
                stone.angle = na

            map.stage = stage
Example #4
0
        cvkey = chr(cv2.waitKey(1) & 255)
        if cvkey == 'q':
            break

        if cvkey == 's':
            import svgwrite

            ts = int(time.time())
            svg_fn = 'map_{}.svg'.format(ts, )

            log.debug('Saving svg to {}'.format(svg_fn))
            dwg = svgwrite.Drawing(svg_fn)
            map.image_svg(dwg, scale=2, stonescale=0.89)
            dwg.save()

        i, nc, na, stage, force = art_step(map)

        do_fail = False  # Never fail
        # do_fail = random() < 0.05   # Simulates that 5% of stones cannot be picked up

        if i is not None and not do_fail:
            stone = map.stones[i]

            map.holes.append(StoneHole(map.stones[i]))

            if nc is not None and na is not None and not do_fail:
                log.debug('Placing stone {} from {} to {}'.format(
                    i, stone.center, nc))
                stone.center = nc
                stone.angle = na