Découvrir le projet et le logiciel PyRat

Projet – Séance 1

  • Le jeu PyRat
  • Objectifs du projet
  • Organisation & Évaluation
  • Créer un programme pour PyRat
  • Récapitulatif de la séance

Le jeu PyRat

À quoi ça ressemble ?

Le jeu PyRat

À quoi ça ressemble ?

Le jeu PyRat

But du jeu

Comment on gagne ?

  • Solo $\rightarrow$ Attraper tous les fromages
  • Multi $\rightarrow$ Attraper + de fromages que les autres équipes

Déroulement d’une partie

Objectifs du projet

Description globale

On y fait quoi ?

  • Programmez des joueurs pour PyRat
  • Customisez les situations de jeu
  • Objectifs à difficulté croissante

Comment on fait ça ?

  • Des codes en Python pour contrôler les mouvements

Objectifs du projet

Séance 1


  • Config $\rightarrow$ 1 fromage
  • Stratégie $\rightarrow$ Exploration aléatoire

Objectifs du projet

Séance 2


  • Config $\rightarrow$ 1 fromage
  • Stratégie $\rightarrow$ DFS / BFS

Objectifs du projet

Séance 3


  • Config $\rightarrow$ 1 fromage + boue
  • Stratégie $\rightarrow$ Algo de Dijkstra

Objectifs du projet

Séance 4


  • Config $\rightarrow$ $N$ fromages + boue
  • Stratégie $\rightarrow$ TSP (exhaustif)

Objectifs du projet

Séance 5


  • Config $\rightarrow$ $N$ fromages + boue
  • Stratégie $\rightarrow$ TSP (heuristique)

Objectifs du projet

Séance 6


  • Config $\rightarrow$ $N$ fromages + boue + adversaire
  • Stratégie $\rightarrow$ Comme vous voulez !

Objectifs du projet

Le tournoi PyRat


Organisation & Évaluation

Classes inversées

Organisation & Évaluation

Préparer une séance

  • Repérez la section suivante sur chaque page :

  • Globalement :

    • Terminez la sesson précédente
    • Lisez les articles / vidéos

Organisation & Évaluation

Évaluation

Quoi ?

  • Rendus $\rightarrow$ 2 livrables intermédiaires
  • Examen final $\rightarrow$ Présentation orale finale

Quand ?

Organisation & Évaluation

Évaluation

On évalue quoi ?

  • Code (C) $\rightarrow$ Fonctionnel, propre
  • Documentation (D) $\rightarrow$ Utilisable, compréhensible
  • Tests (T) $\rightarrow$ Vérifié, garanties

Comment ?

  • Chaque étudiant du trinôme est responsable d’un aspect (C/D/T)
  • On tourne à chaque évaluation
  • Note collective, pondération selon le rôle : $note(resp_D) = (C + 1.5 D + T) / 3.5$

Créer un programme pour PyRat

Une partie de PyRat est un script qui initialise les élements

Les dossiers principaux

  • Games $\rightarrow$ Des codes pour customiser et lancer les parties
  • Players $\rightarrow$ Des modules Python pour décrire le comportement des joueurs

Exemple de partie

# Imports
from pyrat import Game, PlayerSkin
from players.Random5 import Random5
from players.Random6 import Random6

# Run the script
if __name__ == "__main__":

    # First, let's create a game with custom arguments
    game = Game(mud_percentage=20.0, wall_percentage=60.0, maze_width=13, maze_height=10, nb_cheese=1)

    # Instantiate players with different skins, and add them to the game in distinct teams
    player_1 = Random5(skin=PlayerSkin.RAT)
    player_2 = Random6(skin=PlayerSkin.PYTHON)
    game.add_player(player_1, team="Team Ratz")
    game.add_player(player_2, team="Team Pythonz")

    # Start the game
    stats = game.start()

Créer un programme pour PyRat

Un joueur est décrit par une classe Python

Définition de la classe

class Random3 (Player):

    """
    *(This class inherits from* ``Player`` *).*

    This player is an improvement of the ``Random2`` player.
    Here, we add elements that help us explore better the maze.
    More precisely, we keep a set of cells that have already been visited in the game.
    Then, at each turn, we choose in priority a random move among those that lead us to an unvisited cell.
    If no such move exists, we move randomly.
    """

Instanciation du joueur

# Instantiate a variable of type Random3
p = Random3(name="Mon super joueur", skin=PlayerSkin.MARIO)

Créer un programme pour PyRat

Un joueur est décrit par une classe Python

Le constructeur indique quoi faire à l’instanciation

def __init__ ( self,
               *args:    object,
               **kwargs: object
             ) ->        None:

    """
    Initializes a new instance of the class.
    Here, the constructor is only used to initialize a set that will keep track of visited cells.
    This set can later be updated at each turn of the game to avoid going back to cells that have already been visited.

    Args:
        args:   Arguments to pass to the parent constructor.
        kwargs: Keyword arguments to pass to the parent constructor.
    """

    # Inherit from parent class
    super().__init__(*args, **kwargs)

    # We create an attribute to keep track of visited cells
    # We will initialize it in the ``preprocessing()`` method to allow the game to be reset
    # Otherwise, the set would keep the cells visited in previous games
    self.visited_cells = None

Créer un programme pour PyRat

Un joueur est décrit par une classe Python

Rappel — Le déroulement d’une partie

Créer un programme pour PyRat

Un joueur est décrit par une classe Python

La méthode preprocessing()

def preprocessing ( self,
                    maze:       Maze,
                    game_state: GameState,
                  ) ->          None:
    
    """
    *(This method redefines the method of the parent class with the same name).*

    This method is called once at the beginning of the game.
    Here, we just initialize the set of visited cells.

    Args:
        maze:       An object representing the maze in which the player plays.
        game_state: An object representing the state of the game.
    """

    # Initialize visited cells
    self.visited_cells = set()

Créer un programme pour PyRat

Un joueur est décrit par une classe Python

La méthode turn()

def turn ( self,
           maze:       Maze,
           game_state: GameState,
         ) ->          Action:

    """
    *(This method redefines the method of the parent class with the same name).*

    It is called at each turn of the game.
    It returns an action to perform among the possible actions, defined in the ``Action`` enumeration.
    We also update the set of visited cells at each turn.

    Args:
        maze:       An object representing the maze in which the player plays.
        game_state: An object representing the state of the game.
    
    Returns:
        One of the possible actions.
    """

    # Mark current cell as visited
    my_location = game_state.player_locations[self.get_name()]
    if my_location not in self.visited_cells:
        self.visited_cells.add(my_location)

    # Return an action
    action = self.find_next_action(maze, game_state)
    return action

Créer un programme pour PyRat

Un joueur est décrit par une classe Python

Récapitulatif pour créer un joueur

  • Une classe qui hérite de Player

  • Un constructeur __init__() qui déclare les attributs

  • Une méthode preprocessing(maze, game_state) qui :

    • Initialise les attributs
    • Pré-calcule des trucs pour les réutiliser plus tard
  • Une méthode turn(maze, game_state) qui :

    • Fait des calculs à partir des attributs et de l’état du jeu
    • Renvoie un mouvement (Action.NORTH, Action.WEST, …)
  • Possibilité d’ajouter plein d’autres méthodes selon les besoins !

Récapitulatif de la séance

Les éléments à retenir

Résumé

  • Le projet est la situation d’apprentissage principale du semestre

  • On utilisera la théorie des graphes comme fil rouge

  • Appliquez ce que vous verrez en environnement, algorithmique et programmation

Attention

  • Objectifs à difficulté croissante $\rightarrow$ Ne prenez pas de retard !

  • Classes inversées $\rightarrow$ Vous devez lire les articles avant le cours !

Récapitulatif de la séance

Et maintenant ?

Activité pratique (~1h15)

Mise en place de PyRat

  • Constituez un trinôme (niveau équivalent)
  • Installez PyRat
  • Initialisez un dépôt Git
  • Découvrez PyRat
  • Écrivez votre premier programme PyRat

Après la séance

  • Terminez l’activité
  • Allez voir la partie “Avant le cours” de la séance suivante