Hello guys,i am begginer at R and i have an assignment to simulate a snakes and ladders game in R.The board has 100 squares. The only winning square is 100, for example if you’re on square 98 and roll a 6 you would go forward 2 spaces to 100 and then bounce back 4 spaces to 96. My difficulty is insterting the snakes/ladders transitions in the complete transition matrix, and coding the winning condition.
Thats my code.
> nplayers<- 2
> SnakesAndLadderS<-
+ function(nplayers)
+ {
+ transitions <- rbind(
+ c(4,25),
+ c(13,46),
+ c(27,5),
+ c(33,49),
+ c(40,3),
+ c(42,63),
+ c(43,18),
+ c(50,69),
+ c(54,31),
+ c(62,81),
+ c(66,45),
+ c(74,92),
+ c(76,58),
+ c(89,53),
+ c(99,41))
+
+
+ transmat <- 1:106
+ names(transmat) <- as.character(1:106)
+ transmat[transitions[,1]] <- transitions[,2]
+
+ lastpos <- 0
+ curpos <- history <- NULL
+ while(all(curpos < 100)) {
+ curpos <- lastpos + sample(1:6, nplayers, repl=TRUE)
+ curpos <- transmat[curpos]
+ if(any(curpos > 100)) curpos[curpos > 100] <- lastpos[curpos > 100]
+
+ lastpos <- curpos
+ history <- rbind(history, curpos)
+ }
+
+ history
+ }