Commit 0cb7567f authored by Constantin Berhard's avatar Constantin Berhard

Euclid works

parent 86e8da4b
from npc import Npc
import random
class Euclid (Npc):
questions = [
("Imagine there is a circle. The origin and the point (4,0) are on the circle. The circle is symmetric with respect to the line x=y. Where is its center? ", lambda x: "2,2" in ("".join(x.split())).strip("()[]{}")),
("In a rectangular triangle, if the two shortest sides have lengths 3 meters and 4 meters, how long is the longest side?", lambda x: "5m" in ("".join(x.split())).replace("meters","m").replace("meter","m"))
]
class DLevel(Npc.DialogLevel):
def __init__ (self,euclid):
super().__init__(euclid)
self.question, self.answerChecker = random.choice(Euclid.questions)
self.questionAsked = False
def checkStandardRequests(self, teller, verb, message):
resp = super().checkStandardRequests(teller, verb, message)
if resp is not None:
assert len(resp) == 3
return resp
if not self.questionAsked:
self.questionAsked = True
return ("asks", self.question, None)
else:
if self.answerChecker(message):
# correct answer
self.npc.correctAnswerGiven = True
newq, newac = random.choice(Euclid.questions)
return ("says", "That answer is correct. I will go annoy people somewhere else. Good Bye.", Euclid.DLevel(self.npc))
else: # wrong answer
return ("shouts", "WRONG!", None)
def __init__(self,game):
super().__init__(game, Euclid.DLevel(self))
self.correctAnswerGiven = False
def __str__ (self):
return "Euclid"
def __repr__ (self):
return "E"
def afterAnswering(self):
if self.correctAnswerGiven:
self.correctAnswerGiven = False
self.relocate()
\ No newline at end of file
......@@ -4,6 +4,7 @@ import socket
import labyrinth
import random
from treasure import Treasure
from euclid import Euclid
class Game:
def __init__(self, labyrinthFileName, adminPW, logFileName=None, port=1234):
......@@ -11,9 +12,12 @@ class Game:
self.admins = []
with open(labyrinthFileName,'r') as f:
self.labyrinth = labyrinth.Labyrinth(game=self,stream=f)
treasure = Treasure()
treasure = Treasure(self)
ttile = random.choice(self.labyrinth.getFreeTiles())
self.labyrinth.createThing(treasure,ttile)
euclid = Euclid(self)
etile = random.choice(self.labyrinth.getFreeTiles())
self.labyrinth.createThing(euclid,etile)
self.sock = socket.socket()
self.sock.bind(('0.0.0.0', port))
self.sock.listen(100)
......
......@@ -62,7 +62,7 @@ class Labyrinth:
if char in [' ','0','_']:
pass
elif char in ['W','w','#','1']:
wall = Wall()
wall = Wall(self.game)
wall.field = field
field._addThing(wall)
else:
......@@ -152,6 +152,8 @@ class Labyrinth:
sourceField = teller.field
receivers = []
for neighborField in [sourceField.neighbor(dy,dx) for dy,dx in directions.values()]:
if neighborField is None:
continue
for thing in neighborField.things:
if thing is teller:
continue
......@@ -161,4 +163,5 @@ class Labyrinth:
if hasattr(teller,'uid'):
admin.send("player {0} {1}s: {2}".format(teller.uid, verb, message))
else:
admin.send("{0} {1}s: {2}".format(str(teller), verb, message))
\ No newline at end of file
admin.send("{0} {1}s: {2}".format(str(teller), verb, message))
return receivers
\ No newline at end of file
from thing import Thing
import random
from labyrinth import Labyrinth
class Npc (Thing):
class DialogLevel:
def __init__(self, notunderstandable=None):
def __init__(self, npc, notunderstandable=None):
assert isinstance(npc,Npc)
self.npc = npc
if notunderstandable is None:
notunderstandable = ["I don't quite understand you.","What do you mean?","Excuse me?","Can you re-phrase that?"]
self.notunderstandable = notunderstandable
......@@ -11,13 +14,12 @@ class Npc (Thing):
return ("say", "No, I won't join you.", None)
def analyzeLetPass(self, teller, verb, message):
return ("say", "I won't let you pass so easily.", None)
def checkStandardRequests(self, teller, verbbbb, message):
def checkStandardRequests(self, teller, verb, message):
# if you overwrite this, call super first. You should only do something different if it returns None
msg = message.lower()
if ("come" in msg or "join" in msg or "protect" in msg or "help" in msg) and "me" in msg:
return self.analyzeComeWithMe(teller, verb, message)
elif ("let me" in msg and ("pass" in msg or "through" in msg or "go" in msg))
or ("away" in msg):
elif ("let me" in msg and ("pass" in msg or "through" in msg or "go" in msg)) or ("away" in msg):
return self.analyzeLetPass(teller, verb, message)
else:
return None
......@@ -29,15 +31,34 @@ class Npc (Thing):
resp = self.checkStandardRequests(teller,verb,message)
if resp is None: # didn't understand
return ("say",random.choice(self.notunderstandable),None)
else:
return resp
def __init__ (self, game, greetings, initialDialogLevel):
def __init__ (self, game, initialDialogLevel):
super().__init__(game)
self.greetings = greetings
assert isinstance(initialDialogLevel,Npc.DialogLevel)
self.currentDialogLevel = initialDialogLevel
def __repr__ (self):
return "N"
def __str__(self):
return "an NPC"
# convenience function: randomly teleports the npc to a free location
def relocate(self):
newfield = random.choice(self.game.labyrinth.getFreeTiles())
assert isinstance(newfield, Labyrinth.Field)
self.game.labyrinth.moveThing(self,newfield)
def afterAnswering(self):
pass
def tell(self, teller, verb, message):
newmsg, newverb, newdialvl = self.currentDialogLevel.analyzeAnswer(teller, verb, message)
assert isinstance(self.currentDialogLevel,Npc.DialogLevel)
analresp = self.currentDialogLevel.analyzeAnswer(teller, verb, message)
assert len(analresp) == 3
newmsg, newverb, newdialvl = analresp
if newdialvl is not None:
assert isinstance(newdialvl,Npc.DialogLevel)
self.currentDialogLevel = newdialvl
......@@ -46,4 +67,4 @@ class Npc (Thing):
self.game.labyrinth.tell(self, newverb, newmsg)
else:
teller.tell(self, "does not react", "")
self.afterAnswering()
\ No newline at end of file
......@@ -155,7 +155,7 @@ class Player(Thing):
if wallClose:
self.send("You are talking to the walls. This is usually not very effective.")
else:
self.send("You want to say something, but then you realize nobody's caring anyway. Get over it!")
self.send("If you say something, but nobody is there to listen to you, is there a sound at all? We don't know and it doesn't matter.")
elif verb in leaveVerbs:
self.send("Good Bye!")
self.game.removePlayer(self)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment