如何跟踪R中的stopifnot的根?

时间:2014-03-22 07:51:17

标签: r function matrix breakpoints tracing

我有以下程序:

four.in.a.row = function(player, v, debug=FALSE) {
  if (debug) {
    cat(sep="", "four.in.a.row(player=", player, ", v=", v, ")\n")
  }
  with(rle(v), any(lengths== 4 & values == player))
}
won = function(player, board, r, c, debug=FALSE) {
  if (debug) {
    cat(sep="", "won(player=", player, ", board=\n")
    print(board)
    cat(sep="", ", r=", r, ", c=", c, ")\n")
  }
  row=x[r,]
  print(row)
  col=x[,c]
  print(col)
  reverse_diag=x[row(x) + col(x) == r + c]
  print(reverse_diag)
  diag=x[row(x) - col(x) == r - c]
  print(diag)
  four.in.a.row(player,row,debug=FALSE)
  four.in.a.row(player,col,debug=FALSE)
  four.in.a.row(player,diag,debug=FALSE)
  four.in.a.row(player,reverse_diag,debug=FALSE)

  return(FALSE) 
}


x = matrix(data=c(
  "E","E","E","E","E","E","E",
  "E","E","E","E","E","E","E",
  "E","E","E","E","E","E","E",
  "E","E","E","E","E","E","E",
  "E","E","E","E","E","E","E",
  "E","E","E","E","E","E","E"
), nrow=6, ncol=7, byrow=TRUE)
stopifnot(!won(player="X", board=x, r=1, c=1, debug=TRUE))
stopifnot(!won(player="O", board=x, r=1, c=1, debug=TRUE))
stopifnot(!won(player="X", board=x, r=2, c=3, debug=TRUE))
stopifnot(!won(player="O", board=x, r=2, c=3, debug=TRUE))

x = matrix(data=c(
  "E","E","E","E","E","E","O",
  "E","E","E","E","E","E","O",
  "E","E","E","E","E","E","O",
  "E","E","E","E","E","E","O",
  "E","E","E","E","E","E","X",
  "X","X","X","X","O","E","X"
), nrow=6, ncol=7, byrow=TRUE)
stopifnot( won(player="X", board=x, r=6, c=1, debug=TRUE))
stopifnot(!won(player="O", board=x, r=6, c=1, debug=TRUE))
stopifnot(!won(player="X", board=x, r=1, c=7, debug=TRUE))
stopifnot( won(player="O", board=x, r=1, c=7, debug=TRUE))

我收到了这个错误(虽然我不期待它):

> source("rle.R")
won(player=X, board=
     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[2,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[3,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[4,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[5,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[6,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
, r=1, c=1)
[1] "E" "E" "E" "E" "E" "E" "E"
[1] "E" "E" "E" "E" "E" "E"
[1] "E"
[1] "E" "E" "E" "E" "E" "E"
won(player=O, board=
     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[2,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[3,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[4,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[5,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[6,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
, r=1, c=1)
[1] "E" "E" "E" "E" "E" "E" "E"
[1] "E" "E" "E" "E" "E" "E"
[1] "E"
[1] "E" "E" "E" "E" "E" "E"
won(player=X, board=
     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[2,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[3,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[4,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[5,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[6,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
, r=2, c=3)
[1] "E" "E" "E" "E" "E" "E" "E"
[1] "E" "E" "E" "E" "E" "E"
[1] "E" "E" "E" "E"
[1] "E" "E" "E" "E" "E" "E"
won(player=O, board=
     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[2,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[3,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[4,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[5,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
[6,] "E"  "E"  "E"  "E"  "E"  "E"  "E" 
, r=2, c=3)
[1] "E" "E" "E" "E" "E" "E" "E"
[1] "E" "E" "E" "E" "E" "E"
[1] "E" "E" "E" "E"
[1] "E" "E" "E" "E" "E" "E"
won(player=X, board=
     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] "E"  "E"  "E"  "E"  "E"  "E"  "O" 
[2,] "E"  "E"  "E"  "E"  "E"  "E"  "O" 
[3,] "E"  "E"  "E"  "E"  "E"  "E"  "O" 
[4,] "E"  "E"  "E"  "E"  "E"  "E"  "O" 
[5,] "E"  "E"  "E"  "E"  "E"  "E"  "X" 
[6,] "X"  "X"  "X"  "X"  "O"  "E"  "X" 
, r=6, c=1)
[1] "X" "X" "X" "X" "O" "E" "X"
[1] "E" "E" "E" "E" "E" "X"
[1] "X" "E" "E" "E" "E" "E"
[1] "X"
Error: won(player = "X", board = x, r = 6, c = 1, debug = TRUE) is not TRUE

是否有快速通过/断点停止或任何其他方法来追踪错误的方法?除此之外你知道我为什么得到错误的答案吗?

1 个答案:

答案 0 :(得分:1)

你可以做到

debugonce(won)

在重新运行产生意外输出的特定调用之前,然后遍历代码。

多种调试方法中的一种方法,请参阅debugbrowserrecover ...

问题很可能来自won始终返回FALSEfour.in.a.row以上return(FALSE)次调用不会使函数退出return(TRUE)。相反,你想让它返回:

return(four.in.a.row(player,row,debug=debug)  ||
       four.in.a.row(player,col,debug=debug)  ||
       four.in.a.row(player,diag,debug=debug) ||
       four.in.a.row(player,reverse_diag,debug=debug))

或者,如果你觉得它更容易理解:

if (four.in.a.row(player,row,debug=debug))          return(TRUE)
if (four.in.a.row(player,col,debug=debug))          return(TRUE)
if (four.in.a.row(player,diag,debug=debug)          return(TRUE)
if (four.in.a.row(player,reverse_diag,debug=debug)) return(TRUE)
return(FALSE)

我还注意到won在使用x参数时正在使用board