fulvia/modules/hangman.py

91 lines
2.6 KiB
Python
Raw Normal View History

2018-03-29 21:27:04 -04:00
#!/usr/bin/env python3
2018-03-16 03:13:43 -04:00
"""
2018-03-29 21:27:04 -04:00
A class and functions needed for playing handman.
2018-03-16 03:13:43 -04:00
"""
2018-03-29 21:27:04 -04:00
import os
2018-03-16 03:13:43 -04:00
import random
2018-03-29 21:27:04 -04:00
from module import commands, example
2018-03-16 03:13:43 -04:00
2018-03-29 21:27:04 -04:00
class Hangman():
def __init__(self, bot):
2018-03-16 03:13:43 -04:00
self.tries = 8
2018-03-29 21:27:04 -04:00
self.word = self._PickWord(bot)
2018-03-16 03:13:43 -04:00
self.working = [x for x in self.word]
self.blanks = list('_' * len(self.word))
2018-03-29 21:27:04 -04:00
def _PickWord(self, bot):
with open(os.path.join(bot.static, "wordlist.txt"), "r") as file:
word = random.choice(file.read().splitlines())
return word
2018-03-16 03:13:43 -04:00
2018-03-29 21:27:04 -04:00
def update(self, guess):
if not guess in self.word:
return
2018-03-16 03:13:43 -04:00
2018-03-29 21:27:04 -04:00
for n, char in enumerate(self.word):
if char == guess:
self.blanks[n] = guess
2018-03-16 03:13:43 -04:00
2018-03-29 21:27:04 -04:00
def setup(bot):
bot.memory["hangman"] = {}
2018-03-16 03:13:43 -04:00
2018-03-29 21:27:04 -04:00
@commands("hangman", "hm")
2018-03-29 22:05:37 -04:00
@example(".hangman --start")
@example(".hm anus")
2018-03-29 21:27:04 -04:00
def hangman(bot, trigger):
"""
Plays hangman. --start [-s] to start a new game, otherwise words are
taken as attempts to solve and single characters are taken as guesses.
"""
2020-01-07 18:58:19 -05:00
if len(trigger.args) < 2:
2018-03-29 21:27:04 -04:00
return bot.reply("Hang what?")
2018-03-16 03:13:43 -04:00
2020-01-07 18:58:19 -05:00
if trigger.args[1] == "--start" or trigger.args[1] == "-s":
2018-03-29 21:27:04 -04:00
if bot.memory["hangman"].get(trigger.channel):
return bot.reply("There is already a game running in this channel.")
2018-03-16 03:13:43 -04:00
2018-03-29 21:27:04 -04:00
bot.memory["hangman"][trigger.channel] = Hangman(bot)
msg = f"{trigger.nick} has started a game of hangman! " \
+ "Use '.hangman [guess]' to guess a letter or the entire word."
bot.msg(msg)
2018-05-25 15:21:18 -04:00
bot.msg("".join(bot.memory["hangman"][trigger.channel].blanks))
2018-03-16 03:13:43 -04:00
return
2018-03-29 21:27:04 -04:00
if not bot.memory["hangman"].get(trigger.channel):
msg = "There is no game currently running in this channel. " \
+ "Use '.hangman --start' to start one"
return bot.reply(msg)
2018-03-16 03:13:43 -04:00
2020-01-07 18:58:19 -05:00
if len(trigger.args[1]) > 1:
if trigger.args[1] == bot.memory["hangman"][trigger.channel].word:
2018-05-25 15:21:18 -04:00
bot.msg(f"{trigger.nick} has won!")
bot.msg(bot.memory["hangman"][trigger.channel].word)
2018-03-29 21:27:04 -04:00
bot.memory["hangman"].pop(trigger.channel)
return
else:
bot.memory["hangman"][trigger.channel].tries -= 1
2018-03-29 21:27:04 -04:00
msg = "Incorrect. " \
+ f"{bot.memory['hangman'][trigger.channel].tries} tries left."
bot.reply(msg)
2020-01-07 18:58:19 -05:00
elif len(trigger.args[1]) == 1:
if trigger.args[1] in bot.memory["hangman"][trigger.channel].word:
2018-03-29 21:27:04 -04:00
bot.reply("Correct!")
2020-01-07 18:58:19 -05:00
bot.memory["hangman"][trigger.channel].update(trigger.args[1])
2018-03-16 03:13:43 -04:00
2018-03-29 21:27:04 -04:00
else:
bot.memory["hangman"][trigger.channel].tries -= 1
msg = "Incorrect. " \
+ f"{bot.memory['hangman'][trigger.channel].tries} tries left."
bot.reply(msg)
if bot.memory["hangman"][trigger.channel].tries <= 0:
2018-05-25 15:21:18 -04:00
bot.msg("Game over!")
bot.msg(bot.memory['hangman'][trigger.channel].word)
2018-03-29 21:27:04 -04:00
bot.memory["hangman"].pop(trigger.channel)
else:
2018-05-25 15:21:18 -04:00
bot.msg("".join(bot.memory["hangman"][trigger.channel].blanks))