Esempio n. 1
0
def dispose_msg(msg, msg_queue):
    # print('recv:', msg)

    global board
    global s1
    global first_query
    global who_first

    ans = None
    seq = msg.split(' ')
    if seq[0] == 'START:':
        board_size = int(seq[1])
        Board.set_board_size(board_size)
        board = Board()
        if s1 is None:
            s1 = StrategyDNN()
        first_query = True
        who_first = None
        ans = 'START: OK'
        if msg_queue is not None:
            msg_queue.put(('start', ))
        # s1.absorb('?')
        s1.on_episode_start()
    elif seq[0] == 'MOVE:':
        assert len(seq) >= 4, 'protocol inconsistent'
        old_board = copy.deepcopy(board)
        x, y = int(seq[1]), int(seq[2])
        who = Board.STONE_BLACK if int(seq[3]) == 1 else Board.STONE_WHITE
        if who_first is None:
            who_first = who
            print('who first?', who_first)
        if board.is_legal(x, y):
            board.move(x, y, who)

        s1.swallow(who, old_board, board)
        if msg_queue is not None:
            msg_queue.put(('move', who, x * Board.BOARD_SIZE + y))
    elif seq[0] == 'WIN:':
        assert len(seq) == 3, 'protocol inconsistent'
        x, y = int(seq[1]), int(seq[2])
        who = board.get(x, y)
        print('player %d win the game' % (who, ))
    elif seq[0] == 'UNDO:':
        ans = 'UNDO: unsupported yet'
    elif seq[0] == 'WHERE:':
        if who_first is None:
            who_first = Board.STONE_BLACK
            print('who first?', who_first)
        if first_query:
            s1.stand_for = board.query_stand_for(who_first)
            print('i stand for:', s1.stand_for)
            first_query = False
        assert s1.stand_for is not None
        x, y = s1.preferred_move(board)
        ans = 'HERE: %d %d' % (x, y)
    elif seq[0] == 'END:':
        # s1.close()
        ans = 'END: OK'

    return ans
Esempio n. 2
0
class FiveGame(TwoPlayerGame):
    def __init__(self):
        self.reset()
    
    
    def reset(self):
        TwoPlayerGame.reset(self)
        self.movesDone = 0
        self.b = Board()
    
    def isLegal(self, c, pos):
        return self.b.is_legal(pos[0], pos[1])
    
    def _fiveRow(self, c, pos):
        b = self.b.stones.reshape(-1, Board.BOARD_SIZE)
        self.b.find_conn_5(b, pos[0], pos[1], c)
        
    def getLegals(self, c):
        loc = np.where(self.b.stones == 0)
        moves = [i for i in map(lambda i: divmod(i, Board.BOARD_SIZE), loc[0])]
        return moves
    
    def doMove(self, c, pos):
        """ the action is a (color, position) tuple, for the next stone to move.
        returns True if the move was legal. """
        self.movesDone += 1
        if not self.isLegal(c, pos):
            return False
        elif self._fiveRow(c, pos):
            self.winner = c
            self.b.move(pos[0], pos[1], c)
            return True
        else:
            self.b.move(pos[0], pos[1], c)
            if self.movesDone == Board.BOARD_SIZE_SQ:
                self.winner = Board.STONE_EMPTY
            return True
        
    def playToTheEnd(self, p1, p2):
        """ alternate playing moves between players until the game is over. """
        assert p1.color == -p2.color
        i = 0
        p1.game = self
        p2.game = self
        players = [p1, p2]
        while not self.gameOver():
            p = players[i]
            self.performAction(p.getAction())
            i = (i + 1) % 2
Esempio n. 3
0
class FiveGame(TwoPlayerGame):
    def __init__(self):
        self.reset()

    def reset(self):
        TwoPlayerGame.reset(self)
        self.movesDone = 0
        self.b = Board()

    def isLegal(self, c, pos):
        return self.b.is_legal(pos[0], pos[1])

    def _fiveRow(self, c, pos):
        b = self.b.stones.reshape(-1, Board.BOARD_SIZE)
        self.b.find_conn_5(b, pos[0], pos[1], c)

    def getLegals(self, c):
        loc = np.where(self.b.stones == 0)
        moves = [i for i in map(lambda i: divmod(i, Board.BOARD_SIZE), loc[0])]
        return moves

    def doMove(self, c, pos):
        """ the action is a (color, position) tuple, for the next stone to move.
        returns True if the move was legal. """
        self.movesDone += 1
        if not self.isLegal(c, pos):
            return False
        elif self._fiveRow(c, pos):
            self.winner = c
            self.b.move(pos[0], pos[1], c)
            return True
        else:
            self.b.move(pos[0], pos[1], c)
            if self.movesDone == Board.BOARD_SIZE_SQ:
                self.winner = Board.STONE_EMPTY
            return True

    def playToTheEnd(self, p1, p2):
        """ alternate playing moves between players until the game is over. """
        assert p1.color == -p2.color
        i = 0
        p1.game = self
        p2.game = self
        players = [p1, p2]
        while not self.gameOver():
            p = players[i]
            self.performAction(p.getAction())
            i = (i + 1) % 2