rewrote hangman.py
This commit is contained in:
parent
58f3cce7d0
commit
22d76ea932
|
@ -1,91 +1,87 @@
|
||||||
#! /usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""
|
"""
|
||||||
Hangman.
|
A class and functions needed for playing handman.
|
||||||
"""
|
"""
|
||||||
|
import os
|
||||||
import random
|
import random
|
||||||
import module
|
|
||||||
|
from module import commands, example
|
||||||
|
|
||||||
class Hangman():
|
class Hangman():
|
||||||
def __init__(self):
|
def __init__(self, bot):
|
||||||
self.running = False
|
|
||||||
|
|
||||||
def newgame(self):
|
|
||||||
self.running = True
|
|
||||||
self.tries = 8
|
self.tries = 8
|
||||||
self.word = self._PickWord()
|
self.word = self._PickWord(bot)
|
||||||
self.working = [x for x in self.word]
|
self.working = [x for x in self.word]
|
||||||
self.blanks = list('_' * len(self.word))
|
self.blanks = list('_' * len(self.word))
|
||||||
for n,char in enumerate(self.word):
|
|
||||||
if char == ' ':
|
|
||||||
self.blanks[n] = ' '
|
|
||||||
|
|
||||||
def _PickWord(self):
|
def _PickWord(self, bot):
|
||||||
with open("/home/iou1name/fulvia/static/wordlist.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 solve(self, guess):
|
def update(self, guess):
|
||||||
if list(guess) == self.word:
|
if not guess in self.word:
|
||||||
self.running = False
|
return
|
||||||
self.blanks = self.word
|
|
||||||
return 'win'
|
|
||||||
|
|
||||||
elif guess in self.word and len(guess) == 1:
|
for n, char in enumerate(self.word):
|
||||||
while guess in self.working:
|
if char == guess:
|
||||||
index = self.working.index(guess)
|
self.blanks[n] = guess
|
||||||
self.blanks[index] = guess
|
|
||||||
self.working[index] = '_'
|
|
||||||
return 'correct'
|
def setup(bot):
|
||||||
|
bot.memory["hangman"] = {}
|
||||||
|
|
||||||
|
|
||||||
|
@commands("hangman", "hm")
|
||||||
|
def hangman(bot, trigger):
|
||||||
|
"""
|
||||||
|
Plays hangman. --start [-s] to start a new game, otherwise words are
|
||||||
|
taken as attempts to solve and single characters are taken as guesses.
|
||||||
|
"""
|
||||||
|
if not trigger.group(2):
|
||||||
|
return bot.reply("Hang what?")
|
||||||
|
|
||||||
|
if trigger.group(3) == "--start" or trigger.group(3) == "-s":
|
||||||
|
if bot.memory["hangman"].get(trigger.channel):
|
||||||
|
return bot.reply("There is already a game running in this channel.")
|
||||||
|
|
||||||
|
bot.memory["hangman"][trigger.channel] = Hangman(bot)
|
||||||
|
msg = f"{trigger.nick} has started a game of hangman! " \
|
||||||
|
+ "Use '.hangman [guess]' to guess a letter or the entire word."
|
||||||
|
bot.msg(msg)
|
||||||
|
bot.say("".join(bot.memory["hangman"][trigger.channel].blanks))
|
||||||
|
return
|
||||||
|
|
||||||
|
if not bot.memory["hangman"].get(trigger.channel):
|
||||||
|
msg = "There is no game currently running in this channel. " \
|
||||||
|
+ "Use '.hangman --start' to start one"
|
||||||
|
return bot.reply(msg)
|
||||||
|
|
||||||
|
if len(trigger.group(2)) > 1:
|
||||||
|
if trigger.group(2) == bot.memory["hangman"][trigger.channel].word:
|
||||||
|
bot.say(f"{trigger.nick} has won!")
|
||||||
|
bot.say(bot.memory["hangman"][trigger.channel].word)
|
||||||
|
bot.memory["hangman"].pop(trigger.channel)
|
||||||
|
return
|
||||||
else:
|
else:
|
||||||
self.tries = self.tries - 1
|
msg = "Incorrect. " \
|
||||||
if self.tries == 0:
|
+ f"{bot.memory['hangman'][trigger.channel].tries} tries left."
|
||||||
self.running = False
|
bot.reply(msg)
|
||||||
self.blanks = self.word
|
|
||||||
return 'lose'
|
|
||||||
else:
|
|
||||||
return 'incorrect'
|
|
||||||
|
|
||||||
def return_blanks(self):
|
elif len(trigger.group(2)) == 1:
|
||||||
return ''.join(self.blanks)
|
if trigger.group(2) in bot.memory["hangman"][trigger.channel].word:
|
||||||
|
bot.reply("Correct!")
|
||||||
|
bot.memory["hangman"][trigger.channel].update(trigger.group(2))
|
||||||
|
|
||||||
hangman = Hangman()
|
else:
|
||||||
|
bot.memory["hangman"][trigger.channel].tries -= 1
|
||||||
|
msg = "Incorrect. " \
|
||||||
|
+ f"{bot.memory['hangman'][trigger.channel].tries} tries left."
|
||||||
|
bot.reply(msg)
|
||||||
|
|
||||||
@module.commands('hangman')
|
if bot.memory["hangman"][trigger.channel].tries <= 0:
|
||||||
@module.example('.hangman')
|
bot.say("Game over!")
|
||||||
def hangman_start(bot, trigger):
|
bot.say(bot.memory['hangman'][trigger.channel].word)
|
||||||
"""Starts a game of hangman."""
|
bot.memory["hangman"].pop(trigger.channel)
|
||||||
if hangman.running:
|
|
||||||
bot.reply("There is already a game running.")
|
|
||||||
return
|
|
||||||
|
|
||||||
hangman.newgame()
|
|
||||||
bot.say(trigger.nick + " has started a game of hangman! Type .guess to guess a letter or the entire phrase.")
|
|
||||||
bot.say(hangman.return_blanks())
|
|
||||||
|
|
||||||
|
|
||||||
@module.commands('guess')
|
|
||||||
@module.example('.guess a')
|
|
||||||
@module.example('.guess anus')
|
|
||||||
def guess(bot, trigger):
|
|
||||||
"""Makes a guess in hangman. May either guess a single letter or the entire word/phrase."""
|
|
||||||
if not hangman.running:
|
|
||||||
bot.reply('There is no game currently running. Use .hangman to start one')
|
|
||||||
return
|
|
||||||
|
|
||||||
response = hangman.solve(trigger.group(2))
|
|
||||||
|
|
||||||
if response == 'win':
|
|
||||||
bot.say(trigger.nick + " has won!")
|
|
||||||
elif response == 'correct':
|
|
||||||
pass
|
|
||||||
elif response == 'lose':
|
|
||||||
bot.say("Game over.")
|
|
||||||
elif response == 'incorrect':
|
|
||||||
bot.reply("incorrect.")
|
|
||||||
bot.say( str(hangman.tries) + " tries left." )
|
|
||||||
else:
|
else:
|
||||||
bot.say('Fuck.')
|
bot.say("".join(bot.memory["hangman"][trigger.channel].blanks))
|
||||||
|
|
||||||
bot.say(hangman.return_blanks())
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user