questionman.py 1.31 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
47
48
49
50
from npc import Npc

import random

def testQuestion(qpair):
	q,ac = qpair
	print(q)
	a = input("> ")
	print("Is correct: ",ac(a))

class Questionman (Npc):
	class DLevel(Npc.DialogLevel):
		def __init__ (self,npc):
			super().__init__(npc)
			self.question, self.answerChecker = random.choice(self.npc.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 ("ask", self.question, None)
			else:
				if self.answerChecker(message):
					# correct answer
					self.npc.correctAnswerGiven = True
					newq, newac = random.choice(self.npc.questions)
					return ("say", "That answer is correct. I will go annoy people somewhere else. Good Bye.", Questionman.DLevel(self.npc))
				else: # wrong answer
					return ("shout", "WRONG! "+self.question, None)
	
	def __init__(self,game):
		super().__init__(game, Questionman.DLevel(self))
		self.correctAnswerGiven = False
	
	def __str__ (self):
		return "QuestionMan"
	
	def __repr__ (self):
		return "Q"
	
	def blocksMove(self):
		return True
	
	def afterAnswering(self):
		if self.correctAnswerGiven:
			self.correctAnswerGiven = False
			self.relocate()