Ejemplo n.º 1
0
 def load_position(self, fn=None):
     if not fn:
         fd = QtGui.QFileDialog(self)
         fn = fd.getOpenFileName()
     if isfile(fn):
         self.record.clear()
         lines = open(fn).readlines()
         lines[0] = lines[0].replace('s', 'b').replace('g', 'w')
         _, self.pos = board.parse_long_pos(lines)
         self.board.update_pos(self.pos)
         self.board.mode = MODE_PLAY
Ejemplo n.º 2
0
 def load_position(self, fn=None):
     if not fn:
         fd = QtGui.QFileDialog(self)
         fn = fd.getOpenFileName()
     if isfile(fn):
         self.record.clear()
         lines = open(fn).readlines()
         lines[0] = lines[0].replace('s', 'b').replace('g', 'w')
         _, self.pos = board.parse_long_pos(lines)
         self.board.update_pos(self.pos)
         self.board.mode = MODE_PLAY
Ejemplo n.º 3
0
    def do_test(self, engine):
        lines = self.test.position.strip('\n').split('\n')
        movenum, pos = board.parse_long_pos(lines)

        engine.newgame()
        engine.setoption("tcmove", self.time_per_test)
        engine.setposition(pos)
        engine.go()

        log.debug('\n' + pos.to_long_str())
        log.debug("Doing the test %s." % self)
        while True:
            resp = engine.get_response()
            if resp.type == "bestmove":
                log.debug("bestmove: %s" % resp.move)
                bestmove = resp.move
                break
            else:
                log.debug(resp.message)

        if self.test.condition == "score goal":
            if pos.do_move(board.parse_move(bestmove)).is_goal():
                return 1
            return 0

        elif self.test.condition == "prevent goal":
            new_pos = pos.do_move(board.parse_move(bestmove))
            if pos.is_goal():
                return 1
            for p, move in new_pos.get_moves().items():
                if p.is_goal():
                    print "Opponent's goal by:", board.steps_to_str(move)
                    return 0
            return 1

        elif self.test.condition == "piece_position":
            new_pos = pos.do_move(board.parse_move(bestmove))
            after_pp = self.test.after_piece_position
            wpieces, bpieces = new_pos.to_placing_move()
            pieces_str = "%s %s" % (wpieces[2:], bpieces[2:])
            for item in after_pp.split('|'):
                case, sep, reward = item.partition(':')
                neg = False
                for e in case.split(' '):
                    if len(e) == 0:
                        continue

                    if e[0] == '!':
                        e = e[1:]
                        neg = True

                    #check kills
                    if is_kill(e) and check_piece_pos(e, bestmove):
                        continue
                    #check position
                    if check_piece_pos(e, pieces_str):
                        continue
                    break
                else:
                    try:
                        return float(reward)
                    except ValueError:
                        return 1
Ejemplo n.º 4
0
    def do_test(self, engine):
        lines = self.test.position.strip('\n').split('\n')
        movenum, pos = board.parse_long_pos(lines)

        engine.newgame()
        engine.setoption("tcmove", self.time_per_test)
        engine.setposition(pos)
        engine.go()

        log.debug('\n' + pos.to_long_str())
        log.debug("Doing the test %s." % self) 
        while True:
            resp = engine.get_response()
            if resp.type == "bestmove":
                log.debug("bestmove: %s" % resp.move)
                bestmove = resp.move
                break
            else:
                log.debug(resp.message)

        if self.test.condition == "score goal":
            if pos.do_move(board.parse_move(bestmove)).is_goal():
                return 1
            return 0

        elif self.test.condition == "prevent goal":
            new_pos = pos.do_move(board.parse_move(bestmove))
            if pos.is_goal():
                return 1
            for p, move in new_pos.get_moves().items():
                if p.is_goal():
                    print "Opponent's goal by:", board.steps_to_str(move)
                    return 0
            return 1 

        elif self.test.condition == "piece_position":
            new_pos = pos.do_move(board.parse_move(bestmove))
            after_pp = self.test.after_piece_position
            wpieces, bpieces = new_pos.to_placing_move()
            pieces_str = "%s %s" % ( wpieces[2:], bpieces[2:])
            for item in after_pp.split('|'):
                case, sep, reward = item.partition(':') 
                neg = False
                for e in case.split(' '):
                    if len(e) == 0:
                      continue

                    if e[0] == '!' : 
                      e = e[1:]
                      neg = True

                    #check kills
                    if is_kill(e) and check_piece_pos(e, bestmove):
                        continue 
                    #check position
                    if check_piece_pos(e, pieces_str):
                        continue 
                    break 
                else:
                    try: 
                        return float(reward)
                    except ValueError:
                        return 1

            return 0 

        raise Exception("Unknown condition.")