當前位置: 首頁>>代碼示例>>Python>>正文


Python Prolog.asserta方法代碼示例

本文整理匯總了Python中pyswip.Prolog.asserta方法的典型用法代碼示例。如果您正苦於以下問題:Python Prolog.asserta方法的具體用法?Python Prolog.asserta怎麽用?Python Prolog.asserta使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pyswip.Prolog的用法示例。


在下文中一共展示了Prolog.asserta方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: Game

# 需要導入模塊: from pyswip import Prolog [as 別名]
# 或者: from pyswip.Prolog import asserta [as 別名]
class Game():
    def __init__(self,radius=80):
        self.win = Tk()
        self.radius = radius
        self.pawnList1 = []
        self.pawnList2 = []
        self.generatePawns()
        self.playerOne = True
        self.pawnSelected = False
        self.chaining = False
        self.capturing = False
        self.selectedPawn = None
        self.captured = None
        self.prolog = Prolog()
        self.prolog.consult("clauses.pl")
        self.prolog.asserta("hos(dummy)")
        self.defaultPosition()
        self.board = Board(self.pawnList1,self.pawnList2,self.radius,self.win,self)
        self.board.mainloop()

    def getPawnByName(self,name):
        for p in self.pawnList1:
            if p.getName() == name:
                return p
        for p in self.pawnList2:
            if p.getName() == name:
                return p

    def checkMove(self,pos):
        validMoveQuery = "canMove("+self.selectedPawn.getName()+",NewPos)"
        l = list(self.prolog.query(validMoveQuery))
        validMoves = []
        for i in l:
            validMoves.extend(i.values())
        if pos in validMoves:
            return True
        else:
            return False

    def queryCapturing(self,p):
        capturingQuery = "canEat("+p.getName()+",Target,NewPos)"
        l = list(self.prolog.query(capturingQuery))
        posAfterCapturing = []
        for i in l:
            posAfterCapturing.extend(i.values())
        return posAfterCapturing

    def checkCapturing(self,pos):
        posAfterCapturing = self.queryCapturing(self.selectedPawn)
        if pos in posAfterCapturing:
            self.captured = self.getPawnByName(posAfterCapturing[posAfterCapturing.index(pos)+1])
            self.capturing = True
            return True
        else:
            return False

    def defaultPosition(self):
        positions = ["position(a1,60)",
                     "position(b1,71)",
                     "position(c1,62)",
                     "position(d1,73)",
                     "position(e1,64)",
                     "position(f1,75)",
                     "position(g1,66)",
                     "position(h1,77)",
                     "position(a2,00)",
                     "position(b2,11)",
                     "position(c2,02)",
                     "position(d2,13)",
                     "position(e2,04)",
                     "position(f2,15)",
                     "position(g2,06)",
                     "position(h2,17)"]
        for position in positions:
            self.prolog.asserta(position)
        #for pos in self.prolog.query("position(X,Y)"):
            #print pos["X"], pos["Y"]

    def modifyPosition(self):
        for position in self.prolog.query("retractall(position(_,_))"):
            pass

        for pawn in self.pawnList1:
            self.prolog.asserta(
                "position(" + pawn.getName() + "," +
                str(pawn.getRow()) + str(pawn.getColumn()) + ")")
        
        for pawn in self.pawnList2:
            self.prolog.asserta(
                "position(" + pawn.getName() + "," +
                str(pawn.getRow()) + str(pawn.getColumn()) + ")")
            
    def generatePawns(self):
        for i in range(8):
            name = chr(97+i)
            self.pawnList1.append(Pawn(name+"1", 6 + (i % 2), i, 'blue', self.radius))
            self.pawnList2.append(Pawn(name+"2", 0 + (i % 2), i, 'red', self.radius))

    def selectPawn(self,p):
        self.pawnSelected = True
#.........這裏部分代碼省略.........
開發者ID:sagitaress,項目名稱:PrologChecker,代碼行數:103,代碼來源:Checker.py


注:本文中的pyswip.Prolog.asserta方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。