将单人“Snakes&Ladders(蛇梯棋)”游戏扩展为多人版本的技术问询
Multiplayer Snakes & Ladders Implementation in R
Extending the single-player game to multiplayer requires tracking state for each player (position, rolls, slides/ladders encountered) and implementing turn-based gameplay. Here's a complete, commented solution:
Step 1: Define Game Boards (Ladders & Slides)
We'll reuse your original ladder and slide definitions since they're game-wide constants:
# Ladders: start position -> end position (climb up) 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) ) # Slides (Snakes): start position -> end position (slide down) 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) )
Step 2: Player Setup
We'll use a data frame to track each player's progress. You can adjust the number of players easily:
# Configure number of players (change this to add more!) num_players <- 2 # Initialize player state players <- data.frame( Player = paste0("Player ", 1:num_players), CurrentLoc = 0, # Starting position is 0 (off the board) Rolls = 0, # Total dice rolls Slides = 0, # Number of slides encountered Ladders = 0, # Number of ladders climbed stringsAsFactors = FALSE )
Step 3: Main Game Loop
This loop cycles through each player, handles dice rolls, movement, ladder/slide checks, and win conditions:
winner <- NULL # Track the winner (starts as NULL) # Game runs until a winner is found while (is.null(winner)) { # Iterate through each player in turn order for (player_idx in 1:num_players) { # 1. Roll the dice dice_roll <- sample(1:6, 1) players$Rolls[player_idx] <- players$Rolls[player_idx] + 1 # 2. Calculate initial new position new_position <- players$CurrentLoc[player_idx] + dice_roll # 3. Check for ladders (climb up if landed on a start position) ladder_target <- ladder.df$end[ladder.df$start == new_position] if (length(ladder_target) > 0) { new_position <- ladder_target players$Ladders[player_idx] <- players$Ladders[player_idx] + 1 cat(paste0(players$Player[player_idx], " climbed a ladder from ", players$CurrentLoc[player_idx] + dice_roll, " to ", new_position, "!\n")) } # 4. Check for slides (slide down if landed on a start position) slide_target <- slide.df$end[slide.df$start == new_position] if (length(slide_target) > 0) { new_position <- slide_target players$Slides[player_idx] <- players$Slides[player_idx] + 1 cat(paste0(players$Player[player_idx], " slid down from ", players$CurrentLoc[player_idx] + dice_roll, " to ", new_position, "!\n")) } # 5. Update player's current position players$CurrentLoc[player_idx] <- new_position # 6. Check if this player has won (reached or exceeded 100) if (players$CurrentLoc[player_idx] >= 100) { winner <- players$Player[player_idx] cat(paste0("\n", winner, " wins the game!\n")) cat("Final Game Stats:\n") print(players) break # Exit the player turn loop } # 7. Print turn summary for transparency cat(paste0(players$Player[player_idx], " rolled a ", dice_roll, " and moved to position ", new_position, "\n")) } # Exit the main game loop if we have a winner if (!is.null(winner)) break }
Key Improvements for Multiplayer
- Per-Player State Tracking: The
playersdata frame keeps all relevant stats for each player in one place, making it easy to extend or modify. - Turn-Based Gameplay: Players take turns in order, with clear feedback on each action.
- Win Condition Check: After every player's turn, we check if they've reached the winning position, ending the game immediately.
- Customizable Player Count: Just change the
num_playersvariable to add more players.
内容的提问来源于stack exchange,提问作者Joe
相关产品推荐
相关产品推荐

