euclid.py 1.58 KB
Newer Older
Constantin's avatar
Constantin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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()