<s> __author__ = '<STR_LIT>' <EOL> from pybrain . rl . environments . twoplayergames . gomoku import GomokuGame <EOL> class PenteGame ( GomokuGame ) : <EOL> """<STR_LIT>""" <EOL> def reset ( self ) : <EOL> GomokuGame . reset ( self ) <EOL> self . pairsTaken = { self . BLACK : <NUM_LIT:0> , self . WHITE : <NUM_LIT:0> } <EOL> center = ( self . size [ <NUM_LIT:0> ] // <NUM_LIT:2> , self . size [ <NUM_LIT:1> ] // <NUM_LIT:2> ) <EOL> self . _setStone ( - self . startcolor , center ) <EOL> self . movesDone += <NUM_LIT:1> <EOL> def getKilling ( self , c ) : <EOL> """<STR_LIT>""" <EOL> res = GomokuGame . getKilling ( self , c ) <EOL> for p in self . getLegals ( c ) : <EOL> k = self . _killsWhich ( c , p ) <EOL> if self . pairsTaken [ c ] + len ( k ) / <NUM_LIT:2> >= <NUM_LIT:5> : <EOL> res . append ( p ) <EOL> return res <EOL> def _killsWhich ( self , c , pos ) : <EOL> """<STR_LIT>""" <EOL> res = [ ] <EOL> for dir in [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:0> ) , ( <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , - <NUM_LIT:1> ) ] : <EOL> for d in [ - <NUM_LIT:1> , <NUM_LIT:1> ] : <EOL> killcands = [ ] <EOL> for i in [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] : <EOL> next = ( pos [ <NUM_LIT:0> ] + dir [ <NUM_LIT:0> ] * i * d , pos [ <NUM_LIT:1> ] + dir [ <NUM_LIT:1> ] * i * d ) <EOL> if ( next [ <NUM_LIT:0> ] < <NUM_LIT:0> or next [ <NUM_LIT:0> ] >= self . size [ <NUM_LIT:0> ] <EOL> or next [ <NUM_LIT:1> ] < <NUM_LIT:0> or next [ <NUM_LIT:1> ] >= self . size [ <NUM_LIT:1> ] ) : <EOL> break <EOL> if i == <NUM_LIT:3> and self . b [ next ] == c : <EOL> res += killcands <EOL> break <EOL> if i != <NUM_LIT:3> and self . b [ next ] != - c : <EOL> break <EOL> killcands . append ( next ) <EOL> return res <EOL> def doMove ( self , c , pos ) : <EOL> """<STR_LIT>""" <EOL> self . movesDone += <NUM_LIT:1> <EOL> if not self . isLegal ( c , pos ) : <EOL> return False <EOL> elif self . _fiveRow ( c , pos ) : <EOL> self . winner = c <EOL> self . b [ pos ] = '<STR_LIT:x>' <EOL> return True <EOL> else : <EOL> tokill = self . _killsWhich ( c , pos ) <EOL> if self . pairsTaken [ c ] + len ( tokill ) / <NUM_LIT:2> >= <NUM_LIT:5> : <EOL> self . winner = c <EOL> self . b [ pos ] = '<STR_LIT:x>' <EOL> return True <EOL> self . _setStone ( c , pos , tokill ) <EOL> if self . movesDone == ( self . size [ <NUM_LIT:0> ] * self . size [ <NUM_LIT:1> ] <EOL> + <NUM_LIT:2> * ( self . pairsTaken [ self . BLACK ] + self . pairsTaken [ self . WHITE ] ) ) : <EOL> self . winner = self . DRAW <EOL> return True <EOL> def _setStone ( self , c , pos , tokill = None ) : <EOL> """<STR_LIT>""" <EOL> if tokill == None : <EOL> tokill = self . _killsWhich ( c , pos ) <EOL> GomokuGame . _setStone ( self , c , pos ) <EOL> for p in tokill : <EOL> self . b [ p ] = self . EMPTY <EOL> self . pairsTaken [ c ] += len ( tokill ) // <NUM_LIT:2> <EOL> def __str__ ( self ) : <EOL> s = GomokuGame . __str__ ( self ) <EOL> s += '<STR_LIT>' + str ( self . pairsTaken [ self . BLACK ] ) + '<STR_LIT>' + str ( self . pairsTaken [ self . WHITE ] ) + '<STR_LIT:.>' <EOL> return s </s>
