-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsnakesladders.Rmd
More file actions
115 lines (97 loc) · 2.29 KB
/
snakesladders.Rmd
File metadata and controls
115 lines (97 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
---
title: "Snakes and Ladders"
author: "Matt"
date: "2/12/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
This document contains student solutions to the snakes and ladders problem. Students will add to this file using the outline below. Write your name (using three hashtags), then below create an R code block and insert your code. Save the .rmd file, then knit the document to update the html output. Then submit a pull request to merge your changes to the main repository.
### Melissa Horger
```{r, eval=F}
roll_dice<- function(x){
return(sample(1:6, x, replace = TRUE, prob = NULL))
}
roll_dice(10)
total_sum<-0
number_of_rolls<-0
while(total_sum < 25){
number_of_rolls <- number_of_rolls+1
total_sum <-total_sum+sample(c(1,2,3,4,5,6),1)
}
number_of_rolls
# based on the demo in the assignment, the average number of rolls needed to finish the board is 7.5
total_sum<-0
number_of_rolls<-0
while(total_sum < 25){
number_of_rolls <- number_of_rolls+1
total_sum <-total_sum+sample(c(1,2,3,4,5,6),1)
if(total_sum==3)
total_sum+7
if(total_sum==4)
total_sum+10
if(total_sum==6)
total_sum+11
if(total_sum==8)
total_sum+11
if(total_sum==9)
total_sum+9
if(total_sum==10)
total_sum+2
if(total_sum==16)
total_sum+8
if(total_sum==20)
total_sum+2
}
number_of_rolls
total_sum
save_rolls <- c()
for(sims in 1:100){
total_sum<-0
number_of_rolls<-0
while(total_sum < 25){
number_of_rolls <- number_of_rolls+1
total_sum <-total_sum+sample(c(1,2,3,4,5,6),1)
if(total_sum==3)
total_sum+7
if(total_sum==4)
total_sum+10
if(total_sum==6)
total_sum+11
if(total_sum==8)
total_sum+11
if(total_sum==9)
total_sum+9
if(total_sum==10)
total_sum+2
if(total_sum==16)
total_sum+8
if(total_sum==20)
total_sum+2
}
save_rolls[sims] <- number_of_rolls
}
save_rolls
```
### Jeff Kravitz
```{r}
move <- 0
count <- replicate(1000, 0)
game_grid <- data.frame(c(3,6,9,10,14,19,22,24),c(11,17,18,12,4,8,20,16))
for (i in 1:1000) {
spot <- 1
while (spot <= 25) {
move <- sample(1:6, 1)
spot <- spot + move
for (i in 1:8) {
if (spot == game_grid[i,1]) {
spot <- game_grid[i,2]
}
}
count[i] = count[i] + 1
}
}
avg_moves <- mean(count)
print(avg_moves)
```