+ def scores(self):
+ """The hand is any valid scoring combination.
+ """
+ return self.set() or self.run()
+ def subhands(self, lengths=None):
+ """Return all possible subhands.
+
+ Lengths can either be a list of allowed subhand lengths or
+ None. If None, all possible subhand lengths are allowed.
+
+ >>> d = Deck()
+ >>> h = Hand([Card(d, 1, Territory('a')),
+ ... Card(d, 1, Territory('b')),
+ ... Card(d, 1, Territory('c')),
+ ... Card(d, 1, Territory('d'))])
+ >>> for hand in h.subhands():
+ ... print hand
+ [<Card a Infantry>]
+ [<Card b Infantry>]
+ [<Card c Infantry>]
+ [<Card d Infantry>]
+ [<Card a Infantry>, <Card b Infantry>]
+ [<Card a Infantry>, <Card c Infantry>]
+ [<Card a Infantry>, <Card d Infantry>]
+ [<Card b Infantry>, <Card c Infantry>]
+ [<Card b Infantry>, <Card d Infantry>]
+ [<Card c Infantry>, <Card d Infantry>]
+ [<Card a Infantry>, <Card b Infantry>, <Card c Infantry>]
+ [<Card a Infantry>, <Card b Infantry>, <Card d Infantry>]
+ [<Card a Infantry>, <Card c Infantry>, <Card d Infantry>]
+ [<Card b Infantry>, <Card c Infantry>, <Card d Infantry>]
+ [<Card a Infantry>, <Card b Infantry>, <Card c Infantry>, <Card d Infantry>]
+ """
+ for i in range(len(self)):
+ i += 1 # check all sub-hands of length i
+ if lengths != None and i not in lengths:
+ continue # don't check this length
+ indices = range(i)
+ stop = range(len(self)-i, len(self))
+ while indices != stop:
+ yield Hand([self[i] for i in indices])
+ indices = self._increment(indices, stop)
+ yield Hand([self[i] for i in indices])
+ def _increment(self, indices, stop):
+ """
+ >>> d = Deck()
+ >>> h = Hand([Card(d, 1, Territory('a'))])
+ >>> h._increment([0, 1, 2], [2, 3, 4])
+ [0, 1, 3]
+ >>> h._increment([0, 1, 3], [2, 3, 4])
+ [0, 1, 4]
+ >>> h._increment([0, 1, 4], [2, 3, 4])
+ [0, 2, 3]
+ """
+ moveable = [i for i,m in zip(indices, stop) if i < m]
+ assert len(moveable) > 0, 'At stop? indices: %s, stop: %s' % (indices, stop)
+ key = indices.index(moveable[-1])
+ new = indices[key] + 1
+ for i in range(key, len(indices)):
+ indices[i] = new + i-key
+ return indices