92 lines
2.2 KiB
Python
92 lines
2.2 KiB
Python
|
#! /usr/bin/env python3
|
||
|
# -*- coding: utf-8 -*-
|
||
|
"""
|
||
|
Hangman.
|
||
|
"""
|
||
|
import random
|
||
|
import module
|
||
|
|
||
|
class Hangman():
|
||
|
def __init__(self):
|
||
|
self.running = False
|
||
|
|
||
|
def newgame(self):
|
||
|
self.running = True
|
||
|
self.tries = 8
|
||
|
self.word = self._PickWord()
|
||
|
self.working = [x for x in self.word]
|
||
|
self.blanks = list('_' * len(self.word))
|
||
|
for n,char in enumerate(self.word):
|
||
|
if char == ' ':
|
||
|
self.blanks[n] = ' '
|
||
|
|
||
|
def _PickWord(self):
|
||
|
with open("/home/iou1name/.sopel/wordlist.txt",'r') as file:
|
||
|
lines = file.readlines()
|
||
|
wrd = list(lines[ random.randint(0, len(lines))-1 ].strip())
|
||
|
return wrd
|
||
|
|
||
|
def solve(self, guess):
|
||
|
if list(guess) == self.word:
|
||
|
self.running = False
|
||
|
self.blanks = self.word
|
||
|
return 'win'
|
||
|
|
||
|
elif guess in self.word and len(guess) == 1:
|
||
|
while guess in self.working:
|
||
|
index = self.working.index(guess)
|
||
|
self.blanks[index] = guess
|
||
|
self.working[index] = '_'
|
||
|
return 'correct'
|
||
|
else:
|
||
|
self.tries = self.tries - 1
|
||
|
if self.tries == 0:
|
||
|
self.running = False
|
||
|
self.blanks = self.word
|
||
|
return 'lose'
|
||
|
else:
|
||
|
return 'incorrect'
|
||
|
|
||
|
def return_blanks(self):
|
||
|
return ''.join(self.blanks)
|
||
|
|
||
|
hangman = Hangman()
|
||
|
|
||
|
@module.commands('hangman')
|
||
|
@module.example('.hangman')
|
||
|
def hangman_start(bot, trigger):
|
||
|
"""Starts a game of hangman."""
|
||
|
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:
|
||
|
bot.say('Fuck.')
|
||
|
|
||
|
bot.say(hangman.return_blanks())
|