letter/word guessing game in python -


background

i'm attempting code basic letter game in python. in game, computer moderator picks word out of list of possible words. each player (computer ai , human) shown series of blanks, 1 each letter of word. each player guesses letter , position, , told 1 of following:

that letter belongs in position (the best outcome) letter in word, not in position letter not in of remaining blank spaces when word has been revealed, player guess letters correctly wins point. computer moderator picks word , starts again. first player 5 points wins game. in basic game, both players share same set of blanks they're filling in, players benefit each other's work.

question

i'm working on computer ai portion right (bottom of code). want select random letter list of letters haven't been guessed. what's best way this?

import random  #set initial values player1points= 0 ai= 0 usercorrectletters= '' aicorrectletters='' wrongplace='' wrongletters='' correctletters = '' notinword = '' endgame = false alreadyguessed = 'a' userguessposition = 0  ###import wordlist, create mask open('/users/jamyn/documents/workspace/lab3/lab3/wordlist.txt') wordlist:     secretword = random.choice(wordlist.readlines()).strip()  print (secretword)  mask = '_'  * len(secretword) in range (len(secretword)):     if secretword[i] in correctletters:         mask = mask[:i] + secretword[i] + mask [i+1:] letter in mask:     print (letter, end='')     print ()  print ()   def addalreadyguessed():     alreadyguessed= usercorrectletters + aicorrectletters + wrongletters + correctletters  def displaygame():     print ('letters in word not in correct location:', wrongplace)     print ('letters not in word:', notinword)     ##asks user guess, assigns input variable def getuserguess(alreadyguessed):       while true:         print ('enter letter')         userguess = input ()         userguess= userguess.lower()         if len(userguess) != 1:             print ('please enter 1 letter')         elif userguess in alreadyguessed:             print ('that letter has been guessed. try again')         elif userguess not in 'abcdefjhijklmnopqrstuvwxyz':             print ('only letters acceptable guesses. try again.')         else:             return userguess  def newgame():     print ('yay. great. want play again? answer yes or no.')     return input().lower().startswith('y')   userturn=true      while userturn == true:     print ('which character place guess. enter number?')     userguessposition = int(input())      slice1 = userguessposition - 1       print (secretword)         ##player types in letter     guess = getuserguess(wrongletters + correctletters)     if guess== (secretword[slice1:userguessposition]):         correctletters = correctletters + guess         print ('you got right! ')         break     elif guess in secretword:             usercorrectletters = usercorrectletters + guess              correctletters = correctletters + guess             print ('that letter in word, not in position')             break     else:             wrongletters = wrongletters + guess             print ('nope. letter not in word')             break  print ('its computers turn')  aiturn=true  while aiturn == true:     aiguess=random.choice('abcdefghijklmnopqrstuvwxyz')     print (aiguess)  

use pythons set, keep set containing 26 letters, , set of ones guessed, , ask elements in large set not in larger set http://docs.python.org/2/library/sets.html ... pull random choice result

allletters = set(list('abcdefghijklmnopqrstuvwxyz')) usedletters = set() # update go availletters = allletters.difference(usedletters) #s - t    new set elements in s not in t 

to print sets out nicely, can do

print sorted(availletters) 

or

print ', '.join(sorted(availletters)) 

to answer followup on adding guesses, here quick example

allletters = set(list('abcdefghijklmnopqrstuvwxyz')) usedletters = set() # update go while( len(usedletters) != len(allletters) ):     guessedletter = raw_input("pick letter")     availletters = allletters.difference(usedletters)     usedletters.update(guessedletter) 

you have 1 list , subtract letters they've been guessed, such as:

allletters = set(list('abcdefghijklmnopqrstuvwxyz')) while( len(usedletters) != len(allletters) ):     guessedletter = raw_input("pick letter")     allletters.difference_update(guessedletter) 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -