Advertisement
breaking-beaker

Cows and Bulls (with troubleshooting comments)

Nov 12th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.18 KB | None | 0 0
  1. # Jonah Monaghan
  2. # 11/9/17
  3. # Cows and Bulls
  4.  
  5. import random
  6. import os
  7. import sys
  8.  
  9. numbers = [0, 0, 0, 0]
  10. eog = False
  11.  
  12. # FNF exception catching and resolution
  13. try:
  14.     highScoreFile = open("Files\\highscore.txt", "r+")  # Attempt to open file
  15. except FileNotFoundError:  # FILE NOT FOUND
  16.     print("Error Code 001: Highscore file was not found.")  # Display error message
  17.     valid = False  # Boolean to approve user input
  18.     while (valid == False):  # Until User gives valid input
  19.         userInput = input(
  20.             "Would you like this to be resolved? [y/n]\n")  # Ask if user would like error resolved
  21.         if (userInput.lower() == "y"):  # yes
  22.             valid = True  # Input is valid
  23.             try:
  24.                 os.mkdir("Files")  # Make a directory
  25.                 highScoreFile = open("Files\\highscore.txt", "w")  # Write the file
  26.                 highScoreFile.close()
  27.                 highScoreFile = open("Files\\highscore.txt", "r+")
  28.                 highScoreFile.write("9999")  # Write the score inside the file
  29.             except FileExistsError:  # Directory Files exists
  30.                 highScoreFile = open("Files\\highscore.txt", "w")  # Write file into Files directory
  31.                 highScoreFile.close()
  32.                 highScoreFile = open("Files\\highscore.txt", "r+")
  33.                 highScoreFile.write("100")  # Write the score inside the file
  34.         elif userInput.lower() == "n":  # User is an idiot
  35.             valid = True  # Set answer to valid
  36.             print("Closing program...")  # Print status
  37.             sys.exit(0)  # Exit
  38.         else:
  39.             print("Error Code 002a: Invalid Input (Input must be y or n)")  # Print error
  40.  
  41. sHighScore = highScoreFile.readline()
  42. highScoreFile.close()
  43.  
  44. try:
  45.     userHighScore = int(sHighScore)  # try to make the highscore file into a string
  46. except ValueError:
  47.     userHighScore = 9999  # If a value error occurs hardcode the highscore
  48.  
  49.  
  50. def GameState(highScore):
  51.     guesses = 0
  52.     while eog == False:
  53.         guesses += 1
  54.         bulls = 0
  55.         cows = 0
  56.         userInput = input("Please enter a 4 digit number: ")
  57.         if len(userInput) != 4:
  58.             print("Error Code 003: Input must be a 4 digit number")
  59.         else:
  60.             uInList = list(userInput)
  61.  
  62.         # conversion
  63.         for x in range(4):
  64.             uInList[x] = int(uInList[x])  # Uneccessary but rubric says that INTEGERS must be compared
  65.  
  66.         # comparisson
  67.         for x in range(4):
  68.             if (numbers[x] == uInList[x]):
  69.                 bulls += 1
  70.             else:
  71.                 # print("Else trigger") [troubleshooting]
  72.                 for y in range(4):
  73.                     if x != y and numbers[x] == uInList[y]:
  74.                         # print("Match found at index ", y, " with number ", numbers[x]) [troubleshooting]
  75.                         cows += 1
  76.  
  77.         print("Bulls: ", bulls)
  78.         print("Cows:", cows)
  79.  
  80.         if (bulls == 4):
  81.             if guesses < highScore:
  82.                 highScoreFile = open("Files\\highscore.txt", "w")  # Open highscore file
  83.                 highScoreFile.write(str(guesses))  # Rewrite highscore file
  84.                 highScoreFile.close()  # Close highscore file
  85.                 highScore = guesses  # Set new highscore
  86.                 print("NEW HIGHSCORE")  # Print congrats
  87.             print("CONGRATS YOU GOT THE NUMBER IN", guesses, " GUESSES! Would you like to play again? [y/n]")
  88.             userInput = input("")  # Not adding additional verification loop but code can be found on High/Low game
  89.             if userInput == "y":
  90.                 numGen(highScore)
  91.             elif userInput == "n":
  92.                 sys.exit(0)
  93.  
  94.  
  95. def numGen(highScore):
  96.     counter = 0
  97.     while counter < 4:
  98.         fail = False
  99.         tempNumber = random.randint(1, 9)
  100.         for x in range(4):
  101.             if tempNumber == numbers[x]:
  102.                 fail = True
  103.                 # print("Fail triggered on ", counter, " with number ", tempNumber) [troubleshooting]
  104.         if fail == False:
  105.             numbers[counter] = tempNumber
  106.             counter += 1
  107.     # print(numbers) [troubleshooting]
  108.     GameState(highScore)
  109.  
  110.  
  111. numGen(userHighScore)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement