anonymous
  • anonymous
I've been working on assignement 5 for a while, the ghost program, and would like to see someone's working implementation just to check I am complicating things too much.
MIT 6.00 Intro Computer Science (OCW)
schrodinger
  • schrodinger
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this
and thousands of other questions

anonymous
  • anonymous
This is an older solution, so it works but I would probably do it differently if I were to write it again today. Feel free to ask about parts of it and I'll try to explain what I was thinking, in the unlikely event that I can even tell what that was. # Problem Set 5: Ghost # Name: # Collaborators: # Time: # from string import * import random # ----------------------------------- # Helper code # (you don't need to understand this helper code) import string WORDLIST_FILENAME = "words.txt" def load_words(): """ Returns a list of valid words. Words are strings of lowercase letters. Depending on the size of the word list, this function may take a while to finish. """ print "Loading word list from file..." # inFile: file inFile = open(WORDLIST_FILENAME, 'r', 0) # wordlist: list of strings wordlist = [] for line in inFile: wordlist.append(line.strip().lower()) print " ", len(wordlist), "words loaded." return wordlist def get_frequency_dict(sequence): """ Returns a dictionary where the keys are elements of the sequence and the values are integer counts, for the number of times that an element is repeated in the sequence. sequence: string or list return: dictionary """ # freqs: dictionary (element_type -> int) freq = {} for x in sequence: freq[x] = freq.get(x,0) + 1 return freq # (end of helper code) # ----------------------------------- # Actually load the dictionary of words and point to it with # the wordlist variable so that it can be accessed from anywhere # in the program. wordlist = load_words() wordliststring = '' for item in wordlist: wordliststring += item+' ' def play_ghost(): '''shell function to assign players ==[] the first time''' players = [] return ghost_game(players) def ghost_game(players): currentAnswer = get_input('Would you like to play a game? Y/N: ') while currentAnswer !='Y' and currentAnswer!='N' and currentAnswer !='y' and currentAnswer !='n': currentAnswer = get_input('Please enter Y or N: ') currentAnswer = currentAnswer.lower() if currentAnswer == 'n': return if players ==[]: numOfPlayers = raw_input('How many players: ') while check_int(numOfPlayers) == False: numOfPlayers = raw_input('Please enter number of players as an integer: ') players = num_players(int(numOfPlayers)) else: currentAnswer = get_input('Would you like to continue with the same players? Y/N: ') while currentAnswer !='Y' and currentAnswer!='N' and currentAnswer !='y' and currentAnswer !='n': currentAnswer = get_input('Please enter Y or N: ') currentAnswer == currentAnswer.lower() if currentAnswer =='y': players = players if currentAnswer =='n': players = num_players(int(raw_input('How many players: '))) players = play_word(wordlist,wordliststring,players) return ghost_game(players) def num_players(players): '''returns a list with a number of elements (each element==0) equal to the integer it gets''' listPlayers = [] for x in range (0,players): listPlayers.append(0) return listPlayers def play_word (wordlist,wordlistString,players): '''returns a tuple of (intPlayerXScore,) after playing word''' word = '' currentAnswer = '' nextPlayer = 0 while nextPlayer < (len(players)): currentPlayer = nextPlayer+1 playersIndexNum = nextPlayer currentAnswer = get_input('Player ' + str(currentPlayer) + ' please enter a letter: ') while is_letter(currentAnswer) == False: currentAnswer = get_input('Please enter a valid letter: ') currentAnswer = currentAnswer.lower() word += currentAnswer if check_current_string(word, wordlist, wordliststring) == False: players[nextPlayer]+=1 print 'Player ' + str(currentPlayer) + ' loses!' for player in range (len(players)): print 'Player ' +str(player +1) + ' score: ' +str(players[player]) return players if nextPlayer == len(players)-1: nextPlayer = 0 else: nextPlayer += 1 print 'The current fragment is ' + word def get_input(message): '''returns a string using message as prompt''' Answer = str(raw_input(message)) return Answer def is_letter (letter): '''returns bool, checks that letter is a single letter ''' letter = str(letter) if len(letter) !=1: return False if letter.isalpha() == False: return False return True def check_current_string(string, wordlist, wordListString): '''returns bool, checks that a string is part but not all of a complete string > 3 letters''' if count(wordListString, ' '+string) == 0: return False if len(string) > 3: if count(wordlist, string) > 0: return False return True def check_int (number): number = str(number) return number.isdigit()
anonymous
  • anonymous
Great, thanks. So yes, it looks like I'm complicating this a lot... I think I misunderstood the rules of the game... Also having the wordlist in a string like you seems to make it much easier to find what you're looking for.
anonymous
  • anonymous
Just one more question: shouldn't check_current_string() return false only when the string longer than 3 is also a full word (ie not a word fragment) ?

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
Wow. that had me really confused for a while. What happens is that if count(wordListString, ' '+string) == 0: return False checks to see whether the current string is PART of a valid word by returning False UNLESS it can find an instance in wordListString that starts with a space (because otherwise it would count strings it found in the middle of the word as valid) NEXT if len(string) > 3: if count(wordlist, string) > 0: return False checks to see that it is not a COMPLETE word. I think the confusion is in this step, and specifically the fact that it's counting occurrences of the current word in wordlist, NOT wordListString. wordlist is a list of strings, so in order to find the current word in wordlist, it has to EXACTLY match one of the string elements in wordlist.
anonymous
  • anonymous
For instance, the commands x = ['stopped','stopping'] count(x, 'stop') will return 0
anonymous
  • anonymous
So what you're saying is that check_current_string SHOULD return false on a string that is a valid word AND is longer than 3 letters, whether or not it is a sub-word (or word fragment). For exmaple, consider the word "putt" A valid word, but also a valid word fragment ("putty", "putting", etc) In that case, your function returns false, but should it really ? That is, in essence my question. As you can see, it's more about the actual rules of the game than the implementation. It's pretty hard to explain, though. So, if the currrent word fragment is "put" and I type "T" do I lose or do we keep playing ??
anonymous
  • anonymous
If that happens, you lose. From the assignment's list of losing conditions: Forming a word longer than 3 letters ("PEA" is ok, but "PEAR" is not) "PEARS" is also a word, but as soon as someone has completed "PEAR," the game is over.
anonymous
  • anonymous
Which raises a question about the computer version of the game--from what I've read, bluffing is a big part of playing the game live, with the rule that if you think someone has completed a word, or if you think they've made a fragment that can't be a word, you can 'call', and if you're wrong, you lose. It might be a good idea from the standpoint of the computer implementation to NOT automatically check at each input, and instead give the players an option to 'call' instead of entering a letter, which would initiate a word check and one player would lose as a result.
anonymous
  • anonymous
yes, that could be a worthwile addition. Anyway, thanks for your help clearing up that matter!

Looking for something else?

Not the answer you are looking for? Search for more explanations.