---
title: "Battle Simulations"
format: html
---

```{r}
# Define the combat_simulation function as before
combat_simulation <- function(attack_primary, front_op, back_op, skill_defense) {
  # ... (the same function as before)
}

# Create the stats data frame
stats <- data.frame(
  player = c("a01", "b01", "b02"),
  op = c(10, 10, 10),
  ap = c(2, 2, 2),
  hp = c(3, 3, 3),
  defense = c(0, 0, 0),
  attack.primary = c(4, 5, 5),
  attack.secondary = c(3, 2, 2),
  skill.defense = c(5, 3, 3),
  skill.recon = c(2, 3, 3),
  revealed = c(0, 0, 0)
)

# Simulate the combat rounds
for (round in 1:4) {
  attacker <- ifelse(round %% 2 == 1, "b01", "a01")
  defender <- ifelse(round %% 2 == 1, "a01", "b01")
  
  attacker_stats <- stats[stats$player == attacker, ]
  defender_stats <- stats[stats$player == defender, ]
  
  result <- combat_simulation(
    attacker_stats$attack.primary,
    attacker_stats$op,
    defender_stats$op,
    defender_stats$skill.defense
  )
  
  if (is.character(result)) {
    # Handle the case where combat ends ("miss" or "end")
    if (result == "miss") {
      cat(sprintf("%s attacks %s but misses.\n", attacker, defender))
    } else {
      cat(sprintf("%s attacks %s and the combat ends.\n", attacker, defender))
      break  # End the combat loop
    }
  } else {
    # Update the stats data frame with the new values
    stats[stats$player == attacker, "op"] <- result$front_op
    stats[stats$player == defender, "op"] <- result$back_op
    cat(sprintf("%s attacks %s. New stats:\n", attacker, defender))
    print(stats)
  }
}


```


