2018-03-16 03:13:43 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
2018-03-29 22:05:37 -04:00
|
|
|
A class and functions needed for playing handman.
|
2018-03-16 03:13:43 -04:00
|
|
|
"""
|
2018-03-29 22:05:37 -04:00
|
|
|
import os
|
2018-03-16 03:13:43 -04:00
|
|
|
import random
|
|
|
|
|
2018-03-29 22:05:37 -04:00
|
|
|
from module import commands, example
|
2018-03-16 03:13:43 -04:00
|
|
|
|
2018-03-29 22:05:37 -04:00
|
|
|
class Scramble():
|
|
|
|
def __init__(self, bot):
|
|
|
|
self.word = self._PickWord(bot)
|
2018-03-16 03:13:43 -04:00
|
|
|
self.shuffled = [x for x in self.word]
|
|
|
|
random.shuffle(self.shuffled)
|
2018-03-29 22:05:37 -04:00
|
|
|
self.shuffled = "".join(self.shuffled)
|
2018-03-16 03:13:43 -04:00
|
|
|
|
2018-03-29 22:05:37 -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 22:05:37 -04:00
|
|
|
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
|
2018-03-16 03:13:43 -04:00
|
|
|
|
2018-03-29 22:05:37 -04:00
|
|
|
word1 = list(word1)
|
|
|
|
word2 = list(word2)
|
2018-03-16 03:13:43 -04:00
|
|
|
|
2018-03-29 22:05:37 -04:00
|
|
|
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")
|
2020-01-08 07:51:00 -05:00
|
|
|
@example(".sc substation")
|
2018-03-29 22:05:37 -04:00
|
|
|
def scramble(bot, trigger):
|
|
|
|
"""
|
|
|
|
Plays scramble. --start [-s] to start a new game, otherwise arguments
|
|
|
|
are taken as attempts to solve.
|
|
|
|
"""
|
2020-01-07 18:58:19 -05:00
|
|
|
if len(trigger.args) < 2:
|
2018-03-29 22:05:37 -04:00
|
|
|
return bot.reply("Scramble what?")
|
|
|
|
|
2020-01-07 18:58:19 -05:00
|
|
|
if trigger.args[1] == "--start" or trigger.args[1] == "-s":
|
2018-03-29 22:05:37 -04:00
|
|
|
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)
|
2018-05-25 15:21:18 -04:00
|
|
|
bot.msg(bot.memory["scramble"][trigger.channel].shuffled)
|
2018-03-16 03:13:43 -04:00
|
|
|
return
|
|
|
|
|
2018-03-29 22:05:37 -04:00
|
|
|
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
|
2020-01-07 18:58:19 -05:00
|
|
|
if isAnagram(bot, word, trigger.args[1]):
|
2018-05-25 15:21:18 -04:00
|
|
|
bot.msg(f"{trigger.nick} has won!")
|
2018-03-29 22:05:37 -04:00
|
|
|
msg = "Original word: " \
|
|
|
|
+ bot.memory["scramble"][trigger.channel].word
|
2018-05-25 15:21:18 -04:00
|
|
|
bot.msg(msg)
|
2018-03-29 22:05:37 -04:00
|
|
|
bot.memory["scramble"].pop(trigger.channel)
|
|
|
|
else:
|
|
|
|
bot.reply("Incorrect.")
|
2018-05-25 15:21:18 -04:00
|
|
|
bot.msg(bot.memory["scramble"][trigger.channel].shuffled)
|