Created
June 11, 2015 20:22
-
-
Save chris373/92702b41d4d081bed915 to your computer and use it in GitHub Desktop.
FalloutHackingGame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| # read in words from the text file | |
| f = open('C:\Users\Chris\Downloads\words.txt') | |
| words = f.read() | |
| f.close() | |
| # get a list of words by splitting at each new line | |
| # sort by length | |
| words = words.split('\n') | |
| words.sort(key = len) | |
| # sort the words into seperate lists based on length | |
| # first get the lenght of the longest word (last in the sorted list) | |
| lastWordInd = len(words) - 1 | |
| LongestWordLength = len( words[ lastWordInd ] ) | |
| # create a list, that contains a list for each wordlength | |
| # and one extra list (ex. Three letter words will need to go in the | |
| # fourth list to use 3 as it's index) | |
| wordsByLength = [] | |
| for i in range(0, LongestWordLength +1) : | |
| wordsByLength.append([]) | |
| # place each word in the list | |
| for word in words : | |
| wordsByLength[ len(word) ].append(word) | |
| # start the game loop | |
| playing = True | |
| while playing : | |
| difficulty = raw_input('Enter a difficulty level (1-5): ') | |
| difficulty = int(difficulty) | |
| # the game is supposed to show 5-15 words of 4-15 characters in length | |
| # I simply map the difficulty to these values | |
| # ex difficulty of 3 : words: 5 + (3/5)10 , length: 4 + (3/5)11 | |
| numberOfWords = 5 + int((difficulty/5.0)*10) | |
| Length = 4 + int((difficulty/5.0)*11) | |
| # create the list of words for the game | |
| gameList = [] | |
| q = 0 | |
| while q < numberOfWords : | |
| lengthAtIndex = len( wordsByLength[ Length ] ) -1 | |
| rand = random.randint(0, lengthAtIndex) | |
| newWord = wordsByLength[ Length ][ rand ] | |
| print newWord | |
| gameList.append( newWord ) | |
| q += 1 | |
| # pick the winning word | |
| winner = gameList[random.randint(0, len(gameList)-1) ] | |
| # give the user 4 chances | |
| for i in range(0, 4) : | |
| guess = raw_input('submit your guess ') | |
| if guess == winner : | |
| print 'YOU WIN!' | |
| break | |
| else : | |
| matching = 0 | |
| for j in range(0, Length) : | |
| if guess[j] == winner[j] : | |
| matching += 1 | |
| print matching, '/',Length, ' correct. ', 4-(i+1), ' guesses remaining ' | |
| playing = raw_input('play again? y/n') == 'y' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment