Example #1
0
File: move.py Project: bgnori/bglib
  def is_leagal_to_pickup_dice(self):
    assert self.board.rolled != (0, 0)
    if not self.available:
      return True
    bar = util.move_pton('bar')
    if self.board.has_chequer_to_move(bar):
      if not self.guess_your_single_pm_from_source(bar):
        return True #dance

    for pt in POINTS:
      if self.board.has_chequer_to_move(pt):
        if self.guess_your_single_pm_from_source(pt):
          return False
          #if self.board.rolled[0] == self.board.rolled[1]:
          #  print 'can move checker from', util.move_ntop(pt);

    if self.board.is_doubles():
      return True
            
    #rewind all moves and check for blocked moves.
    mf = MoveFactory(self.board, self.move, self.available)
    inverses  = []
    for pm in mf.move:
      inverses.append(PartialMove(die=pm.die, src=pm.dest, dest=pm.src, is_hitting=pm.is_hitting))
    for i in inverses:
      mf.append(i)

    assert len(mf.move) == 0

    # No need to check doubles.
    # care only about small-big use.
    # thus ...
    for src in POINTS:
      if mf.board.has_chequer_to_move(src):
        for dst in range(0, src):
          if mf.board.is_open_to_land(dst) or mf.board.is_hitting_to_land(dst):
            pms = mf.guess_your_multiple_pms(src, dst)
            if pms and len(pms) == 2:
              # all dice are used.
              # that means there is way to use all dice.
              return False
    # There is no way to use both dice
    # Make sure use big one
    # thus ... 
    if self.available.get_max() != mf.available.get_max():
      return True #max is used.

    die = mf.available.get_max()
    for src in POINTS:
      if mf.board.has_chequer_to_move(src):
        pm = mf.guess_your_single_pm_from_source(src, available=AvailableToPlay(rolled=(die, 0)))
        if pm:
          return False # There is way to use big one.
    return True
Example #2
0
 def t023_test(self):
   self.assertEqual(util.move_pton('off'), -1)
Example #3
0
 def t021_test(self):
   self.assertEqual(util.move_pton('1'), 0)
Example #4
0
 def t019_test(self):
   self.assertEqual(util.move_pton('bar'), 24)
Example #5
0
 def t030_test(self):
   try: 
     util.move_pton('100')
     self.assert_(False)
   except ValueError:
     pass
Example #6
0
 def t029_test(self):
   try: 
     util.move_pton('hoge')
     self.assert_(False)
   except ValueError:
     pass
Example #7
0
def app(environ, start_response):
    from StringIO import StringIO
    stdout = StringIO()
    q = cgi.parse_qs(environ['QUERY_STRING'])

    EMPTY = ('',)
    new = q.get('new', EMPTY)[0]
    gnubgid = q.get('gnubgid', EMPTY)[0]
    move = q.get('move', EMPTY)[0]
    cube = q.get('cube', EMPTY)[0]
    pickupdice = q.get('pickupdice', EMPTY)[0]
    dice= q.get('dice', EMPTY)[0]

    print >>sys.stderr, 'got: new=%s, gnubgid=%s, move=%s, cube=%s pickupdice=%s dice=%s'%(new, gnubgid, move, cube, pickupdice,  dice)
    if new:
      print 'setting initial position for game with', new
      to_action, score = new.split(':')
      score, length = score.split('/')
      print score, length
      X_score, O_score = score.split('-')
      print X_score, O_score
      a = int(to_action)
      b = BoardEditor(
        on_action=a,
        on_inner_action=a,
        game_state = ON_GOING,
        match_length=int(length),
        score=(int(X_score), int(O_score)),
        crawford=('*' in score),
      )
      p, m = encode(b)

    else :
      pid, mid = gnubgid.split(':')
      print >>sys.stderr, pid, mid
      assert len(pid) == 14
      #01234567890123
      #sGfwgAPbuIEDIA
      assert len(mid) == 12
      #01234567890123
      #cIkqAAAAAAAA

      b = BoardEditor()
      decode(b, pid, mid)
      print b
      if b.is_leagal_to_move(b.on_action):
        #ugh! can be empty, DANCE ! assert move 
        moves = move.split(' ')
        mf = MoveFactory(b)
      
        for mv in moves:
          print >>sys.stderr, mv
          if mv:
            m = moveRegExp.search(mv)
            if not m:
              break
            d = m.groupdict()
            print d
            n = int(d['multi'] or '1')
            while n > 0:
              print d['src'], d['dest']
              found = mf.guess_your_multiple_pms(move_pton(d['src']),
                                                 move_pton(d['dest']))
              assert found
              #assert not d['hitting'] or n!=1 or found.is_hitting 
              mf.add(found)
              #mf.append(found)
              n = n -1;
      
        assert mf.is_leagal_to_pickup_dice()
      
        if pickupdice:
          print 'pickupdice', pickupdice
          mf.pickupdice()
        print mf.board
        p, m = encode(mf.board)
      elif b.is_leagal_to_roll(b.on_action):
        if cube == 'no double' and dice:
          b.rolled = (int(dice[0]), int(dice[1]))
        elif 'Double' in cube:
          pass
        print b
        p, m = encode(b)
      else:
        assert False
      pass #else case of if new:

    assert len(p) == 14
    assert len(m) == 12

  
    value = {"status": True, "gnubgid": "%s:%s"%(p, m)}
    j = '%s(%s);'%(q['callback'][0], simplejson.dumps(value))
    print >>stdout, j
    print >>sys.stderr, 'sending:' ,j
    start_response("200 OK", [('Content-Type','text/javascript')])
    return [stdout.getvalue()]