Как отследить корень stopifnot в R?
У меня есть следующая программа:
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
Есть ли быстрый способ пройти через / остановку stopifnot или любой другой метод, чтобы отследить, что не так? Кроме того, вы знаете, почему я получаю неправильный ответ?
1 ответ
Решение
Вы могли бы сделать
debugonce(won)
перед повторным запуском конкретного вызова, который дает неожиданный вывод, и пройдитесь по коду.
Это один подход среди нескольких методов отладки, см. debug
, browser
, recover
...
Скорее всего проблема исходит от won
всегда возвращаюсь FALSE
: four.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
аргумент.