rewrote scramble.py
This commit is contained in:
parent
22d76ea932
commit
37e11985c7
|
@ -33,6 +33,8 @@ def setup(bot):
|
|||
|
||||
|
||||
@commands("hangman", "hm")
|
||||
@example(".hangman --start")
|
||||
@example(".hm anus")
|
||||
def hangman(bot, trigger):
|
||||
"""
|
||||
Plays hangman. --start [-s] to start a new game, otherwise words are
|
||||
|
|
|
@ -1,79 +1,85 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
Scramble.
|
||||
A class and functions needed for playing handman.
|
||||
"""
|
||||
import os
|
||||
import random
|
||||
import module
|
||||
|
||||
from module import commands, example
|
||||
|
||||
class Scramble():
|
||||
def __init__(self):
|
||||
self.running = False
|
||||
|
||||
def newgame(self):
|
||||
self.running = True
|
||||
self.word = self._PickWord()
|
||||
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):
|
||||
with open("/home/iou1name/fulvia/static/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 _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(givenWord, givenGuess):
|
||||
word = [x for x in givenWord]
|
||||
guess = [x for x in givenGuess]
|
||||
with open('/home/iou1name/fulvia/static/words6.txt', 'r') as file:
|
||||
words = file.readlines()
|
||||
if not ''.join(guess)+'\n' in words:
|
||||
return "notaword"
|
||||
del words
|
||||
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
|
||||
|
||||
for char in word:
|
||||
if char in guess:
|
||||
guess.pop(guess.index(char))
|
||||
word1 = list(word1)
|
||||
word2 = list(word2)
|
||||
|
||||
for char in word1:
|
||||
if char in word2:
|
||||
word2.remove(char)
|
||||
else:
|
||||
return 'incorrect'
|
||||
return 'correct'
|
||||
return False
|
||||
|
||||
if not word2:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
scramble = Scramble()
|
||||
def setup(bot):
|
||||
bot.memory["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.")
|
||||
|
||||
@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.say(bot.memory["scramble"][trigger.channel].shuffled)
|
||||
return
|
||||
|
||||
scramble.newgame()
|
||||
bot.say(trigger.nick + " has started a game of scramble! Type .sc to guess the solution.")
|
||||
bot.say(''.join(scramble.shuffled))
|
||||
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)
|
||||
|
||||
|
||||
@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))
|
||||
word = bot.memory["scramble"][trigger.channel].word
|
||||
if isAnagram(bot, word, trigger.group(2)):
|
||||
bot.say(f"{trigger.nick} has won!")
|
||||
msg = "Original word: " \
|
||||
+ bot.memory["scramble"][trigger.channel].word
|
||||
bot.say(msg)
|
||||
bot.memory["scramble"].pop(trigger.channel)
|
||||
else:
|
||||
bot.reply("Incorrect.")
|
||||
bot.say(bot.memory["scramble"][trigger.channel].shuffled)
|
||||
|
|
Loading…
Reference in New Issue
Block a user