#!/usr/bin/env python3 """ A class and functions needed for playing handman. """ import os import random from module import commands, example class Scramble(): def __init__(self, bot): self.word = self._PickWord(bot) self.shuffled = [x for x in self.word] random.shuffle(self.shuffled) self.shuffled = "".join(self.shuffled) def _PickWord(self, bot): with open(os.path.join(bot.static, "wordlist.txt"), "r") as file: word = random.choice(file.read().splitlines()) return word def isAnagram(bot, word1, word2): """Checks to see if word2 is an anagram of word1.""" with open(os.path.join(bot.static, "wordlist.txt"), "r") as file: words = file.read().splitlines() if not word2 in words: return False word1 = list(word1) word2 = list(word2) for char in word1: if char in word2: word2.remove(char) else: return False if not word2: return True else: return False def setup(bot): bot.memory["scramble"] = {} @commands("scramble", "sc") @example(".scramble --start") @example(".sc anus") def scramble(bot, trigger): """ Plays scramble. --start [-s] to start a new game, otherwise arguments are taken as attempts to solve. """ if not trigger.group(2): return bot.reply("Scramble what?") if trigger.group(3) == "--start" or trigger.group(3) == "-s": if bot.memory["scramble"].get(trigger.channel): return bot.reply("There is already a game running in this channel.") bot.memory["scramble"][trigger.channel] = Scramble(bot) msg = f"{trigger.nick} has started a game of scramble! " \ + "Use '.scramble [guess]' to guess a letter or the entire word." bot.msg(msg) bot.msg(bot.memory["scramble"][trigger.channel].shuffled) return if not bot.memory["scramble"].get(trigger.channel): msg = "There is no game currently running in this channel. " \ + "Use '.scramble --start' to start one" return bot.reply(msg) word = bot.memory["scramble"][trigger.channel].word if isAnagram(bot, word, trigger.group(2)): bot.msg(f"{trigger.nick} has won!") msg = "Original word: " \ + bot.memory["scramble"][trigger.channel].word bot.msg(msg) bot.memory["scramble"].pop(trigger.channel) else: bot.reply("Incorrect.") bot.msg(bot.memory["scramble"][trigger.channel].shuffled)