예제 #1
0
    def test_CustomException(self):
        #Arrange
        logFileName = "TestLog.log"
        logFileMode = "w"
        logName = "TestLogFile"
        logLevel = "ERROR"
        myExFound = False

        #Act
        MyLogger.addFileLogger(logFileName=logFileName,
                               logFileMode=logFileMode,
                               logName=logName,
                               logLevel=logLevel)
        myLoggerFile = MyLogger.myLog["File"].handlers[0].baseFilename

        try:
            raise InvalidParameterException("testParam", "testValue",
                                            "^[0-9]$")
        except InvalidParameterException as IPEx:
            MyLogger.logException(["File"], IPEx)

        #Assert
        with open(myLoggerFile) as file:
            for line in file:
                if "InvalidParameterException" in line:
                    myExFound = True
                    break

        self.assertTrue(myExFound)
예제 #2
0
    def __init__(self,
                 boardSize=None,
                 gameMode=None,
                 difficulty=None,
                 skipPlay=False):
        """
        Create TicTacToe game class. Optional to pass in boardSize, gameMode (1 to play vs CPU, 2 to play vs Humans)
        and difficulty 1 (Easy), 2 (Medium), 3 (Hard). Skip parameters to use user prompt instead
        """
        print("\nWelcome to Tic Tac Toe!")

        # Get and validate game parameters from user or init
        try:
            self.boardSize = self.getBoardSize(boardSize, '^[3|5|7|9]$')
            self.numPlayers = self.getGameMode(gameMode, '^[1-2]$')
            self.difficulty = self.getDifficulty(difficulty, '^[1-3]$')
        except InvalidParameterException as IPEx:
            # Log and Quit
            MyLogger.logException(["DB"], IPEx)
            print(IPEx)
            raise

        self._initGame()

        # For testing dont want to start user prompts so skip
        if not skipPlay:
            self._playGame()
예제 #3
0
    def __init__(self, gameMode=None, skipPlay=False):
        """
        Create Othello game class. Optional to pass in gameMode (1 to play vs CPU, 2 to play vs Humans), dont't pass for user prompt
        """
        print("\nWelcome to Othello!")

        # Get and validate game parameters from user or init
        try:
            self.numPlayers = self.getGameMode(gameMode, '^[1-2]$')
        except InvalidParameterException as IPEx:
            # Log and Quit
            MyLogger.logException(["DB"], IPEx)
            print(IPEx)
            raise

        self._initGame()

        # For testing dont want to start user prompts so skip
        if not skipPlay:
            self._playGame()
예제 #4
0
    def ShowGameMenu():
        """
        Display Game Menu with list of registered games
        """
        choiceOutput = "Type the number of the game you would like to play or Q to Quit\n"
        for idx, game in enumerate(GameFactory.games):
            choice = f"{str(idx+1)}) {game}\n"
            choiceOutput += choice
        choiceOutput += "Q) Quit\n"

        validGame = False
        while (not (validGame)):
            game = input(choiceOutput)

            if game == 'Q' or game == 'q':
                break
            elif (not (game.isdigit())):
                print("Choice is not a number")
            elif int(game) > len(GameFactory.games) or int(game) < 1:
                print("Invalid choice")
            else:
                try:
                    gameClass = GameFactory.getGame(int(game) - 1)
                    eval(gameClass)()
                except AssertionError as AssertEx:
                    MyLogger.logException(["DB"], AssertEx)
                    print(AssertEx)
                except NameError as NameEx:
                    MyLogger.logException(["DB"], NameEx)
                    print(NameEx)
                except Exception as Ex:
                    strOutputError = "Could not load game. Check the registered game name matches the game's class name and the module has been "
                    strOutputError += "imported as \'from Package.Module import className\'.\n"
                    print(strOutputError)

                    MyLogger.logException(["DB"], Ex)
                    print(Ex)
                    raise