diff --git a/.gitignore b/.gitignore index 3f998a0..bec5766 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ config.yml # Cache folder __pycache__ +.venv \ No newline at end of file diff --git a/extensions/guess.py b/extensions/guess.py new file mode 100644 index 0000000..d8d3d45 --- /dev/null +++ b/extensions/guess.py @@ -0,0 +1,68 @@ +import discord +from discord.ext import commands +import random +from main import get_prefix + +# Name of the command that activates the game. +COMMAND = 'guess' +# Prefix registeration +PREFIX = get_prefix() + +class Guess(commands.Cog): + def __init__(self, bot): + self.bot = bot + # The number the user is trying to guess + self.number = self.generate_number() + # Number of guesses made at getting the right number + self.attempts = 0 + # Track who is actually playing the game. + self.guesser = '' + # Track if the game is active. + self.active = False + + def generate_number(self): + return random.randint(0, 100) + + @commands.command(name=COMMAND) + async def guess(self, ctx): + """Guess the number""" + self.guesser = ctx.author + self.active = True + await ctx.send('''Starting guessing game with <@!{}> +Guess a number between 1 and 100. Stop guessing to quit.'''.format(ctx.author.id)) + + @commands.Cog.listener() + async def on_message(self, ctx): + if ctx.author == self.guesser and self.active == True and COMMAND not in ctx.content: + try: + guessed_number = int(ctx.content) + except ValueError: + await ctx.channel.send('Quitting guessing game...') + self.number = self.generate_number() + self.attempts = 0 + self.active = False + return + + self.attempts += 1 + + if guessed_number > self.number: + await ctx.channel.send('Too high.') + return + + if guessed_number < self.number: + await ctx.channel.send('Too low.') + return + + await ctx.channel.send( + '{0}. You guessed right! Guesses made: {1}'.format( + self.number, + self.attempts + ) + ) + self.number = self.generate_number() + self.attempts = 0 + self.active = False + return + +def setup(bot): + bot.add_cog(Guess(bot)) diff --git a/extensions/hangman.py b/extensions/hangman.py new file mode 100644 index 0000000..c0c0636 --- /dev/null +++ b/extensions/hangman.py @@ -0,0 +1,81 @@ +import discord +from discord.ext import commands +import random +from main import get_prefix + +# Name of the command that activates the game. +COMMAND = 'hangman' +# Prefix registeration +PREFIX = get_prefix() + + + +class Hangman(commands.Cog): + def __init__(self, bot): + self.bot = bot + # The number the user is trying to guess + self.word = self.generate_word() + # Number of guesses made at getting the right number + self.attempts = 0 + # Track who is actually playing the game. + self.guesser = '' + # Track if the game is active. + self.active = False + + def generate_word(self): + with open ("extensions\\words_for_hangman.txt",'r') as f: + for i in f.readlines(): + word_list = i.split('|') + index = random.randint(0,len(word_list)-1) + return word_list[index] + + @commands.command(name=COMMAND) + async def guess(self, ctx): + """Guess the number""" + self.guesser = ctx.author + self.active = True + await ctx.send('''Starting guessing game with <@!{}> +Start guessing letters and see if you can solve for the word. Stop guessing to quit.'''.format(ctx.author.id)) + + @commands.Cog.listener() + async def on_message(self, ctx): + if ctx.author == self.guesser and self.active == True and COMMAND not in ctx.content: + try: + guessed_number = int(ctx.content) + except ValueError: + await ctx.channel.send('Quitting guessing game...') + self.number = self.generate_number() + self.attempts = 0 + self.active = False + return + + self.attempts += 1 + + if guessed_number > self.number: + await ctx.channel.send('Too high.') + return + + if guessed_number < self.number: + await ctx.channel.send('Too low.') + return + + await ctx.channel.send( + '{0}. You guessed right! Guesses made: {1}'.format( + self.number, + self.attempts + ) + ) + self.number = self.generate_number() + self.attempts = 0 + self.active = False + return + + @commands.Cog.listener() + async def on_message(self, ctx): + if ctx.author == self.guesser and self.active == True and COMMAND not in ctx.content: + await ctx.channel.send( + '_'*len(self.word) + ) + +def setup(bot): + bot.add_cog(Hangman(bot)) \ No newline at end of file diff --git a/rng.py b/extensions/rng.py similarity index 100% rename from rng.py rename to extensions/rng.py diff --git a/extensions/test.py b/extensions/test.py new file mode 100644 index 0000000..d6ef448 --- /dev/null +++ b/extensions/test.py @@ -0,0 +1,78 @@ +from io import StringIO +from random import randint + + +class Hangman(): + + def __init__(self) -> None: + # The number the user is trying to guess + self.word = self.generate_word() + # Number of guesses made at getting the right number + self.fail_count = 0 + # Track who is actually playing the game. + self.guesser = '' + # Track if the game is active. + self.active = True + + def generate_word(self): + #todo: change to newline separated txt file + with open ("extensions\\words_for_hangman.txt",'r') as f: + for i in f.readlines(): + word_list = i.split('|') + index = randint(0,len(word_list)-1) + return word_list[index] + + def guess(self): + user_guess = input("Guess a letter that is in the word or the whole word: ") + if len(user_guess) > 1: + whole_word_guess = user_guess + return ("whole_word_guess", whole_word_guess) + else: + return ("user_guess",user_guess) + + def game_logic(self): + building_word = '' + while self.active == True and self.fail_count < 6: + word_screen = '' + for let in self.word: + if let not in building_word: + word_screen+='_ ' + else: + word_screen+=let + print(word_screen) + + if word_screen == self.word: + return "YOU WIN!" + + guess = self.guess() + if guess[0] == "whole_word_guess": + if guess[1] == self.word: + return "Hey you've guessed the word and you win!" + else: + self.fail_count +=1 + if self.fail_count == 6: + print("GAME OVER.") + print("Sorry! {0} is not correct! You have {1} guesses left. ".format(guess[1], 6-self.fail_count)) + else: + if guess[1] in self.word: + print("that's great! {0} is one of the letters!".format(guess[1])) + building_word+=guess[1] + else: + self.fail_count +=1 + if self.fail_count == 6: + print("GAME OVER.") + print("Sorry {0} is NOT in the word".format(guess[1])) + #todo: output the word on failure. + + + + + + + +if __name__ == "__main__": + + h = Hangman() + + h.generate_word() + print(h.game_logic()) \ No newline at end of file diff --git a/extensions/words_for_hangman.txt b/extensions/words_for_hangman.txt new file mode 100644 index 0000000..15732d9 --- /dev/null +++ b/extensions/words_for_hangman.txt @@ -0,0 +1 @@ +any|words|will|do|firetruck \ No newline at end of file diff --git a/guess.py b/guess.py deleted file mode 100644 index 845e271..0000000 --- a/guess.py +++ /dev/null @@ -1,45 +0,0 @@ -import discord -from discord.ext import commands -import random - -class Guess(commands.Cog): - def __init__(self, bot): - self.bot = bot - # The number the user is trying to guess - self.number = self.generate_number() - # Number of guesses made at getting the right number - self.attempts = 0 - - def generate_number(self): - return random.randint(0, 100) - - @commands.command(name='guess') - async def guess(self, ctx): - """Guess the number""" - try: - guessed_number = int(ctx.message.clean_content.split(' ')[-1]) - except Exception: - await ctx.send('Guess a number between 0 and 100') - return - - self.attempts += 1 - - if guessed_number > self.number: - await ctx.send('Too high.') - return - - if guessed_number < self.number: - await ctx.send('Too low.') - return - - await ctx.send( - '{0}. You guessed right! Guesses made: {1}'.format( - self.number, - self.attempts - ) - ) - self.number = self.generate_number() - self.attempts = 0 - -def setup(bot): - bot.add_cog(Guess(bot)) diff --git a/main.py b/main.py index 2f023b7..c1842db 100644 --- a/main.py +++ b/main.py @@ -1,24 +1,26 @@ import discord from discord.ext import commands import yaml +import os with open('config.yml', 'r') as file: config = yaml.safe_load(file) -def get_prefix(bot, message): +def get_prefix(): """A callable Prefix for our bot. This could be edited to allow per server prefixes.""" # Prefixes may also contain spaces prefixes = ['.'] - return commands.when_mentioned_or(*prefixes)(bot, message) + return commands.when_mentioned_or(*prefixes) #bot = discord.Client() -bot = commands.Bot(command_prefix='.', description='CodiHacks bot') +bot = commands.Bot(command_prefix=get_prefix(), description='CodiHacks bot') # Load cogs by the filenames -bot.load_extension('guess') -bot.load_extension('rng') +for i in os.listdir('extensions'): + if i.endswith('.py') and os.path.isfile('extensions/' + i): + bot.load_extension('extensions.' + i.replace('.py', '')) # Event listeners @bot.event