-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
134 lines (100 loc) · 3.94 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#! usr/bin/python3
import discord
import os
import random
from discord.ext import commands
#####################################################################
"""the basic commands to run the bot on discord"""
client = discord.Client()
client=commands.Bot(command_prefix='m.')#setting a prefix for the bot.In this case "m." is the prefix
with open('token.txt') as f:
""" using a text file to store the discord bot token"""
token = f.read()
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#######################################################################
@client.command()
async def hangman(ctx, *, Mode=None):
'''using m.hangman as the command to run the game'''
global WORDLIST_FILENAME
global secretWord
if Mode == None:
await ctx.send("Welcome to HangMan!! ")
await ctx.send("select a topic \n Movies \n Games \n Animals \n Use m.hangman <topic>")
return
if Mode == "Movies":
WORDLIST_FILENAME = "movies.txt"
if Mode == "Games":
WORDLIST_FILENAME = "games.txt"
if Mode == "Animals":
WORDLIST_FILENAME = "animals.txt"
def loadwords():#this function allots a secret word from the text file chosen by the user
inFile = open(WORDLIST_FILENAME, 'r')
line = inFile.readline()
wordlist = line.split()
secretWord = random.choice(wordlist)
return secretWord
secretWord = loadwords()
global mistakeMade
global lettersGuessed
print(secretWord)
"""making variables global to use it in other functions as well"""
mistakeMade=0
lettersGuessed=[]
await ctx.send("I am thinking of a word that is "+ str(len(secretWord)) + " letters long \n Use m.guess <letter> to guess the letters of the word")
@client.command()
async def guess(ctx, *, word=None):
global secretWord
global mistakeMade
if word == None:
await ctx.send("Enter a letter to guess")
return
def isWordGuessed(secretWord, lettersGuessed):#this funtion is called when the user guesses the correct letter
c=0
for i in lettersGuessed:
if i in secretWord:
c+=1
if c==len(secretWord):
return True
else:
return False
def getGuessedWord(secretWord, lettersGuessed):#forms the overall word based on the users letters guesses and the missing letters
s=[]
for i in secretWord:
if i in lettersGuessed:
s.append(i)
ans=''
for i in secretWord:
if i in s:
ans+=i
else:
ans+='\_ '
return ans
global lettersGuessed
while 10 - mistakeMade >0:
guess= word
if guess in lettersGuessed:
await ctx.send("You already guessed that letter, try again")
await ctx.send(getGuessedWord(secretWord,lettersGuessed))
if guess in secretWord and guess not in lettersGuessed:
lettersGuessed.append(guess)
await ctx.send("wow lucky guess, u got it right")
await ctx.send(getGuessedWord(secretWord,lettersGuessed))
if guess not in secretWord and guess not in lettersGuessed:
lettersGuessed.append(guess)
mistakeMade +=1
await ctx.send("wrong letter , choose again")
await ctx.send(getGuessedWord(secretWord,lettersGuessed))
if isWordGuessed(secretWord,lettersGuessed):
await ctx.send("\~\~\~\~\~\~")
await ctx.send("YOU WON!!!!!!!")
break
await ctx.send("U Have " + str(10-mistakeMade) + " guesses left" )
if 10-mistakeMade == 0:
await ctx.send("Youve ran out of guesses,gg ")
await ctx.send("the word was " + str(secretWord))
break
break
#######################################################################
client.run(token)