Esempio n. 1
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()
Esempio n. 2
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)
Esempio n. 3
0
    def test_CreateDBLogger(self):
        #Arrange
        logName = "TestLogDB"
        logTableName = "GameCenter"
        logLevel = "DEBUG"
        logConnString = ""
        logDBName = "Log"

        #Act
        MyLogger.addDBLogger(logName, logConnString, logDBName, logTableName,
                             logLevel)

        #Assert
        self.assertIsNotNone(MyLogger.myLog["DB"])
Esempio n. 4
0
    def test_LogToDB(self):
        #Arrange
        logName = "TestLogDB"
        logTableName = "GameCenter"
        logLevel = "DEBUG"
        logConnString = ""
        logDBName = "Log"

        #Act
        MyLogger.addDBLogger(logName, logConnString, logDBName, logTableName,
                             logLevel)
        actResult = MyLogger.logDebug(["DB"], "test")

        #Assert
        self.assertTrue(actResult)
Esempio n. 5
0
    def test_CreateFileLogger(self):
        #Arrange
        logFileName = "TestLog.log"
        logFileMode = "w"
        logName = "TestLogFile"
        logLevel = "DEBUG"

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

        #Assert
        self.assertIsNotNone(MyLogger.myLog["File"])
        assert os.path.exists(myLoggerFile)
Esempio n. 6
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()
Esempio n. 7
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
Esempio n. 8
0
        Lookup game by sorted index and return name of game
        """
        creator = cls.games[gameID]
        if not creator:
            strOutputError = f"Invalid gameID({gameID}) or game not found.\n"
            raise AssertionError(strOutputError)

        return creator


if __name__ == '__main__':
    # start logging
    logFileName = "GameCenter.log"
    logName = "GameCenterLogFile"
    logLevel = "DEBUG"
    MyLogger.addFileLogger(logFileName, logName, logLevel=logLevel)

    logName = "GameCenterLogDB"
    logConnString = ""
    logDBName = "Log"
    logTableName = "GameCenter"
    logLevel = "DEBUG"
    MyLogger.addDBLogger(logName, logConnString, logDBName, logTableName,
                         logLevel)

    # Register list of games using className here
    GameFactory.registerGame("TicTacToe")
    GameFactory.registerGame("ChutesAndLadders")
    GameFactory.registerGame("Othello")

    GameCenter.ShowGameMenu()