rewrote scramble.py

This commit is contained in:
iou1name 2018-03-29 22:05:37 -04:00
parent 22d76ea932
commit 37e11985c7
2 changed files with 68 additions and 60 deletions

View File

@ -33,6 +33,8 @@ def setup(bot):
@commands("hangman", "hm") @commands("hangman", "hm")
@example(".hangman --start")
@example(".hm anus")
def hangman(bot, trigger): def hangman(bot, trigger):
""" """
Plays hangman. --start [-s] to start a new game, otherwise words are Plays hangman. --start [-s] to start a new game, otherwise words are

View File

@ -1,79 +1,85 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
""" """
Scramble. A class and functions needed for playing handman.
""" """
import os
import random import random
import module
from module import commands, example
class Scramble(): class Scramble():
def __init__(self): def __init__(self, bot):
self.running = False self.word = self._PickWord(bot)
def newgame(self):
self.running = True
self.word = self._PickWord()
self.shuffled = [x for x in self.word] self.shuffled = [x for x in self.word]
random.shuffle(self.shuffled) random.shuffle(self.shuffled)
self.shuffled = "".join(self.shuffled)
def _PickWord(self): def _PickWord(self, bot):
with open("/home/iou1name/fulvia/static/words6.txt",'r') as file: with open(os.path.join(bot.static, "wordlist.txt"), "r") as file:
lines = file.readlines() word = random.choice(file.read().splitlines())
wrd = list(lines[ random.randint(0, len(lines))-1 ].strip()) return word
return wrd
def gameover(self):
self.running = False
self.shuffled = self.word
def isAnagram(givenWord, givenGuess): def isAnagram(bot, word1, word2):
word = [x for x in givenWord] """Checks to see if word2 is an anagram of word1."""
guess = [x for x in givenGuess] with open(os.path.join(bot.static, "wordlist.txt"), "r") as file:
with open('/home/iou1name/fulvia/static/words6.txt', 'r') as file: words = file.read().splitlines()
words = file.readlines() if not word2 in words:
if not ''.join(guess)+'\n' in words: return False
return "notaword"
del words
for char in word: word1 = list(word1)
if char in guess: word2 = list(word2)
guess.pop(guess.index(char))
for char in word1:
if char in word2:
word2.remove(char)
else: else:
return 'incorrect' return False
return 'correct'
if not word2:
return True
else:
return False
scramble = Scramble() def setup(bot):
bot.memory["scramble"] = {}
@module.commands('scramble')
@module.example('.scramble') @commands("scramble", "sc")
def scramble_start(bot, trigger): @example(".scramble --start")
"""Starts a game of scramble.""" @example(".sc anus")
if scramble.running: def scramble(bot, trigger):
bot.reply("There is already a game running.") """
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.say(bot.memory["scramble"][trigger.channel].shuffled)
return return
scramble.newgame() if not bot.memory["scramble"].get(trigger.channel):
bot.say(trigger.nick + " has started a game of scramble! Type .sc to guess the solution.") msg = "There is no game currently running in this channel. " \
bot.say(''.join(scramble.shuffled)) + "Use '.scramble --start' to start one"
return bot.reply(msg)
word = bot.memory["scramble"][trigger.channel].word
@module.commands('sc') if isAnagram(bot, word, trigger.group(2)):
@module.example('.sc anus') bot.say(f"{trigger.nick} has won!")
def guess(bot, trigger): msg = "Original word: " \
"""Makes a guess in scramble.""" + bot.memory["scramble"][trigger.channel].word
if not scramble.running: bot.say(msg)
bot.reply('There is no game currently running. Use .scramble to start one') bot.memory["scramble"].pop(trigger.channel)
return else:
bot.reply("Incorrect.")
response = isAnagram(scramble.word, list(trigger.group(2))) bot.say(bot.memory["scramble"][trigger.channel].shuffled)
if response == 'correct':
bot.say(trigger.nick + " has won!")
scramble.gameover()
elif response == 'notaword':
bot.say("I don't recognize that word.")
elif response == 'incorrect':
bot.reply("incorrect.")
bot.say(''.join(scramble.shuffled))