示例#1
0
 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)
示例#2
0
    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)
示例#3
0
    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)
示例#4
0
    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)
示例#5
0
 def __validateWithTemporaryMastercombination(self, tmpMasterCombination,
                                              colorCombination):
     tmpEvaluator = Evaluator(tmpMasterCombination)
     return tmpEvaluator.evaluateCombination(colorCombination)
示例#6
0
    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)
示例#7
0
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)
示例#8
0
 def setMasterCombination(self, masterCombination):
     if not self.__gameIsRunning:
         self.__masterCombination = masterCombination
         self.__validator.validateCombination(masterCombination)
         self.__evaluator = Evaluator(masterCombination)
示例#9
0
    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))