pystermind/witty_retort.py

42 lines
1013 B
Python

import random
import functools
class MessageTable:
contents = []
def add_entry(self, string, probability):
entry = {
"remark": string,
"probability": probability,
"table": self
}
self.contents.append(entry)
def get_message(self):
probability_sum = functools.reduce(
lambda total, entry:
entry["probability"] + total,
self.contents, 0
);
hit_value = random.randrange(probability_sum)
running_value = 0
for i in self.contents:
running_value += i["probability"]
if hit_value < running_value:
return i
remarks = MessageTable()
remarks.add_entry("Not quite", 30)
remarks.add_entry("Keep trying", 20)
remarks.add_entry("Intresting strategy", 15)
remarks.add_entry("Close", 10)
remarks.add_entry("Don't give up", 10)
remarks.add_entry("I though you had it", 10)
remarks.add_entry("You can do it!", 5)