Example #1
0
#!/usr/bin/env python

from zehint import hint

alphA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
alphz = 'zyxwvutsrqponmlkjihgfedcba'

def test(h):
    print "Hits : ", h.hits
    print "Moves: ", h.moves
    print ''.join([str(x) for x in h.dice])+''.join([ \
          (alphA if True else alphz)[x[0]] \
          for x in h.moves ])
    print ""


# GNU Backgammon  Position ID: tu0UAiAWAAAAAA
#                 Match ID   : cIkGAAAAAAAA
#test(hint( "3/off", [1,5], [1] ))

# GNU Backgammon  Position ID: zwcBAPjdIwAAAA
#                 Match ID   : cIkKAAAAAAAA
#test(hint( "6/off", [5,2], [] ))

#test(hint( "6/off", [2,2], [] ))

#test(hint( "5/off 4/2", [6,2], [] ))

test(hint( "1/off", [5,4], [] ))
Example #2
0
def play(s,gid):
    print 'gid = ', gid

    url = 'http://zooescape.com/backgammon.pl?v=200&gid=%s' % (gid)
    html = s.get(url).text

    # check if logged in
    if html.find('Log in to <A href="/">play Backgammon</A>')!=-1:
       return False

    g = read_board(html)
    print g.board+':'+g.match+'\n'

    if args.msgdb is not None:
        check_msgs(args.msgdb,html,gid,g.opp()[1])

    # Get hint from gnubg -------------------------------------------
    pipe = Popen([args.gnubg,'-t'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
    gnubg = pipe.communicate( input='''
set sound enable false
set threads %d
set evaluation chequerplay evaluation plies %d
set evaluation chequerplay evaluation cubeful off
set evaluation cubedecision evaluation cubeful off

set beavers 0
set jacoby off

set player 0 name %s
set player 1 name %s

new game
set matchid %s
set board %s
hint
''' % (args.threads,args.plies, \
       g.opp()[1],g.players[g.moving][1], \
       g.match,g.board) )[0]

    gnubg = gnubg[gnubg.rfind(' GNU Backgammon'):]
    if len(gnubg)==0:
        print 'GNU Backgammon failed unexpectedly'
        sys.exit(1)
    print gnubg
    l1 = gnubg.rfind('    1. ')
    moves = gnubg[l1:gnubg.find('Eq',l1)]
    moves = moves[moves.rfind('ply')+3:].strip()

    h = hint( moves, g.dice,
        [ 25-alphA.find(x[0]) for x in re.findall(r'[A-Z].', g.state[0]) ]
        # if int(x[1],16)>1 removed
        # count even 1 checker as a point here,
        # because gnubg explicitly marks hits
    )
    print "Hits : ", h.hits
    print "Moves: ", h.moves

    moving_dpip  = -sum([ x[0]-x[1] for x in h.moves ])
    oponent_dpip =  sum(h.hits)

    turn = len(re.findall(r'[1-6]{2}[a-zA-Z]*', g.state[1]))

    # TODO: only roll if needed
    # s.post('http://zooescape.com/backgammon-roll.pl', {
    #     'gid': gid, 'turn': turn-1
    # })

    # Send move request ---------------------------------------------
    s.post(url, {
        'bg_form_moves'   : ''.join([str(x) for x in h.dice])+''.join([ \
                            (alphA if g.moving_black else alphz)[x[0]] \
                            for x in h.moves ]),
        'bg_form_pips_b'  : str(get_pip(g.state[0],not g.moving_black) \
                            + (moving_dpip if g.moving_black else oponent_dpip)),
        'bg_form_pips_w'  : str(get_pip(g.state[0],g.moving_black) \
                            + (oponent_dpip if g.moving_black else moving_dpip)),
        'bg_submit'       : '1',
        'bg_button_submit': 'Submit',
        'bg_turn_num'     : turn
    } )

    if log_file is not None:
        l1 += 6
        l2 = gnubg.find('\n',l1)
        l3 = gnubg.find('\n',l2+1)
        log_file.write( '%7s %15s %4s %3s\n   %s\n   %s' % (
            gid, g.opp()[1], g.opp()[7], g.opp()[10],
            gnubg[l1:l2], gnubg[l2+7:l3-1]
        ) )

    return True