sopel/modules/scramble.py
2017-11-22 19:26:40 -05:00

80 lines
1.8 KiB
Python
Executable File

# coding=utf-8
"""
Scramble.
"""
import random
import module
class Scramble():
def __init__(self):
self.running = False
def newgame(self):
self.running = True
self.word = self._PickWord()
self.shuffled = [x for x in self.word]
random.shuffle(self.shuffled)
def _PickWord(self):
with open("/home/iou1name/.sopel/words6.txt",'r') as file:
lines = file.readlines()
wrd = list(lines[ random.randint(0, len(lines))-1 ].strip())
return wrd
def gameover(self):
self.running = False
self.shuffled = self.word
def isAnagram(givenWord, givenGuess):
word = [x for x in givenWord]
guess = [x for x in givenGuess]
with open('/home/iou1name/.sopel/words6.txt', 'r') as file:
words = file.readlines()
if not ''.join(guess)+'\n' in words:
return "notaword"
del words
for char in word:
if char in guess:
guess.pop(guess.index(char))
else:
return 'incorrect'
return 'correct'
scramble = Scramble()
@module.commands('scramble')
@module.example('.scramble')
def scramble_start(bot, trigger):
"""Starts a game of scramble."""
if scramble.running:
bot.reply("There is already a game running.")
return
scramble.newgame()
bot.say(trigger.nick + " has started a game of scramble! Type .sc to guess the solution.")
bot.say(''.join(scramble.shuffled))
@module.commands('sc')
@module.example('.sc anus')
def guess(bot, trigger):
"""Makes a guess in scramble."""
if not scramble.running:
bot.reply('There is no game currently running. Use .scramble to start one')
return
response = isAnagram(scramble.word, list(trigger.group(2)))
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))