Ejemplo n.º 1
0
 def run(self):
     """Define the game behavior and routes"""
     
     # Show main menu
     self.user, self.select = homecontroller.executeMenu(self.user)
     
     # New game
     if self.select == 'N' or self.select == 'n':
         mazeName, maze, sizeIndex = mazecontroller.selectMaze()
         
         gameOpened = True
         while gameOpened:
             # Reset divisor, move, currentScore and items user attributes if currentMaze is not None and if user is ok with that
             if self.user.currentMaze is not None:
                 select = input('\n' + '\033[1;31m' + 'Vous êtes sur le point de supprimer votre labyrinthe sauvegardé. Voulez-vous vraiment continuer (o/n) ? ' + '\033[0m')
             
                 if select == 'O' or select == 'o':    
                     userManager = UserManager(self.user.name)
                     userManager.resetData()
                 elif select == 'N' or select == 'n':
                     gameOpened = False
                 else:
                     print(self.error)
                     continue
             
             gameOpened = mazecontroller.play(mazeName, maze, self.user.name, sizeIndex)
     
     # Load game if data user contains a saved maze
     elif self.user.currentMaze is not None and (self.select == 'C' or self.select =='c'):
         gameOpened = True
         while gameOpened:
             gameOpened = mazecontroller.play(self.user.currentMaze['mazeName'], self.user.currentMaze['maze'], self.user.name)
     
     # Options menu
     elif self.select == 'O' or self.select == 'o':
         optionsOpened = True
         while optionsOpened:
             optionsOpened = optionscontroller.executeMenu(self.user.name)
     
     # Quit the game
     elif self.select == 'Q' or self.select == 'q':
         select = input('Voulez-vous vraiment quitter la partie (o/n) ? ')
         if select == 'O' or select == 'o':
             print('\n' + '\033[1;35m' + 'Au revoir {0} !'.format(self.user.name) + '\033[0m')
             return False
         elif select == 'N' or select == 'n':
             return True
         else:
             print(self.error)
     else:
         print(self.error)
     
     return True
Ejemplo n.º 2
0
def executeMenu(name):
    """Define if user is registered in data/players
    If user has a saved maze in his data, show a load option in menu.
    Return menu view, saveMaze and user instance."""
    userManager = UserManager(name)
    user = userManager.isRegister()
    load = ''
    
    with open('app/home/views/menu.txt') as file:
        content = file.read()
    
    if user.currentMaze is not None:
        load = '\n|  C - Charger partie'
    
    return user, input(content.format(load))
Ejemplo n.º 3
0
def executeMenu(name):
    """Define if user is registered in data/players
    If user has a saved maze in his data, show a load option in menu.
    Return menu view, saveMaze and user instance."""
    userManager = UserManager(name)
    user = userManager.isRegister()
    load = ''

    with open('app/home/views/menu.txt') as file:
        content = file.read()

    if user.currentMaze is not None:
        load = '\n|  C - Charger partie'

    return user, input(content.format(load))
Ejemplo n.º 4
0
def executeStats(user):
    """Show user statistics"""
    # Define a new User object
    stats = UserManager(user)
    stats = stats.isRegister()
    
    with open('app/options/views/statistics.txt') as file:
        content = file.read()
        
    end = input(content.format(stats.name, stats.score, stats.gamesPlayed, stats.bestScores['Welcome'], stats.bestScores['Two_ways'], stats.bestScores['The_vault'], stats.bestScores['Ride'], stats.bestScores['Right_or_left'], stats.bestScores['The_bank'], stats.bestScores['Round-trip'], stats.bestScores['The_pyramid'], stats.bestScores['Fort_Knox']))
    
    if end != '':
        print(errorMessage())
        return True
    else:
        return False
Ejemplo n.º 5
0
def play(nameMaze, maze, userName, sizeIndex=None):
    """Define gameplay and all elements to use in this game"""
    mazeManager = MazeManager()
    userManager = UserManager(userName)
    maze = Maze(nameMaze, maze)
    user = userManager.isRegister()
    error = '\n' + '\033[1;31m' + 'VEUILLEZ SAISIR UNE VALEUR VALIDE (ex : N5 ou Q) !' + '\033[0m'

    # Define divisor if sizeIndex is defined
    if sizeIndex == 1:
        user.divisor = 10
    elif sizeIndex == 2:
        user.divisor = 5
    elif sizeIndex == 3:
        user.divisor = 2

    # get game interface
    with open('app/maze/views/game.txt', 'r') as file:
        game = file.read()

    # Define current maze
    user.currentMaze = {
        'mazeName': maze.name,
        'maze': maze.printMaze(),
    }

    allowMove = True
    while allowMove:
        # Define user data with currentMaze and save it to allow user to reload
        data = {
            'score': user.score,
            'gamesPlayed': user.gamesPlayed,
            'bestScores': {
                'Welcome': user.bestScores['Welcome'],
                'Two_ways': user.bestScores['Two_ways'],
                'The_vault': user.bestScores['The_vault'],
                'Ride': user.bestScores['Ride'],
                'Right_or_left': user.bestScores['Right_or_left'],
                'The_bank': user.bestScores['The_bank'],
                'Round-trip': user.bestScores['Round-trip'],
                'The_pyramid': user.bestScores['The_pyramid'],
                'Fort_Knox': user.bestScores['Fort_Knox']
            },
            'divisor': user.divisor,
            'items': user.items,
            'move': user.move,
            'currentScore': user.currentScore,
            'currentMaze': user.currentMaze
        }

        userManager.saveData(data)

        # Print maze, game informations and allow user to move
        select = input(
            game.format(maze.printMaze(), user.items, user.move,
                        user.currentScore))

        # Quit option
        if select == 'Q' or select == 'q':
            select = input('Voulez-vous vraiment quitter la partie (o/n) ? ')
            if select == 'O' or select == 'o':
                userManager.saveData(data)
                allowMove = False
                return False
            elif select == 'N' or select == 'n':
                continue
            else:
                userManager.saveData(data)
                print('\n' + '\033[1;31m' +
                      'VEUILLEZ SAISIR UNE LETTRE VALIDE !' + '\033[0m')
                continue

        # check select length and define direction and move number
        if len(select) == 1:
            direction = select
            moveNumber = 1
        elif len(select) > 1:
            direction = select[0]

            try:
                moveNumber = int(select[1:])
            except ValueError:
                print(error)
                continue
        else:
            print(error)
            continue

        # Move user and modify informations
        if direction == 'N' or direction == 'n' or direction == 'E' or direction == 'e' or direction == 'S' or direction == 's' or direction == 'O' or direction == 'o':
            try:
                nbItems, end = maze.move(direction, moveNumber)
            except:
                print('\n' + '\033[1;31m' +
                      'AÏE ! VOUS VOUS HEURTEZ À UN MUR !' + '\033[0m')
                continue
            else:
                user.items += nbItems
                user.move += moveNumber
                user.currentMaze['maze'] = maze.printMaze()
        else:
            print(error)
            continue

        # game end
        if end:
            # Add 20 points and add currentScore to data score
            user.currentScore = user.getCurrentScore()
            user.currentScore += 20
            data['score'] += user.currentScore

            # Congratulations message
            message = '\n' + '\033[1;35m' + 'FÉLICITATIONS {0} ! VOUS MARQUEZ {1} POINTS '.format(
                user.name.upper(), user.currentScore)

            # Increment gamesPlayed
            data['gamesPlayed'] += 1

            # Modify bestScore if is greater than current score and print message
            if user.currentScore > data['bestScores'][maze.name]:
                data['bestScores'][maze.name] = user.currentScore
                message += 'ET VOUS BATTEZ VOTRE PRÉCÉDENT RECORD !!!'
            else:
                message += '!!!\nMalheureusement, vous ne battez pas votre précédent record : {0} points'.format(
                    data['bestScores'][maze.name])

            # print message
            print(message + '\033[0m')

            # Save data user and reset it
            userManager.saveData(data)
            userManager.resetData()

            return False
Ejemplo n.º 6
0
def play(nameMaze, maze, userName, sizeIndex=None):
    """Define gameplay and all elements to use in this game"""
    mazeManager = MazeManager()
    userManager = UserManager(userName)
    maze = Maze(nameMaze, maze)
    user = userManager.isRegister()
    error = '\n' + '\033[1;31m' + 'VEUILLEZ SAISIR UNE VALEUR VALIDE (ex : N5 ou Q) !' + '\033[0m'
    
    # Define divisor if sizeIndex is defined
    if sizeIndex == 1:
        user.divisor = 10
    elif sizeIndex == 2:
        user.divisor = 5
    elif sizeIndex == 3:
        user.divisor = 2
    
    # get game interface
    with open('app/maze/views/game.txt', 'r') as file:
        game = file.read()
    
    # Define current maze
    user.currentMaze = {
        'mazeName': maze.name,
        'maze': maze.printMaze(),
    }
    
    allowMove = True
    while allowMove:
        # Define user data with currentMaze and save it to allow user to reload
        data = {
            'score': user.score,
            'gamesPlayed': user.gamesPlayed,
            'bestScores': {
                'Welcome': user.bestScores['Welcome'],
                'Two_ways': user.bestScores['Two_ways'],
                'The_vault': user.bestScores['The_vault'],
                'Ride': user.bestScores['Ride'],
                'Right_or_left': user.bestScores['Right_or_left'],
                'The_bank': user.bestScores['The_bank'],
                'Round-trip': user.bestScores['Round-trip'],
                'The_pyramid': user.bestScores['The_pyramid'],
                'Fort_Knox': user.bestScores['Fort_Knox']
            },
            'divisor': user.divisor,
            'items': user.items,
            'move': user.move,
            'currentScore': user.currentScore,
            'currentMaze': user.currentMaze
        }
    
        userManager.saveData(data)
        
        # Print maze, game informations and allow user to move
        select = input(game.format(maze.printMaze(), user.items, user.move, user.currentScore))
        
        # Quit option
        if select == 'Q' or select == 'q':
            select = input('Voulez-vous vraiment quitter la partie (o/n) ? ')
            if select == 'O' or select == 'o':
                userManager.saveData(data)
                allowMove = False
                return False
            elif select == 'N' or select == 'n':
                continue
            else:
                userManager.saveData(data)
                print('\n' + '\033[1;31m' + 'VEUILLEZ SAISIR UNE LETTRE VALIDE !' + '\033[0m')
                continue
        
        # check select length and define direction and move number
        if len(select) == 1:
            direction = select
            moveNumber = 1
        elif len(select) > 1:
            direction = select[0]
            
            try:
                moveNumber = int(select[1:])
            except ValueError:
                print(error)
                continue
        else:
            print(error)
            continue
        
        # Move user and modify informations
        if direction == 'N' or direction == 'n' or direction == 'E' or direction == 'e' or direction == 'S' or direction == 's' or direction == 'O' or direction == 'o':
            try:
                nbItems, end = maze.move(direction, moveNumber)
            except:
                print('\n' + '\033[1;31m' + 'AÏE ! VOUS VOUS HEURTEZ À UN MUR !' + '\033[0m')
                continue
            else:
                user.items += nbItems
                user.move += moveNumber
                user.currentMaze['maze'] = maze.printMaze()
        else:
            print(error)
            continue
        
        # game end
        if end:
            # Add 20 points and add currentScore to data score
            user.currentScore = user.getCurrentScore()
            user.currentScore += 20
            data['score'] += user.currentScore
            
            # Congratulations message
            message = '\n' + '\033[1;35m' + 'FÉLICITATIONS {0} ! VOUS MARQUEZ {1} POINTS '.format(user.name.upper(), user.currentScore)
            
            # Increment gamesPlayed
            data['gamesPlayed'] += 1
            
            # Modify bestScore if is greater than current score and print message
            if user.currentScore > data['bestScores'][maze.name]:
                data['bestScores'][maze.name] = user.currentScore
                message += 'ET VOUS BATTEZ VOTRE PRÉCÉDENT RECORD !!!'
            else:
                message += '!!!\nMalheureusement, vous ne battez pas votre précédent record : {0} points'.format(data['bestScores'][maze.name])
            
            # print message
            print(message + '\033[0m')
            
            # Save data user and reset it
            userManager.saveData(data)
            userManager.resetData()
            
            return False