def testFunction_getLastAttempt(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.getLastAttempt() == evaluatedCombi, True) evaluatedCombi = EvaluatedCombination(Colorcombination([5, 6, 7, 0]), Evaluation(1, 0, False)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual(attempts.getLastAttempt() == evaluatedCombi, True) evaluatedCombi = EvaluatedCombination(Colorcombination([5, 1, 7, 0]), Evaluation(1, 1, False)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual(attempts.getLastAttempt() == evaluatedCombi, True) evaluatedCombi = EvaluatedCombination(Colorcombination([5, 6, 7, 0]), Evaluation(1, 0, False)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual(attempts.getLastAttempt() == evaluatedCombi, True) evaluatedCombi = EvaluatedCombination(Colorcombination([5, 1, 2, 0]), Evaluation(1, 2, False)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual(attempts.getLastAttempt() == evaluatedCombi, True)
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 __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 testFunction_evaluateCombination(self): sud = Evaluator(Colorcombination([0, 1, 2, 3])) #calculate right colors on the right place evaluations self.assertEqual( sud.evaluateCombination(Colorcombination([0, 1, 2, 3])), Evaluation(0, 4, True)) self.assertEqual( sud.evaluateCombination(Colorcombination([4, 1, 2, 3])), Evaluation(0, 3, False)) self.assertEqual( sud.evaluateCombination(Colorcombination([4, 5, 2, 3])), Evaluation(0, 2, False)) self.assertEqual( sud.evaluateCombination(Colorcombination([4, 5, 6, 3])), Evaluation(0, 1, False)) self.assertEqual( sud.evaluateCombination(Colorcombination([4, 5, 6, 7])), Evaluation(0, 0, False)) #calculate right colors on the wrong place evaluations self.assertEqual( sud.evaluateCombination(Colorcombination([3, 2, 1, 0])), Evaluation(4, 0, False)) self.assertEqual( sud.evaluateCombination(Colorcombination([3, 2, 1, 7])), Evaluation(3, 0, False)) self.assertEqual( sud.evaluateCombination(Colorcombination([3, 2, 6, 7])), Evaluation(2, 0, False)) self.assertEqual( sud.evaluateCombination(Colorcombination([3, 5, 6, 7])), Evaluation(1, 0, False)) self.assertEqual( sud.evaluateCombination(Colorcombination([4, 5, 6, 7])), Evaluation(0, 0, False)) #calculate mixed evaluations self.assertEqual( sud.evaluateCombination(Colorcombination([2, 1, 7, 4])), Evaluation(1, 1, False)) self.assertEqual( sud.evaluateCombination(Colorcombination([2, 1, 0, 4])), Evaluation(2, 1, False)) self.assertEqual( sud.evaluateCombination(Colorcombination([2, 1, 0, 3])), Evaluation(2, 2, False)) self.assertEqual( sud.evaluateCombination(Colorcombination([2, 1, 6, 3])), Evaluation(1, 2, False))
def __validateWithTemporaryMastercombination(self, tmpMasterCombination, colorCombination): tmpEvaluator = Evaluator(tmpMasterCombination) return tmpEvaluator.evaluateCombination(colorCombination)
def testFunction_getCombinationsWithNoRightColor(self): evaluator = Evaluator(Colorcombination([0, 1, 2, 3])) attempts = Attempts() evaluatedCombi = EvaluatedCombination(Colorcombination([5, 6, 7, 0]), Evaluation(1, 0, False)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual(attempts.getCombinationsWithNoRightColor() == [], True) evaluatedCombi = EvaluatedCombination(Colorcombination([4, 5, 6, 7]), Evaluation(0, 0, False)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual( attempts.getCombinationsWithNoRightColor() == [ Colorcombination([4, 5, 6, 7]) ], True) evaluatedCombi = EvaluatedCombination(Colorcombination([5, 1, 7, 0]), Evaluation(1, 1, False)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual( attempts.getCombinationsWithNoRightColor() == [ Colorcombination([4, 5, 6, 7]) ], True) evaluatedCombi = EvaluatedCombination(Colorcombination([5, 6, 7, 0]), Evaluation(1, 0, False)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual( attempts.getCombinationsWithNoRightColor() == [ Colorcombination([4, 5, 6, 7]) ], True) evaluatedCombi = EvaluatedCombination(Colorcombination([0, 1, 2, 5]), Evaluation(0, 1, False)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual( attempts.getCombinationsWithNoRightColor() == [ Colorcombination([4, 5, 6, 7]) ], True) evaluatedCombi = EvaluatedCombination(Colorcombination([4, 6, 5, 7]), Evaluation(0, 0, False)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual( attempts.getCombinationsWithNoRightColor() == [ Colorcombination([4, 5, 6, 7]), Colorcombination([4, 6, 5, 7]) ], True) evaluatedCombi = EvaluatedCombination(Colorcombination([4, 6, 5, 7]), Evaluation(0, 0, False)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual( attempts.getCombinationsWithNoRightColor() == [ Colorcombination([4, 5, 6, 7]), Colorcombination([4, 6, 5, 7]) ], True) evaluatedCombi = EvaluatedCombination(Colorcombination([0, 1, 2, 3]), Evaluation(0, 4, True)) attempts.addEvaluatedCombination(evaluatedCombi) self.assertEqual( attempts.getCombinationsWithNoRightColor() == [ Colorcombination([4, 5, 6, 7]), Colorcombination([4, 6, 5, 7]) ], True)
def setMasterCombination(self, masterCombination): if not self.__gameIsRunning: self.__masterCombination = masterCombination self.__validator.validateCombination(masterCombination) self.__evaluator = Evaluator(masterCombination)