Ejemplo n.º 1
0
    def testError_checkCombination(self):
        sud = Validator(4, 8)
        #numbers and dimension to low
        for startValueOfCombi in range(-1, 4):
            for endValueOfCombi in range(-1, 4):
                combi = list(range(startValueOfCombi, endValueOfCombi))
                with self.assertRaises(ValueError):
                    #print(str(startValueOfCombi) +" "+ str(endValueOfCombi) +" "+ str(combi) +" "+ str(len(combi)))
                    sud.validateCombination(Colorcombination(combi))
        #numbers and dimension to high
        for startValueOfCombi in range(4, 6):
            for endValueOfCombi in range(4, 6):
                combi = list(range(startValueOfCombi, endValueOfCombi))
                with self.assertRaises(ValueError):
                    #print(str(startValueOfCombi) +" "+ str(endValueOfCombi) +" "+ str(combi) +" "+ str(len(combi)))
                    sud.validateCombination(Colorcombination(combi))

        combi = [1, 1, 1, 1]
        with self.assertRaises(ValueError):
            sud.validateCombination(Colorcombination(combi))
        combi = [1, 1, 2, 1]
        with self.assertRaises(ValueError):
            sud.validateCombination(Colorcombination(combi))
        combi = [1, 2, 1, 3]
        with self.assertRaises(ValueError):
            sud.validateCombination(Colorcombination(combi))
Ejemplo n.º 2
0
 def testFunction_checkCombination(self):
     sud = Validator(4, 8)
     #valid combination
     for startValueOfCombi in range(0, 5):
         combi = list(range(startValueOfCombi, startValueOfCombi + 4))
         #print(str(startValueOfCombi) +" "+ str(startValueOfCombi + 4) +" "+ str(combi) +" "+ str(len(combi)))
         sud.validateCombination(Colorcombination(combi))
Ejemplo n.º 3
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)