# CSC 120, Assignment #3, Function definitions. # The state of the simulation is represented by a list with two elements, # named "grid" and "pos". The "grid" element is a matrix of 0s and 1s, # with a 1 in a grid position where grass is present. The "pos" element # is a vector of length two giving the row and column position of the cow. # Update the state by one time step. Takes the current state and the # probability of grass growing at a point as arguments, and returns # the updated state. one_step <- function (state, prgrow) { grid <- state$grid pos <- state$pos # Move the cow randomly up, down, left, or right, staying where it is # if the move would take it off the grid. U <- runif(1) if (U<0.25) { if (pos[1]>1) pos[1] <- pos[1] - 1 } else if (U<0.5) { if (pos[1]1) pos[2] <- pos[2] - 1 } else { if (pos[2]