# SIMULATION D'UNE MARCHE ALÉATOIRE ET REPRÉSENTATION DE SON GRAPHIQUE LLELA11 <- function(N = 4){ # INITIALISATION DES POSITIONS DU LOUP ET DE L'AGNEAU ET CRÉATION DE LA PIÈCE piece <- c("F", "P") DepAX <- 1 ; DepAY <- 1 ; DepLX <- N + 1 ; DepLY <- N + 1 # CALCULS DES N DÉPLACEMENTS ET DES N POSITIONS DANS DES VECTEURS Piece_Agneau <- sample(piece, N, replace = TRUE) DeplaceAgneauX <- (Piece_Agneau == "P") * 1 DeplaceAgneauY <- (Piece_Agneau == "F") * 1 PositionsAX <- c(DepAX, cumsum(DeplaceAgneauX) + DepAX) PositionsAY <- c(DepAY, cumsum(DeplaceAgneauY) + DepAY) Piece_Loup <- sample(piece, N, replace = TRUE) DeplaceLoupX <- -(Piece_Loup == "P") DeplaceLoupY <- -(Piece_Loup == "F") PositionsLX <- c(DepLX, cumsum(DeplaceLoupX) + DepLX) PositionsLY <- c(DepLY, cumsum(DeplaceLoupY) + DepLY) # GRAPHIQUE DE LA MARCHE ALÉATOIRE plot(c(1, N + 1), c(1, N + 1), xlab = NA, ylab = NA) ; grid(col = "grey30") text(c(1, N + 1), c(1, N + 1), c("AGNEAU", "LOUP"), cex = .6, pos = c(3, 1), col = c("blue", "green3")) points(PositionsAX, PositionsAY, pch = 19, cex = .7, col = "blue") lines(PositionsAX, PositionsAY, col = "blue") points(PositionsLX, PositionsLY, pch = 19, cex = .7, col = "green3") lines(PositionsLX, PositionsLY, col = "green3") # MARQUAGE DU POINT DE LA RENCONTRE ET AFFICHAGE D'UN MESSAGE RENCONTRE OU PAS DE RENCONTRE if(sum(((PositionsAX == PositionsLX) & (PositionsAY == PositionsLY))) == 1) { points(PositionsLX[N + 1], PositionsLY[N + 1], pch = 8, cex = 2, col = "red") paste("Rencontre en (", PositionsLX[N + 1]," ; ", PositionsLY[N + 1], ")") } else { "Pas de rencontre" } }