本文整理汇总了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
#.........这里部分代码省略.........