Skip to content

Hangman #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ config.yml

# Cache folder
__pycache__
.venv
68 changes: 68 additions & 0 deletions extensions/guess.py
Original file line number Diff line number Diff line change
@@ -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))
81 changes: 81 additions & 0 deletions extensions/hangman.py
Original file line number Diff line number Diff line change
@@ -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))
File renamed without changes.
78 changes: 78 additions & 0 deletions extensions/test.py
Original file line number Diff line number Diff line change
@@ -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())
1 change: 1 addition & 0 deletions extensions/words_for_hangman.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
any|words|will|do|firetruck
45 changes: 0 additions & 45 deletions guess.py

This file was deleted.

12 changes: 7 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down