mardi 21 novembre 2017

If conditions appear to not be activating?Monopoly Simulation

I am attempting to simulate a 1 player game of monopoly. I have tried to list all Cchest cards, chance cards, and rolling doubles 3x "speeding go to jail" rule. When i simulate I am getting equal probability for all squares. I should be expecting Jail to have the highest. I am not sure what is going wrong, here is my code.

simulate_monopoly <- function(n,d) {

  die1 = sample(1:d, n, replace = TRUE)
  die2 = sample(1:d, n, replace = TRUE)

  rolls = die1 +die2
  #Do chance and commuinity chest Sample outside of the loop

  position = numeric(n+1) #Store position
  position[1] = 0
  position = c(rep(0, n))
  NewPosition = c(rep(0,n))
  Chance = sample(1:16, 1, replace = TRUE)
  Chest = sample(1:10, 1, replace = TRUE)

  i = 1
  for (i in 1:n){

    NewPosition[i] = rolls[i] + position[i]
    NewPosition[i] = NewPosition[i] %% 40
    position[i+1] = NewPosition[i]

    if (die1[i] == die2[i] && die1[i+1] == die2[i+1] && die1[i+2] == die2[i+2]&& die1[i+3] != die2[i+3])     
    {NewPosition[i+3] = 10

    NewPosition[i] = rolls[i] + position[i]
    NewPosition[i] = NewPosition[i] %% 40
    position[i+1] = NewPosition[i]}

    if (NewPosition[i] == 2 || NewPosition[i] == 17 || NewPosition[i] == 33){

    Chest

      if (Chest == 1){
        NewPosition[i+1] = 10
      }  
      else if (Chest == 2){
        NewPosition[i+1] = 0}
      else {NewPosition[i+1] = NewPosition[i]}  


    NewPosition[i+2] = rolls[i+2] + position[i+2]
    NewPosition[i+2] = NewPosition[i+2] %% 40
    position[i+3] = NewPosition[i+2]}


    if (NewPosition[i] == 7 ||NewPosition[i] == 22 || NewPosition[i] == 36){

      Chance

        if (Chance == 1){
          NewPosition[i+1] = 10}

        if (Chance == 2){
          NewPosition[i+1] = 0}
        if (Chance == 3){
          NewPosition[i+1] = 11
        }
        if (Chance == 4){
          NewPosition[i+1] = 24
        }
        if (Chance == 5){
          NewPosition[i+1] = 39}

        if (Chance == 6){
          NewPosition[i+1] = 5}

        if (Chance == c(7||8)){
          if (NewPosition[i] == 7){
            NewPosition[i+1] = 15
          }
          else if (NewPosition[i] == 22){
            NewPosition[i+1] = 25}
          else if (NewPosition[i] == 36){
            NewPosition[i+1] = 5
          }
        }
        if (Chance == 9){
          if (NewPosition[i] == c(7||36)){
            NewPosition[i+1] = 12}
          else if (NewPosition[i] == 22){
            NewPosition[i+1] = 28}
        }
        if (Chance ==10){
          NewPosition[i+1] = NewPosition[i+1] - 3 
        }
      NewPosition[i+2] = rolls[i+2] + position[i+2]
      NewPosition[i+2] = NewPosition[i+2] %% 40
      position[i+3] = NewPosition[i+2]
  }  
  }
  return(NewPosition)  
  }

sim_10000 <- simulate_monopoly(10000,6)

table(sim_10000)

Aucun commentaire:

Enregistrer un commentaire