Attachment 'GuessTheWord.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 #-----------------------------------------------------------------------------------------------------
  50 #retrieve list of strings for questions from config file called 'questions.txt'
  51 strings={}          #this list will hold the words to be guessed from config file
  52 check=1
  53 listq=[]
  54 listc=[]
  55 
  56 file = open('questions.txt','r')
  57 for string in file.readlines():
  58     if check%2!=0:
  59             listq.append(string.rstrip())
  60     elif check%2==0:
  61             listc.append(string.rstrip())
  62     check+=1
  63 file.close()
  64 
  65 #create dictionary for mapping question and cue    
  66 for key,value in zip(listq,listc):
  67     strings[key]=value
  68 
  69 #--------------------------------------------------------------------------------------------------------
  70 gameover='N'
  71 
  72 while gameover == 'N':
  73 
  74     #choose the string for question
  75     random.shuffle(listq)
  76     i=random.randint(0,(len(listq)-1))    
  77 
  78     #variable to control the main loop of game
  79     loop=1
  80 
  81     #variable to check the count of try
  82     count=0
  83 
  84     #need to create list of blanks
  85     repeat=len(listq[i])
  86     cue=strings[listq[i]]
  87     fill=0
  88 
  89     #This list is to create a question
  90     question=[]
  91 
  92     #This list is to store the user's guess
  93     answer=[]
  94 
  95     #this list will be used to record the player's guess
  96     while fill < repeat:
  97         #let us fill the answer list with hyphens
  98         question.append(listq[i][fill].lower())
  99         answer.append('-')
 100         fill=fill+1
 101 
 102     #dispList(question)         #for debugging
 103     #display the empty list
 104     print " "
 105     print 'Cue # 2 :', cue
 106     print "\n\n"    
 107     dispList(answer)
 108 
 109     print ""
 110     print ""
 111 
 112     #Actual program starts here
 113     while loop==1:
 114 
 115         if (answer == question):
 116             print '\nCongratulations ! You got it !! You took',count,'attempts to get the right answer !!!  \n'
 117             break
 118 
 119         #take the input from player
 120         try:
 121             char=raw_input("\nEnter your guess character: ").lower()
 122             count=count+1
 123             
 124         except(EOFError):
 125             print '\n\nCharacter you entered is not in the answer. Try again !'
 126             continue
 127 
 128         except(KeyboardInterrupt):
 129             print '\n\nYou chose to quit the game ! See you later !!!'
 130             break
 131 
 132         #check if player entered more than one character and display error if yes
 133         if len(char)>1:
 134             print '\nEnter only one character at a time !\n'
 135             dispList(answer)
 136             continue    
 137             
 138         #check if input is present in our answer
 139         elif ifPresent(char,listq[i]):
 140             #print 'Character present'      for debugging
 141 
 142             #since it is present call the function to find the index no. on which the entered character is present    
 143             indexList=findIndex(char,listq[i])
 144 
 145             #print indexList                for debugging
 146 
 147             #filling the character in the answer list wherever it is present based on index we found earlier
 148             answer=fillList(char,indexList,answer)
 149 
 150             #print answer                   for debugging
 151 
 152             #display the answer with input filled in
 153             dispList(answer)    
 154 
 155         #give another chance to player since character entered earlier is not present in our answer                        
 156         else:
 157             print '\nCharacter you entered is not in the answer. Try again !'
 158             dispList(answer)
 159             continue       
 160 
 161     #print 'Out of Loop ! phew !!'          for debugging
 162 
 163     gameover = raw_input("Do you want to END the game (Y/N) ?  ").upper()
 164 
 165 print 'Bye.. see you again !!!'
 166 
 167 #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.

Unable to edit the page? See the FrontPage for instructions.