Attachment 'GuessWordGame.py'
Download 1 #Author : Gagan Shrestha
2 #Description : I am trying to recreate the Game that I had created long time back using C++ to win a programming contest
3
4 import random #need to generate random no.
5
6 #Describing the rules to the players
7 print """Welcome to this home-made game. I call it "GuessTheWord" !
8 The game, as its name suggests, is to guess a word. You will see the blanks representing the letters in a word. So, cue no. 1 - You know the no. of letters in the word.
9
10 You need to enter one letter at a time as your guess. If the letter exists in the guess word, the blanks will be replaced for each occurrence of the entered letter.
11
12 Press Ctrl+c to quit the game.
13
14 Now lets start the game !!!"""
15
16
17 print 50*'-'
18
19 #function to display list
20 def dispList(list):
21 print "\n"
22 for i in list:
23 print i,
24 print "\n"
25
26 #function to check the presence of character in the string
27 def ifPresent(char,str):
28 return str.count(char)
29
30 #function to find the indices of the character in the list
31 def findIndex(char,str):
32 i=0
33 indexList=[]
34 #print len(str) for debugging
35 while i < len(str):
36 if char==str[i]:
37 indexList.append(i)
38 i=i+1
39 return indexList
40
41 #function to fill the characters in the list
42 def fillList(char,indexList,List):
43 i=0
44 for i in indexList:
45 List[i]=char
46 #print answerList for debugging
47 return List
48
49 #retrieve list of strings for questions from config file called 'questions.txt'
50 strings=[] #this list will hold the words to be guessed from config file
51 file=open('questions.txt','r')
52 for string in file.readlines():
53 strings.append(string.rstrip())
54 file.close()
55
56 #choose the string for question
57 random.shuffle(strings)
58 i=random.randint(0,(len(strings)-1))
59
60 #variable to control the main loop of game
61 loop=1
62
63 #variable to check the count of try
64 count=0
65
66 #need to create list of blanks
67 repeat=len(strings[i])
68 fill=0
69
70 #This list is to create a question
71 question=[]
72
73 #This list is to store the user's guess
74 answer=[]
75
76 #this list will be used to record the player's guess
77 while fill < repeat:
78 #let us fill the answer list with hyphens
79 question.append(strings[i][fill].lower())
80 answer.append('-')
81 fill=fill+1
82
83 #dispList(question) #for debugging
84 #display the empty list
85 print "\n\n"
86 dispList(answer)
87
88 print ""
89 print ""
90
91 #Actual program starts here
92 while loop==1:
93
94 if (answer == question):
95 print '\nCongratulations ! You got it !! You took',count,'attempts to get the right answer !!! \n'
96 break
97
98 #take the input from player
99 try:
100 char=raw_input("\nEnter your guess character: ").lower()
101 count=count+1
102
103 except(EOFError):
104 print '\n\nCharacter you entered is not in the answer. Try again !'
105 continue
106
107 except(KeyboardInterrupt):
108 print '\n\nYou chose to quit the game ! See you later !!!'
109 break
110
111 #check if player entered more than one character and display error if yes
112 if len(char)>1:
113 print '\nEnter only one character at a time !\n'
114 dispList(answer)
115 continue
116
117 #check if input is present in our answer
118 elif ifPresent(char,strings[i]):
119 #print 'Character present' for debugging
120
121 #since it is present call the function to find the index no. on which the entered character is present
122 indexList=findIndex(char,strings[i])
123
124 #print indexList for debugging
125
126 #filling the character in the answer list wherever it is present based on index we found earlier
127 answer=fillList(char,indexList,answer)
128
129 #print answer for debugging
130
131 #display the answer with input filled in
132 dispList(answer)
133
134 #give another chance to player since character entered earlier is not present in our answer
135 else:
136 print '\nCharacter you entered is not in the answer. Try again !'
137 dispList(answer)
138 continue
139
140 #print 'Out of Loop ! phew !!' for debugging
141
142 #End of Programme
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.