Guess the combination, pardon, Bulls and cows
I have had plenty of time today, I was on leave for some reason. Nevermind, as I have stayed on the computer for the better part of the day, eventually the idea arose to re-code a long-forgotten Guess the combination game, or Mastermind, or Bulls and cows but I'm affraid that name someone copyrighted, so let's stay with Guess the combination. Anyone who learned BASIC eons ago on the old-school minicomputers of the 1980s passed that algorithm with tons of IF-GOTO branches from the main game loop. Well, I didn't touch any coding for years and got that idea to have some fun recreating the game from scratch, not using any internet help, besides a Python 3 document two times. When I got stuck parsing user input, you know, it became a cool one - more basic, less snakeed. And I'm playing a boring game and having great fun tonight. Cheers :-)
It wouldn't be a complete "boasting" in full LinkedIn style without a source and some screenshots.
############################################################
# GuessCombination Game
# Progammed by Ivan D. Pavi?evi?
# Combination of 4 numbers and you hae 10 attempt to solve.
# GNU GPL v3
# 2024-03-20, Belgrade, Serbia
############################################################
import random
import sys
info = """
\t::=:: Guess Combination Game ::=::
\tYou have ten attempts to solve combination of four single digit
\tnumbers. Enter elements 0 .. 9 with spaces in-between.
\tFor example 1 2 3 4, but not 1234, that throws an exception :-).
\tUse keyboard interrupt (Ctrl-C) to break the game at any point.
\t~~~~~~~~~~ Good luck! ~~~~~~~~~~
=================================================================================
"""
class GuessCombination:
def __init__(self):
self.attempts = 10
self.combination = []
self.combinationSorted = []
self.guess = []
def makeCombination(self):
for i in range(0,4):
self.combination.append(random.choice(range(0,10)))
def myCombination(self, combination = []):
self.guess = combination
def compareResults(self):
guessed = 0
placed = 0
for i in range(0, 4):
if self.combination[i] == self.guess[i]:
placed += 1
for i in range(0, 4):
if self.combination[i] in self.guess:
guessed += 1
self.guess.pop(self.guess.index(self.combination[i]))
print("Guessed {} and placed {}.".format(guessed, placed))
if placed == 4:
self.Win()
def Win(self):
print("\n===== YOU WIN =====\n")
sys.exit(0)
def gameLoop(self):
combination = []
while self.attempts > 0:
print("\n===== ATTEMPT NUMBER {} =====\n".format(11 - self.attempts))
self.attempts -= 1
try:
combination = list(map(int, input("\nEnter your four number combination (0 .. 9): ").strip().split()))[:4]
self.myCombination(combination)
self.compareResults()
except KeyboardInterrupt:
print("\nYou breaks the game! Better luck next time.\n")
sys.exit(1)
except SystemExit:
sys.exit(0)
except:
print("An error occured. You must enter combination with blank spaces between!")
self.attempts +=1
print("\n=====BETTER LUCK NEXT TIME GAME OVER =====\n")
print("The combination was: {}".format(self.combination))
if __name__ == '__main__':
print(info)
gc = GuessCombination()
gc.makeCombination()
gc.gameLoop()