Exemplo n.º 1
0
    def play(self, screen):
        """the AI plays here"""
        must_choose = 0
        (ai_x, ai_y) = self.getSelectedStone()
        #        print "AI x = %d y = %d " % (ai_x,ai_y)

        pStone = Stone(self.board, ai_x, ai_y)
        pStone.selected()

        (new_x, new_y, nb, action) = pStone.canCaptureStones()
        if new_x >= 0 and new_y >= 0:
            self.update(screen)

        timeout = 0
        while 1:

            if timeout < 10000:
                self.update(screen)
                timeout += 1

            (new_x, new_y, nb, action) = pStone.canCaptureStones()
            if new_x >= 0 and new_y >= 0:

                timeout = 0

                #                print "moves to (%d , %d)" % (new_x,new_y)
                if pStone.move(new_x, new_y):
                    if pStone.mustChoose():
                        pStone.chooseAction(action)
                        pStone.selected()

                    if pStone.canMove():
                        pStone.selected()

                    else:
                        #                         print "can't move and capture "
                        pStone.unselected()
                        break

            else:
                stones = self.getFreeStones()

                found = False
                for (sx, sy) in stones:
                    neib = utils.get_positions((sx, sy))

                    for (xx, yy) in neib:
                        oStone = Stone(self.board, sx, sy)

                        if xx >= 0 and xx < self.board.cols and yy >= 0 and yy < self.board.rows and oStone.legalMove(
                                xx, yy):
                            new_board = self.board.copy()
                            new_board.unselectedAll()

                            oStone = Stone(new_board, sx, sy)
                            if oStone.move(xx, yy):
                                if not oStone.canBeCaptured():
                                    found = True
                                    pStone = Stone(self.board, sx, sy)
                                    pStone.selected()
                                    pStone.move(xx, yy)
                                    break

                    if found:
                        break

                if found:
                    break

                else:
                    print "must choose one"
                    neib = utils.get_positions((ai_x, ai_y))
                    oStone = Stone(self.board, ai_x, ai_y)
                    for (xx, yy) in neib:
                        if xx >= 0 and xx < self.board.cols and yy >= 0 and yy < self.board.rows and oStone.legalMove(
                                xx, yy):
                            found = True
                            pStone = Stone(self.board, sx, sy)
                            pStone.selected()
                            pStone.move(xx, yy)
                            break
                    break

            self.board.unselectedAll()
            self.update(screen)
Exemplo n.º 2
0
    def play(self,screen):
        """the AI plays here"""
        must_choose = 0
        (ai_x, ai_y) = self.getSelectedStone()
#        print "AI x = %d y = %d " % (ai_x,ai_y)

        pStone = Stone(self.board , ai_x ,ai_y)
        pStone.selected()

        (new_x, new_y , nb , action) = pStone.canCaptureStones()
        if new_x >= 0 and new_y >= 0 :
            self.update(screen)

        timeout = 0
        while 1 :

            if timeout<10000:
                self.update(screen)
                timeout+=1

            (new_x, new_y , nb , action) = pStone.canCaptureStones()
            if new_x >= 0 and new_y >= 0 :

                timeout = 0

#                print "moves to (%d , %d)" % (new_x,new_y)
                if pStone.move(new_x,new_y) :
                    if pStone.mustChoose():
                        pStone.chooseAction(action)
                        pStone.selected()

                    if pStone.canMove() :
                        pStone.selected()

                    else :
#                         print "can't move and capture "
                        pStone.unselected()
                        break

            else :
                stones = self.getFreeStones()

                found = False
                for (sx,sy) in stones :
                    neib = utils.get_positions((sx,sy))


                    for (xx ,yy) in  neib:
                        oStone = Stone(self.board,sx,sy)

                        if xx>=0 and xx < self.board.cols and yy >=0 and yy < self.board.rows and oStone.legalMove(xx,yy) :
                            new_board = self.board.copy()
                            new_board.unselectedAll()

                            oStone = Stone(new_board,sx,sy)
                            if oStone.move(xx,yy):
                                if not oStone.canBeCaptured():
                                    found = True
                                    pStone = Stone(self.board , sx ,sy)
                                    pStone.selected()
                                    pStone.move(xx,yy)
                                    break

                    if found:
                        break

                if found:
                    break

                else:
                    print "must choose one"
                    neib = utils.get_positions((ai_x,ai_y))
                    oStone = Stone(self.board,ai_x,ai_y)
                    for (xx ,yy) in  neib:
                        if xx>=0 and xx < self.board.cols and yy >=0 and yy < self.board.rows and oStone.legalMove(xx,yy) :
                            found = True
                            pStone = Stone(self.board , sx ,sy)
                            pStone.selected()
                            pStone.move(xx,yy)
                            break
                    break


            self.board.unselectedAll()
            self.update(screen)