From 123d4a64aaf1b845c0f17ed1159ac109ef5c077b Mon Sep 17 00:00:00 2001 From: Trevor Corcoran Date: Thu, 16 Sep 2021 12:52:04 -0400 Subject: [PATCH 1/5] Added extensions folder and improved guessing game. --- extensions/guess.py | 61 +++++++++++++++++++++++++++++++++++++ rng.py => extensions/rng.py | 0 guess.py | 45 --------------------------- main.py | 6 ++-- 4 files changed, 65 insertions(+), 47 deletions(-) create mode 100644 extensions/guess.py rename rng.py => extensions/rng.py (100%) delete mode 100644 guess.py diff --git a/extensions/guess.py b/extensions/guess.py new file mode 100644 index 0000000..cdb165d --- /dev/null +++ b/extensions/guess.py @@ -0,0 +1,61 @@ +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 + # 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='guess') + async def guess(self, ctx): + """Guess the number""" + self.guesser = ctx.author + self.active = True + await ctx.send('Starting guessing game with {}'.format(ctx.author)) + + @commands.Cog.listener() + async def on_message(self, ctx): + if ctx.author == self.guesser and self.active == True and ctx.content.split(' ')[0] != '.guess': + 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/rng.py b/extensions/rng.py similarity index 100% rename from rng.py rename to extensions/rng.py 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..cf5a010 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ import discord from discord.ext import commands import yaml +import os with open('config.yml', 'r') as file: config = yaml.safe_load(file) @@ -17,8 +18,9 @@ def get_prefix(bot, message): bot = commands.Bot(command_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 From 9333004a704aa2d0c22ce0bcf77aad3b5a6236ce Mon Sep 17 00:00:00 2001 From: Trevor Corcoran Date: Thu, 16 Sep 2021 19:09:58 -0400 Subject: [PATCH 2/5] Fixed hard-coded references to command prefix in extensions/guess.py --- extensions/guess.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/extensions/guess.py b/extensions/guess.py index cdb165d..20b7e11 100644 --- a/extensions/guess.py +++ b/extensions/guess.py @@ -2,6 +2,9 @@ from discord.ext import commands import random +# Name of the command that activates the game. +COMMAND = 'guess' + class Guess(commands.Cog): def __init__(self, bot): self.bot = bot @@ -17,16 +20,17 @@ def __init__(self, bot): def generate_number(self): return random.randint(0, 100) - @commands.command(name='guess') + @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 {}'.format(ctx.author)) + 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 ctx.content.split(' ')[0] != '.guess': + if ctx.author == self.guesser and self.active == True and COMMAND not in ctx.content.split(' ')[0]: try: guessed_number = int(ctx.content) except ValueError: From 8b7e26e5b02f53d7ae732806c69ad070322e3a6a Mon Sep 17 00:00:00 2001 From: Trevor Corcoran Date: Thu, 16 Sep 2021 21:23:57 -0400 Subject: [PATCH 3/5] Added Jay's patch and fixed auto-quit bug. --- extensions/guess.py | 5 ++++- main.py | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/extensions/guess.py b/extensions/guess.py index 20b7e11..d8d3d45 100644 --- a/extensions/guess.py +++ b/extensions/guess.py @@ -1,9 +1,12 @@ 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): @@ -30,7 +33,7 @@ async def guess(self, ctx): @commands.Cog.listener() async def on_message(self, ctx): - if ctx.author == self.guesser and self.active == True and COMMAND not in ctx.content.split(' ')[0]: + if ctx.author == self.guesser and self.active == True and COMMAND not in ctx.content: try: guessed_number = int(ctx.content) except ValueError: diff --git a/main.py b/main.py index cf5a010..c1842db 100644 --- a/main.py +++ b/main.py @@ -6,16 +6,16 @@ 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 for i in os.listdir('extensions'): From c7f43d53e8c666879fb6cf40e28cbfeb22a146e1 Mon Sep 17 00:00:00 2001 From: sdc_eab Date: Sun, 19 Sep 2021 11:57:37 -0400 Subject: [PATCH 4/5] adding venv to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) 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 From a3dbb849d35b15f689ffa58d419a88bce4ce686e Mon Sep 17 00:00:00 2001 From: sean Date: Thu, 23 Sep 2021 12:42:59 -0400 Subject: [PATCH 5/5] wrote the beginnings of a hangman game. --- extensions/hangman.py | 81 ++++++++++++++++++++++++++++++++ extensions/test.py | 78 ++++++++++++++++++++++++++++++ extensions/words_for_hangman.txt | 1 + 3 files changed, 160 insertions(+) create mode 100644 extensions/hangman.py create mode 100644 extensions/test.py create mode 100644 extensions/words_for_hangman.txt 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/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