def testFunction_getCombinationsWithAllRightColor(self): evaluator = Evaluator(Colorcombination([0, 1, 2, 3])) attempts = Attempts() evaluatedCombi = EvaluatedCombination(Colorcombination([5, 6, 7, 0]), Evaluation(1, 0, False)) attempts.addEvaluatedCombination(evaluatedCombi) with self.assertRaises(ValueError): attempts.getCombinationWithAllRightColors() evaluatedCombi = EvaluatedCombination(Colorcombination([0, 1, 2, 3]), Evaluation(1, 3, True)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual( attempts.getCombinationWithAllRightColors() == Colorcombination( [0, 1, 2, 3]), True) evaluatedCombi = EvaluatedCombination(Colorcombination([4, 6, 5, 7]), Evaluation(0, 0, False)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual( attempts.getCombinationWithAllRightColors() == Colorcombination( [0, 1, 2, 3]), True) evaluatedCombi = EvaluatedCombination(Colorcombination([0, 1, 3, 2]), Evaluation(0, 4, True)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual( attempts.getCombinationWithAllRightColors() == Colorcombination( [0, 1, 2, 3]), True) attempts.clearAttempts() evaluatedCombi = EvaluatedCombination(Colorcombination([0, 1, 3, 2]), Evaluation(0, 4, True)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual( attempts.getCombinationWithAllRightColors() == Colorcombination( [0, 1, 3, 2]), True) evaluatedCombi = EvaluatedCombination(Colorcombination([5, 6, 7, 0]), Evaluation(1, 0, False)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual( attempts.getCombinationWithAllRightColors() == Colorcombination( [0, 1, 3, 2]), True) evaluatedCombi = EvaluatedCombination(Colorcombination([0, 1, 2, 3]), Evaluation(1, 3, True)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual( attempts.getCombinationWithAllRightColors() == Colorcombination( [0, 1, 3, 2]), True) evaluatedCombi = EvaluatedCombination(Colorcombination([4, 6, 5, 7]), Evaluation(0, 0, False)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual( attempts.getCombinationWithAllRightColors() == Colorcombination( [0, 1, 3, 2]), True)
def testFunction_addEvaluatedCombination(self): evaluator = Evaluator(Colorcombination([0, 1, 2, 3])) attempts = Attempts() evaluatedCombi = EvaluatedCombination(Colorcombination([4, 5, 6, 7]), Evaluation(0, 0, False)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual(attempts.getNumberOfAttempts() == 1, True) evaluatedCombi = EvaluatedCombination(Colorcombination([5, 6, 7, 0]), Evaluation(1, 0, False)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual(attempts.getNumberOfAttempts() == 2, True) evaluatedCombi = EvaluatedCombination(Colorcombination([5, 1, 7, 0]), Evaluation(1, 1, False)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual(attempts.getNumberOfAttempts() == 3, True) evaluatedCombi = EvaluatedCombination(Colorcombination([5, 6, 7, 0]), Evaluation(1, 0, False)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual(attempts.getNumberOfAttempts() == 4, True) evaluatedCombi = EvaluatedCombination(Colorcombination([5, 1, 2, 0]), Evaluation(1, 2, False)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual(attempts.getNumberOfAttempts() == 5, True) attempts.clearAttempts() self.assertEqual(attempts.getNumberOfAttempts() == 0, True)
def testFunction_checkIfCombinationExist(self): attempts = Attempts() self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([0, 0, 1])), False) attempts.addEvaluatedCombination( EvaluatedCombination(Colorcombination([0, 0, 1]), Evaluation(0, 0, False))) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([0, 0, 1])), True) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([0, 1, 0])), False) attempts.addEvaluatedCombination( EvaluatedCombination(Colorcombination([0, 1, 0]), Evaluation(1, 0, False))) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([0, 1, 0])), True) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([0, 1, 1])), False) attempts.addEvaluatedCombination( EvaluatedCombination(Colorcombination([0, 1, 1]), Evaluation(1, 1, False))) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([0, 1, 1])), True) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([1, 0, 0])), False) attempts.addEvaluatedCombination( EvaluatedCombination(Colorcombination([1, 0, 0]), Evaluation(1, 0, False))) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([1, 0, 0])), True) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([0, 0, 0])), False) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([0, 0, 1])), True) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([0, 1, 0])), True) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([0, 1, 1])), True) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([1, 0, 0])), True) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([1, 0, 1])), False) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([1, 1, 0])), False) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([1, 1, 1])), False) attempts.clearAttempts() attempts.addEvaluatedCombination( EvaluatedCombination(Colorcombination([0, 2, 3, 1]), Evaluation(1, 0, False))) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([0, 2, 3, 1])), True) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([0, 2, 1, 3])), False) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([0, 3, 2, 1])), False) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([0, 3, 1, 2])), False) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([0, 1, 2, 3])), False) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([0, 1, 3, 2])), False) self.assertEqual( attempts.checkIfCombinationExist(Colorcombination([2, 0, 3, 1])), False)
class GameCoordinator(): def __init__(self, lengthOfGuess, numberOfColors, masterCombination, maxNumberOfAttempts): self.__gameIsRunning = False self.__validator = Validator(lengthOfGuess, numberOfColors) self.__masterCombination = masterCombination self.__validator.validateCombination(masterCombination) self.__evaluator = Evaluator(masterCombination) self.attempts = Attempts() self.__maxNumberOfAttempts = maxNumberOfAttempts self.__validator.validateMaxNumberOfAttempts(maxNumberOfAttempts) #self.player = HumanPlayer() #self.player = NPC_random(lengthOfGuess, numberOfColors, self.attempts) #self.player = NPC_hardCoded(lengthOfGuess, numberOfColors, self.attempts) self.player = NPC_csp(lengthOfGuess, numberOfColors, self.attempts) def playGame(self): if not self.__gameIsRunning: self.__gameStart() while self.__gameIsRunning: if self.attempts.getNumberOfAttempts( ) < self.__maxNumberOfAttempts: self.__playRound() else: self.__gameLost() return FinalScore( False, self.attempts.getNumberOfAttempts(), self.__validator.validateForNoObviousErrors( self.attempts)) return FinalScore( True, self.attempts.getNumberOfAttempts(), self.__validator.validateForNoObviousErrors(self.attempts)) def __gameStart(self): self.attempts.clearAttempts() self.__gameIsRunning = True print("\nStarted a new MindMaster Game.") print("Setup is " + str(self.__validator) + ", you have a maximum number of " + str(self.__maxNumberOfAttempts) + " attempts.") self.player.introduceYourself() def __endGame(self): self.__gameIsRunning = False self.player.debriefing( self.__validator.validateForNoObviousErrors(self.attempts)) def __playRound(self): gameIsFinished = self.__getAndProcessCombination() if gameIsFinished: self.__gameWon() else: print( str(self.attempts.getLastAttempt()) + " {" + str(self.attempts.getNumberOfAttempts()) + "/" + str(self.__maxNumberOfAttempts) + "}") def __gameLost(self): self.__endGame() print("\nYou lost the game.") print("You have reached the maximum number of " + str(self.attempts.getNumberOfAttempts()) + " tries.") print("You best attempt was " + str(self.attempts.getBestAttempt()) + ".") def __gameWon(self): self.__endGame() try: self.__validator.validateAttempts(self.attempts) except ValueError: print("\n Game has ended, but there was an error.") print("\nYou won the game.") print("The MasterCombination was: " + str(self.__masterCombination) + ". You needed " + str(self.attempts.getNumberOfAttempts()) + " of " + str(self.__maxNumberOfAttempts) + " tries.") if self.__validator.validateForNoObviousErrors(self.attempts): print("\nBut you could have done better, believe me ..") def __getAndProcessCombination(self): newCombination = self.__getNewUserCombination() evaluation = self.__evaluator.evaluateCombination(newCombination) self.attempts.addEvaluatedCombination( EvaluatedCombination(newCombination, evaluation)) return evaluation.gameFinished def __getNewUserCombination(self): while (True): try: userCmbi = Colorcombination(self.player.readInputIn()) self.__validator.validateCombination(userCmbi) break except ValueError as e: print( str(e) + ". Combination not valid, do it again. Just write the color values seperated by a whitespace and hit enter." ) return userCmbi def setMasterCombination(self, masterCombination): if not self.__gameIsRunning: self.__masterCombination = masterCombination self.__validator.validateCombination(masterCombination) self.__evaluator = Evaluator(masterCombination)
def testError_validateForNoObviousRrrors(self): sud = Validator(4, 8) attempts = Attempts() evaluatedCombi = EvaluatedCombination(Colorcombination([4, 5, 6, 7]), Evaluation(0, 0, False)) attempts.addEvaluatedCombination(evaluatedCombi) debriefing = sud.validateForNoObviousErrors(attempts) if debriefing: self.fail() evaluatedCombi = EvaluatedCombination(Colorcombination([5, 6, 7, 0]), Evaluation(1, 0, False)) attempts.addEvaluatedCombination(evaluatedCombi) debriefing = sud.validateForNoObviousErrors(attempts) if not debriefing: self.fail() attempts.clearAttempts() evaluatedCombi = EvaluatedCombination(Colorcombination([5, 1, 7, 0]), Evaluation(1, 1, False)) attempts.addEvaluatedCombination(evaluatedCombi) debriefing = sud.validateForNoObviousErrors(attempts) if debriefing: self.fail() evaluatedCombi = EvaluatedCombination(Colorcombination([5, 6, 7, 0]), Evaluation(1, 0, False)) attempts.addEvaluatedCombination(evaluatedCombi) debriefing = sud.validateForNoObviousErrors(attempts) if debriefing: self.fail() evaluatedCombi = EvaluatedCombination(Colorcombination([5, 1, 2, 0]), Evaluation(1, 2, False)) attempts.addEvaluatedCombination(evaluatedCombi) debriefing = sud.validateForNoObviousErrors(attempts) if debriefing: self.fail() evaluatedCombi = EvaluatedCombination(Colorcombination([4, 5, 6, 7]), Evaluation(0, 0, False)) attempts.addEvaluatedCombination(evaluatedCombi) debriefing = sud.validateForNoObviousErrors(attempts) if debriefing: self.fail() evaluatedCombi = EvaluatedCombination(Colorcombination([1, 2, 5, 0]), Evaluation(3, 0, False)) attempts.addEvaluatedCombination(evaluatedCombi) debriefing = sud.validateForNoObviousErrors(attempts) if not debriefing: self.fail() attempts.clearAttempts() evaluatedCombi = EvaluatedCombination(Colorcombination([5, 1, 7, 0]), Evaluation(1, 1, False)) attempts.addEvaluatedCombination(evaluatedCombi) debriefing = sud.validateForNoObviousErrors(attempts) if debriefing: self.fail() evaluatedCombi = EvaluatedCombination(Colorcombination([1, 3, 2, 0]), Evaluation(3, 1, False)) attempts.addEvaluatedCombination(evaluatedCombi) debriefing = sud.validateForNoObviousErrors(attempts) if debriefing: self.fail() evaluatedCombi = EvaluatedCombination(Colorcombination([5, 1, 2, 0]), Evaluation(1, 2, False)) attempts.addEvaluatedCombination(evaluatedCombi) debriefing = sud.validateForNoObviousErrors(attempts) if not debriefing: self.fail()