Skip to content

Instantly share code, notes, and snippets.

@R3DHULK
Created March 7, 2023 19:13
Show Gist options
  • Save R3DHULK/919cbd592eecd701077c728890fd4130 to your computer and use it in GitHub Desktop.
Save R3DHULK/919cbd592eecd701077c728890fd4130 to your computer and use it in GitHub Desktop.
Text Based Train Simulator In R
# define the initial state of the game
game_state <- list(
time = 0,
speed = 0,
position = 0,
distance = 1000,
max_speed = 100,
acceleration = 10,
braking = 20
)
# define a function to print the game state
print_state <- function() {
cat("Time:", game_state$time, "\n")
cat("Speed:", game_state$speed, "\n")
cat("Position:", game_state$position, "\n")
cat("Distance:", game_state$distance, "\n")
}
# define a function to update the game state based on the player's action
update_state <- function(action) {
# calculate the time elapsed
time_elapsed <- 1
# calculate the new speed and position based on the player's action
if (action == "accelerate") {
new_speed <- min(game_state$speed + game_state$acceleration * time_elapsed, game_state$max_speed)
} else if (action == "brake") {
new_speed <- max(game_state$speed - game_state$braking * time_elapsed, 0)
} else {
new_speed <- game_state$speed
}
new_position <- game_state$position + new_speed * time_elapsed
# update the game state
game_state$time <- game_state$time + time_elapsed
game_state$speed <- new_speed
game_state$position <- new_position
game_state$distance <- game_state$distance - new_position + game_state$position
}
# print the initial game state and simulate the game loop
print_state()
while (game_state$distance > 0) {
cat("\n")
cat("Enter 'a' to accelerate, 'b' to brake, or 'n' to do nothing.\n")
action <- readline()
update_state(action)
print_state()
}
# print the final game state
cat("Game over!\n")
print_state()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment