-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhand_cricket_voicebased.py
193 lines (184 loc) · 6.87 KB
/
hand_cricket_voicebased.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import os.path
import nltk
from nltk.corpus import wordnet
import os
import playsound
import speech_recognition as sr
from gtts import gTTS
import random
def synonyms(word): #to get synonyms of a word
synms = []
for syn in wordnet.synsets(word):
for l in syn.lemmas():
synms.append(l.name())
ans = list(set(synms))
lans=len(ans)
for i in range(lans):ans[i]=ans[i].replace("_"," ")
return ans
def play_cricket(turn,num):
if turn == "bat":
plysco = []
afisco = []
speak("you are batting now.")
for i in range(6):
speak("your turn..")
plywrd = get_audio()
while plywrd not in num:
speak("please choose number between 1 and 6")
plywrd = get_audio()
#plywrd = input("user : ").lower()
afino = random.randint(1,6) #cricket_game random choice.
plyno = num.get(plywrd)
speak("my choice is " + str(afino))
if afino == plyno:
speak("you are out..")
break
else:
plysco.append(plyno)
speak("you are bowling now.")
for i in range(6):
speak("your turn..")
plywrd = get_audio()
#plywrd = input("user : ").lower()
while plywrd not in num:
speak("please choose number between 1 and 6")
plywrd = get_audio()
#plywrd = input("user : ").lower()
afino = random.randint(1,6)
plyno = num.get(plywrd)
speak("my choice is " + str(afino))
if afino == plyno:
speak("i lost my wicket.")
break
else:
afisco.append(afino)
finply = sum(plysco)
finafi = sum(afisco)
speak("your score is " + str(finply) + " and my score is " + str(finafi))
if finply > finafi:
speak("Hurry! You won.")
elif finply < finafi:
speak("You have lost. Better luck next time.")
else:
speak("this is a tie.")
else:
plysco = []
afisco = []
speak("you are bowling now.")
for i in range(6):
speak("your turn..")
plywrd = get_audio()
#plywrd = input("user : ").lower()
while plywrd not in num:
speak("please choose number between 1 and 6")
plywrd = get_audio()
#plywrd = input("user : ").lower()
afino = random.randint(1,6)
plyno = num.get(plywrd)
speak("my choice is " + str(afino))
if afino == plyno:
speak("i lost my wicket.")
break
else:
afisco.append(afino)
speak("you are batting now.")
for i in range(6):
speak("your turn..")
plywrd = get_audio()
#plywrd = input("user : ").lower()
while plywrd not in num:
speak("please choose number between 1 and 6")
plywrd = get_audio()
#plywrd = input("user : ").lower()
afino = random.randint(1,6)
plyno = num.get(plywrd)
speak("my choice is " + str(afino))
if afino == plyno:
speak("you are out..")
break
else:
plysco.append(plyno)
finply = sum(plysco)
finafi = sum(afisco)
speak("your score is " + str(finply) + " and my score is " + str(finafi))
if finply > finafi:
speak("Hurry! You won.")
elif finply < finafi:
speak("You have lost. Better luck next time.")
else:
speak("this is a tie.")
def hand_cricket():
speak("would you like to know rules and basic tutorials of game?")
ans = get_audio()
#ans = input("user : ").lower()
ans = ans.split(" ")
temp = 0
for ech in ans:
if ech in ["yes", "would", "will","like"]+synonyms("ok"):
temp = 1
break
if temp == 1:
text = "the game is very simple. we have one over(6 balls). you choose one number and i choose one in between 1 and 6(including both). the batsman will get the score of the number they commit and gets out if bowler number equals batsman. be carefull batsman have only one wicket(life). my number is genune and made from predefined random function. lets have fun."
else:
text = "lets get into the game."
speak(text)
rep = "yes"
while "yes" in rep:
num = {"one":1, "won":1, "two":2, "tu":2, "to":2, "too":2, "three":3, "free":3, "four":4, "five":5, "six":6, "sex":6, "1":1, "2":2, "3":3, "4":4, "5":5, "6":6}
tsword = {1:"heads", 2:"tails"}
choose = {1:"bat", 2:"bowl"}
speak("heads or tails?")
toss = get_audio()
#toss = input("user : ").lower()
if "head" in toss or "hack" in toss:
plytoss = 1
afitoss = 2
else:
plytoss = 2
afitoss = 1
tosswin = random.randint(1,2)
if tosswin == plytoss:
speak("its " + tsword.get(plytoss) + " what you choose bat or bowl?")
resp = get_audio()
#resp = input("user : ").lower()
if "bat" in resp:
plytrn = "bat"
afitrn = "bowl"
else:
plytrn = "bowl"
afitrn = "bat"
else:
afitrn = choose.get(random.randint(1,2))
plytrn = "bat" if afitrn == "bowl" else "bowl"
speak("its " + tsword.get(afitoss) + " i choose to " + afitrn)
play_cricket(plytrn,num)
speak("say yes to play one more game and no for exit.")
rep = get_audio()
#rep = input("user : ").lower()
def speak(txtop):
tts=gTTS(text=txtop, lang="en")
filename = "voice.mp3"
tts.save(filename)
txtop = txtop.lower()
print("Cricket_Game : " + txtop)
playsound.playsound(filename)
os.remove(filename)
def get_audio():
num = {"one":1, "won":1, "two":2, "tu":2, "to":2, "too":2, "three":3, "free":3, "four":4, "five":5, "six":6, "sex":6, "1":1, "2":2, "3":3, "4":4, "5":5, "6":6}
r = sr.Recognizer()
with sr.Microphone() as Source:
print("listening...")
audio = r.listen(Source)
said=""
try:
said = r.recognize_google(audio)
said = said.lower()
if said in num:
print("User : " + str(num.get(said)))
else:
print("User : " + said)
except Exception as e:
print("No audio recognized ...")
return said
speak("Welcome to voice based hand cricket game.")
hand_cricket()