'Making a one player game multiplayer in R

I have this code that simulates a snakes and ladders game with 2 players. Unfortunately it seems like it simulates a game with 2 players, but the players play independently. I want to make them play together. In this case, I want the game to finish when one of the players lands on 100 (the winner). Right now the code stops running when they both eventually land on 100, so there is no winner.

ladder.df <- data.frame(start=c(1,4,9,21,28,36,51,71,80), end=c(38,14,31,42,84,44,67,91,100))
slide.df <- data.frame(start=c(98,95,93,87,64,62,56,49,47,16), end=c(78,75,73,24,60,19,53,11,26,6))
play_game <- function(ladder_df, slide_df) {
  curLoc <- 0 # Current location
  nroll <- 0 # Number of rolls
  slides <- 0 # Number of slides encountered
  ladders <- 0 # Number of ladders encountered
  # Keep rolling dice and moving until reach 100 or greater ending the game
  while(curLoc < 100) {
    roll <- sample(6,1) # generate random number between [1 to 6]
    curLoc <- curLoc + roll # increase position
    nroll <- nroll + 1 # increase number of rolls
    # Need to check if we landed on a ladder or slide and move forward or back
    if (any(ladder_df$s %in% curLoc)) {
      curLoc <- ladder_df$e[ladder_df$s %in% curLoc]
      ladders <- ladders + 1
    }
    if (any(slide_df$s %in% curLoc)) {
      curLoc <- slide_df$e[slide_df$s %in% curLoc]
      slides <- slides + 1
    }
    else if (curLoc == 100) {
      break
    }
    else if (curLoc > 100){
      curLoc <- curLoc + (100 - curLoc) - (roll - (100 - curLoc))
    }
  }
  return(data.frame(  num_rolls = nroll,
                    num_ladders = ladders,
                     num_slides = slides))
}


 #Create a game with more players
    play_game_with_players <- function(num_players = 1, ladder_df, slide_df) {
      # Pre-allocate a list with the number of players
      results <- vector("list", num_players)
      for (i in seq_along(results)) {
        results[[i]] <- play_game(ladder_df, slide_df)
      }
      return(results)
    }

(First I created a game for one player, and then with the second function I expanded it to many players). Any help is appreciated! I suspect the solution is simple, but I'm not very experienced with R. I am not allowed to use libraries for this assignment.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source