如何将方法实现为函数

时间:2019-02-15 08:38:15

标签: r

我有一个名为taketurn的函数,我正在尝试实现一个称为doubles的功能,将其翻成taketurn,以便它检查掷骰是否掷骰子。我想知道如何植入该功能。我尝试将player $ double()放入taketurn函数中,但出现错误。我通过doubles函数得到的错误无法返回true或false。

player <- setRefClass("player", 
  fields = list(
    pos = "numeric",      # position on the board
    verbose = "logical",
    jail="logical",
    tjail="numeric",
    countd= "numeric" # option to have it print all the info
  ), 
  methods = list(



  doubles=function(player, tracking){
      roll<-dice()
      countd<<-0
      while(countd<=3){
        countd<<-countd+1
        if(roll$doubles==FALSE){
       move_fwd(roll$movement)
      tracking$increase_count(pos)
       if(verbose) cat(paste("Tally at", pos, ":", gameboard$title[pos], "\n"))
      break #addes doubles and if not odubles player moves like normal
      } #if not 3 yet
      if(countd<3) {
        move_fwd(roll$movement)
        tracking$increase_count(pos)
      if(verbose) cat(paste("Tally at", pos, ":", gameboard$title[pos], "\n"))
      if(verbose)cat(paste("Doubles, roll again"))
      if(verbose)cat(paste("You have rolled",countd,"doubles"))
        jail<<-FALSE
      }
      else{
        if(countd==3){
          go_2_space_n(11)
          if(verbose)cat("You have rolled 3 doubles,go to jail")
          go_2_jail(player,tracking)
          jail<<-TRUE 
          times_jail()
          break
        }
      }
      }
      },

)

taketurn <- function(player, tracking){  
  roll <- dice()

  player$move_fwd(roll$movement)
  tracking$increase_count(player$pos)
}

```{r}

0 个答案:

没有答案