Problem Set 3: Solutions | 8 April 2020 - 15 April 202 at 10 AM

Problem 1. Review

Let’s play a game - snakes and ladders!

Snakes and Ladders is an ancient Indian board game regarded today as a worldwide classic. It is played between two or more players on a board with numbered, gridded squares and populated by “ladders” and “snakes”. These “ladders” and “snakes” connect two specific board squares together. If you land on a ladder after the role of a die, you jump up the board game and get closer to the final square. If you land on a snake, you fall down the board game.

How to play:

# Snakes and Ladders 

# Step 1: Create Your Board with Snakes And Ladders
finalSquare <- 25
board <- c(0:finalSquare)
board[03] = +08
board[06] = +11
board[09] = +09
board[10] = +02
board[14] = -10
board[19] = -11
board[22] = -02
board[24] = -08

# Step 2: Initializations
square <- 0

# Step 3: Loop

# You will need your loop to allow you to 
## 1. move up or down for a snake or ladder
## 2. roll the dice
## 3. move by that rolled amount
## 4. once you get to the final square, game stops and print("Game over!")

repeat {
    square <- square + board[(square+1)]
    diceRoll <- sample(1:6,1)
    square <- square + diceRoll
    print(square)
      if (square > finalSquare) {
        print("Game over!")
        break
      }}
## [1] 1
## [1] 5
## [1] 17
## [1] 38
## [1] "Game over!"
# Alternative-way to code this game by Ana Silberg 

# Step 1: Create Your Board

board_length = 100

snakes <- list("99" = "54", "70" = "55", "52" = "42", "25" = "2", "95" = "72")

ladders <- list("6" = "25", "11" = "40", "60" = "85", "46" = "90", "17" = "69")

# Step 2: Initializations

player_location <- 1

# Step 3: Loop

# move up or down for ladder or snake
# roll the dice
# move by that rolled amount
# once you get to the final square, game stops and print("Game over!")

while(player_location < board_length){
  # Roll die at start of turn
  die_roll_value <- sample(1:6, 1)
  player_location <- player_location + die_roll_value
  # sep removes the space cat normally adds between parameters in function
  cat("Rolled a ", die_roll_value, "! ", sep = "")
  
  # These hold the end location of snakes or ladders if they exist. NULL otherwise. We access the end point of the snake or ladder by finding the list with the name of the starting position. This returns a list and to convert it to a string we use "unlist"
  snake_position <- unlist(snakes[toString(player_location)])
  ladder_position <- unlist(ladders[toString(player_location)])
  
  # Checks if there is a snake at the current location. "is.null" evaluates to true if it is NULL.
  if (is.character(snake_position)){
    cat("Landed on a snake starting at position ", player_location, "! ", sep = "")
    player_location <- as.integer(snake_position)
    cat("Now at position ", player_location, "! :(", "\n", sep = "")
  }
  # Does the same for ladder
  else if (is.character(ladder_position)){
    cat("Landed on a ladder starting at position ", player_location, "! ", sep = "")
    player_location <- as.integer(ladder_position)
    cat("Now at position ", player_location, "! :)", "\n", sep = "")
  }
  else {
    cat("Now at position ", player_location, "!", "\n", sep = "")
  }
}
## Rolled a 5! Landed on a ladder starting at position 6! Now at position 25! :)
## Rolled a 6! Now at position 31!
## Rolled a 1! Now at position 32!
## Rolled a 3! Now at position 35!
## Rolled a 2! Now at position 37!
## Rolled a 2! Now at position 39!
## Rolled a 5! Now at position 44!
## Rolled a 1! Now at position 45!
## Rolled a 6! Now at position 51!
## Rolled a 3! Now at position 54!
## Rolled a 3! Now at position 57!
## Rolled a 4! Now at position 61!
## Rolled a 4! Now at position 65!
## Rolled a 1! Now at position 66!
## Rolled a 6! Now at position 72!
## Rolled a 3! Now at position 75!
## Rolled a 1! Now at position 76!
## Rolled a 1! Now at position 77!
## Rolled a 2! Now at position 79!
## Rolled a 1! Now at position 80!
## Rolled a 2! Now at position 82!
## Rolled a 6! Now at position 88!
## Rolled a 3! Now at position 91!
## Rolled a 3! Now at position 94!
## Rolled a 5! Landed on a snake starting at position 99! Now at position 54! :(
## Rolled a 3! Now at position 57!
## Rolled a 6! Now at position 63!
## Rolled a 4! Now at position 67!
## Rolled a 2! Now at position 69!
## Rolled a 3! Now at position 72!
## Rolled a 1! Now at position 73!
## Rolled a 3! Now at position 76!
## Rolled a 3! Now at position 79!
## Rolled a 5! Now at position 84!
## Rolled a 1! Now at position 85!
## Rolled a 6! Now at position 91!
## Rolled a 4! Landed on a snake starting at position 95! Now at position 72! :(
## Rolled a 5! Now at position 77!
## Rolled a 6! Now at position 83!
## Rolled a 5! Now at position 88!
## Rolled a 3! Now at position 91!
## Rolled a 1! Now at position 92!
## Rolled a 3! Landed on a snake starting at position 95! Now at position 72! :(
## Rolled a 1! Now at position 73!
## Rolled a 4! Now at position 77!
## Rolled a 4! Now at position 81!
## Rolled a 2! Now at position 83!
## Rolled a 3! Now at position 86!
## Rolled a 4! Now at position 90!
## Rolled a 4! Now at position 94!
## Rolled a 4! Now at position 98!
## Rolled a 1! Landed on a snake starting at position 99! Now at position 54! :(
## Rolled a 3! Now at position 57!
## Rolled a 5! Now at position 62!
## Rolled a 6! Now at position 68!
## Rolled a 1! Now at position 69!
## Rolled a 2! Now at position 71!
## Rolled a 1! Now at position 72!
## Rolled a 6! Now at position 78!
## Rolled a 6! Now at position 84!
## Rolled a 2! Now at position 86!
## Rolled a 4! Now at position 90!
## Rolled a 3! Now at position 93!
## Rolled a 4! Now at position 97!
## Rolled a 3! Now at position 100!
print("Game over!")
## [1] "Game over!"

Problem 2. Nested Loops

Use a nested-for loop to display the multiplication tables from 1-10.

for (i in seq(1,10)){
   for (j in seq(1,10)) {
     k = i*j
     cat(k, end="\t")
   }
  cat("\n")
}
## 1    2   3   4   5   6   7   8   9   10  
## 2    4   6   8   10  12  14  16  18  20  
## 3    6   9   12  15  18  21  24  27  30  
## 4    8   12  16  20  24  28  32  36  40  
## 5    10  15  20  25  30  35  40  45  50  
## 6    12  18  24  30  36  42  48  54  60  
## 7    14  21  28  35  42  49  56  63  70  
## 8    16  24  32  40  48  56  64  72  80  
## 9    18  27  36  45  54  63  72  81  90  
## 10   20  30  40  50  60  70  80  90  100