예제 #1
0
def game(players, state=None):
    def getp(sgn):
        for p in players:
            if p.sgn == sgn:
                return p

    if state is None:
        state = rndstate(-1)

    sgn = chkwho(state)
    s = getState(state)
    player = getp(sgn)
    score = player.evalR(s.win)
    i = players.index(player)

    for j in xrange(1000):
        if j == 999:
            raise Exception('infinite loop')

        try:
            snew = encState(s.state)
            pos = player.update(snew, score).predict(snew)
            s1 = takeAction(s, pos, sgn)

            if (s1.win != 0):
                break

            sgn = 1 if sgn == 2 else 2
            i = (i + 1) % 2
            player = players[i]
            score = player.evalR(s.win)
            s = s1
        except ColException:
            score = -100.
        except Exception as e:
            print_exc()
            set_trace()

    for p in players:
        score = p.evalR(s1.win)
        # p.setScore(score)
        p.update(encState(s1.state), score)
        p.replay()
        p.reset()

    return s1.win, score
예제 #2
0
    def testtransform(self):
        state = rndstate(10)
        # show(state)
        s1 = encState(state)
        assert s1.shape == (1, 84), s1.shape

        def f(x, sgn):
            if x == 1:
                return sgn
            elif x == 0:
                return ' '
            else:
                raise Exception('Unknown state')

        s2 = u''.join([f(x, 'O') for x in s1[0, :42]])
        for x, y in zip(s2, state):
            if x == 'O':
                assert x == y, (x, y)
        s2 = u''.join([f(x, 'X') for x in s1[0, 42:]])
        for x, y in zip(s2, state):
            if x == 'X':
                assert x == y, (x, y)