Example #1
0
 def start_move_animation(self, move_num):
     self.pos, _ = self.get_pos(move_num)
     pos, last_move = self.get_pos(move_num, separate_last_move=True)
     self.board.update_pos(pos)
     if last_move:
         steps = board.parse_move(last_move)
         self.board.start_move_animation(steps)
Example #2
0
 def start_move_animation(self, move_num):
     self.pos, _ = self.get_pos(move_num)
     pos, last_move = self.get_pos(move_num, separate_last_move=True)
     self.board.update_pos(pos)
     if last_move: 
         steps = board.parse_move(last_move)
         self.board.start_move_animation(steps)
Example #3
0
    def xmousePressEvent(self, e):
        index = self.point2index(e.pos())
        if not self.square_empty(index):
            return
        steps = filter(lambda x: stepstr2to(x) == index, self.marked)
        if steps == []:
            return 
        #there might be more steps with same prefix e.g. Eg5w, Eg5w hg4n, etc.
        step = steps[0][0:STEPSTR_LEN]
        steps = map(lambda x: x.partition(step)[2].strip(), steps)

        new_pos = self.pos.do_step(board.parse_move(step))
        print new_pos.to_short_str()
        new_steps = [ s for s in filter(lambda x: x != '', steps)]
        if '' in steps:
                new_steps += [ board.steps_to_str(step) for step, _ in new_pos.get_steps()]
        self.update_pos_inner(new_pos, new_steps)
Example #4
0
    def get_pos(self, row, separate_last_move=False):
        moves = map(lambda x: str(x.text()).strip(), [self.record.item(index) for index in xrange(1, row + 1)])
        pos = self.make_empty_pos()
        if row == 0: #moves == ['record'] or moves == []:
            return pos, []

        if not separate_last_move:
            moves.append(' ')

        moves = map(lambda x: x[x.find(' ') + 1:], moves)
        for m in moves[:-1]: 
            try: 
                steps = board.parse_move(m)
                pos = pos.do_move(steps)
            except ValueError:
                break

        return pos, moves[-1]
Example #5
0
    def get_pos(self, row, separate_last_move=False):
        moves = map(lambda x: str(x.text()).strip(),
                    [self.record.item(index) for index in xrange(1, row + 1)])
        pos = self.make_empty_pos()
        if row == 0:  #moves == ['record'] or moves == []:
            return pos, []

        if not separate_last_move:
            moves.append(' ')

        moves = map(lambda x: x[x.find(' ') + 1:], moves)
        for m in moves[:-1]:
            try:
                steps = board.parse_move(m)
                pos = pos.do_move(steps)
            except ValueError:
                break

        return pos, moves[-1]
Example #6
0
    def xmousePressEvent(self, e):
        index = self.point2index(e.pos())
        if not self.square_empty(index):
            return
        steps = filter(lambda x: stepstr2to(x) == index, self.marked)
        if steps == []:
            return
        #there might be more steps with same prefix e.g. Eg5w, Eg5w hg4n, etc.
        step = steps[0][0:STEPSTR_LEN]
        steps = map(lambda x: x.partition(step)[2].strip(), steps)

        new_pos = self.pos.do_step(board.parse_move(step))
        print new_pos.to_short_str()
        new_steps = [s for s in filter(lambda x: x != '', steps)]
        if '' in steps:
            new_steps += [
                board.steps_to_str(step) for step, _ in new_pos.get_steps()
            ]
        self.update_pos_inner(new_pos, new_steps)
Example #7
0
def process_record(line):
    moves = line.split('\\n')
    pos = make_empty_pos()
    for m in moves: 
        #print m
        #strip off move number
        m = m[m.find(' ') + 1:]
        try: 
            steps = board.parse_move(m)
            lastpos = pos
            pos = pos.do_move(steps)
        except ValueError:
            break

    goal = pos.is_goal()
    if goal:
        #check not "give opponent" goal
        if ((lastpos.color, goal) == (board.COL_GOLD, -1)  or (lastpos.color, goal) == (board.COL_SILVER, 1)):
            #print lastpos.to_long_str()  
            #print board.steps_to_str(steps)
            return
            
        print lastpos.to_short_str(), "#", board.steps_to_str(steps)
Example #8
0
def do_test(engine, pos_str, move):
    pos = board.parse_short_pos(board.COL_SILVER, 4, pos_str)

    engine.newgame()
    engine.setposition(pos)
    engine.engine.send("goalcheck\n")

    result = ""
    while True:
        resp = engine.get_response()
        if resp.type == "info":
            lmsg = resp.message.split(" ")
            if lmsg[0] == "goalcheck":
                result = lmsg[1]
                extra = " ".join(lmsg[2:])
                break

    wrong = {}
    if result not in ["gold", "silver"]:
        wrong["result"] = True
    else:
        goal_pos = pos.do_move(board.parse_move(extra))

        if not goal_pos.is_goal():
            wrong["goal"] = True

    if len(wrong) > 0:
        print "========================="
        print pos.to_long_str()
        print result

        if wrong.has_key("goal"):
            print "Wrong goal move generated"
            print extra
        if wrong.has_key("result"):
            print "should be", move
Example #9
0
def process_record(line):
    moves = line.split('\\n')
    pos = make_empty_pos()
    for m in moves:
        #print m
        #strip off move number
        m = m[m.find(' ') + 1:]
        try:
            steps = board.parse_move(m)
            lastpos = pos
            pos = pos.do_move(steps)
        except ValueError:
            break

    goal = pos.is_goal()
    if goal:
        #check not "give opponent" goal
        if ((lastpos.color, goal) == (board.COL_GOLD, -1)
                or (lastpos.color, goal) == (board.COL_SILVER, 1)):
            #print lastpos.to_long_str()
            #print board.steps_to_str(steps)
            return

        print lastpos.to_short_str(), "#", board.steps_to_str(steps)
Example #10
0
def do_test(engine, pos_str, move):
    pos = board.parse_short_pos(board.COL_SILVER, 4, pos_str)

    engine.newgame()
    engine.setposition(pos)
    engine.engine.send('goalcheck\n')

    result = ''
    while True:
        resp = engine.get_response()
        if resp.type == "info":
            lmsg = resp.message.split(' ')
            if lmsg[0] == 'goalcheck':
                result = lmsg[1]
                extra = ' '.join(lmsg[2:])
                break

    wrong = {}
    if result not in ['gold', 'silver']:
        wrong['result'] = True
    else:
        goal_pos = pos.do_move(board.parse_move(extra))

        if not goal_pos.is_goal():
            wrong['goal'] = True

    if (len(wrong) > 0):
        print "========================="
        print pos.to_long_str()
        print result

        if wrong.has_key('goal'):
            print "Wrong goal move generated"
            print extra
        if wrong.has_key('result'):
            print "should be", move
Example #11
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
Example #12
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.")