x [-1L]中的错误!= x [-n]:未实现这些类型的比较

时间:2014-03-23 01:51:05

标签: r function debugging vector

我一直在寻找问题的根源。根据追溯,我猜问题是使用four.in.a.row函数,即使我已经测试过了。

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))
}
# Returns TRUE if (matrix) board (of character strings)
# contains at least four in a row of (string) player, who
# just played in position (r, c). (Here "r" means "row" and
# "c" means "column").
#
# Hint: this function should call four.in.a.row() four times.
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_w=board[r,]
  cat("row is = ", row_w, "\n")
  col_w=board[,c]
  cat("col is = ", col_w, "\n")
  reverse_diag_w=board[row(board) + col(board) == r + c]
  cat("reverse diag is = ", reverse_diag_w, "\n")
  diag_w=x[row(board) - col(board) == r - c]
  cat("diag is = ", diag_w, "\n")
  four.in.a.row(player,row_w,debug=FALSE)
  four.in.a.row(player,col_w,debug=FALSE)
  four.in.a.row(player,diag_w,debug=FALSE)
  four.in.a.row(player,reverse_diag_w,debug=FALSE)
  return(FALSE) # correct this return() statement
}

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"  "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)
row is =  X X X X O E X 
col is =  E E E E E X 
reverse diag is =  X E E E E E 
diag is =  X 
Error: won(player = "X", board = x, r = 6, c = 1, debug = TRUE) is not TRUE

我的主要问题是,当我单独运行four.in.a.row函数时,它会返回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)
> row_x=x[6,]
> row_x
[1] "X" "X" "X" "X" "O" "E" "X"
> four.in.a.row("X",row_x,debug=FALSE)
[1] TRUE

将代码更改为@Flodel告诉我的内容后:

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))
}
# Returns TRUE if (matrix) board (of character strings)
# contains at least four in a row of (string) player, who
# just played in position (r, c). (Here "r" means "row" and
# "c" means "column").
#
# Hint: this function should call four.in.a.row() four times.
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_w=board[r,]
  cat("row is = ", row_w, "\n")
  col_w=board[,c]
  cat("col is = ", col_w, "\n")
  reverse_diag_w=board[row(board) + col(board) == r + c]
  cat("reverse diag is = ", reverse_diag_w, "\n")
  diag_w=board[row(board) - col(board) == r - c]
  cat("diag is = ", diag_w, "\n")
  #four.in.a.row(player,row_w,debug=FALSE)
  #four.in.a.row(player,col_w,debug=FALSE)
  #four.in.a.row(player,diag_w,debug=FALSE)
  #four.in.a.row(player,reverse_diag_w,debug=FALSE)
  #return(FALSE) # correct this return() statement
  return(four.in.a.row(player,row_w,debug=FALSE)  ||
           four.in.a.row(player,col_w,debug=FALSE)  ||
           four.in.a.row(player,diag_w,debug=FALSE) ||
           four.in.a.row(player,reverse_diag_w,debug=FALSE))
}

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"  "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)
row is =  X X X X O E X 
col is =  E E E E E X 
reverse diag is =  X E E E E E 
diag is =  X 
 Show Traceback

 Rerun with Debug
 Error in x[-1L] != x[-n] : comparison of these types is not implemented 

这里是追溯的结果:

Error in x[-1L] != x[-n] : comparison of these types is not implemented 
9 rle(v) 
8 with(rle(v), any(lengths == 4 & values == player)) at rle.R#5
7 four.in.a.row(player, diag, debug = FALSE) at rle.R#32
6 won(player = "X", board = x, r = 6, c = 1, debug = TRUE) 
5 stopifnot(won(player = "X", board = x, r = 6, c = 1, debug = TRUE)) at rle.R#46
4 eval(expr, envir, enclos) 
3 eval(ei, envir) 
2 withVisible(eval(ei, envir)) 
1 source("rle.R") 

1 个答案:

答案 0 :(得分:1)

这要归功于Flodel的指南:

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))
}
# Returns TRUE if (matrix) board (of character strings)
# contains at least four in a row of (string) player, who
# just played in position (r, c). (Here "r" means "row" and
# "c" means "column").
#
# Hint: this function should call four.in.a.row() four times.
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_w=board[r,]
  cat("row is = ", row_w, "\n")
  col_w=board[,c]
  cat("col is = ", col_w, "\n")
  reverse_diag_w=board[row(board) + col(board) == r + c]
  cat("reverse diag is = ", reverse_diag_w, "\n")
  diag_w=x[row(board) - col(board) == r - c]
  cat("diag is = ", diag_w, "\n")
  #four.in.a.row(player,row_w,debug=FALSE)
  #four.in.a.row(player,col_w,debug=FALSE)
  #four.in.a.row(player,diag_w,debug=FALSE)
  #four.in.a.row(player,reverse_diag_w,debug=FALSE)
  #return(FALSE) # correct this return() statement
  return(four.in.a.row(player,row_w,debug=debug)  ||
           four.in.a.row(player,col_w,debug=debug)  ||
           four.in.a.row(player,diag_w,debug=debug) ||
           four.in.a.row(player,reverse_diag_w,debug=debug))
}

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"  "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)
row is =  X X X X O E X 
col is =  E E E E E X 
reverse diag is =  X E E E E E 
diag is =  X 
four.in.a.row(player=X, v=XXXXOEX)
won(player=O, 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)
row is =  X X X X O E X 
col is =  E E E E E X 
reverse diag is =  X E E E E E 
diag is =  X 
four.in.a.row(player=O, v=XXXXOEX)
four.in.a.row(player=O, v=EEEEEX)
four.in.a.row(player=O, v=X)
four.in.a.row(player=O, v=XEEEEE)
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=1, c=7)
row is =  E E E E E E O 
col is =  O O O O X X 
reverse diag is =  X E E E E O 
diag is =  O 
four.in.a.row(player=X, v=EEEEEEO)
four.in.a.row(player=X, v=OOOOXX)
four.in.a.row(player=X, v=O)
four.in.a.row(player=X, v=XEEEEO)
won(player=O, 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=1, c=7)
row is =  E E E E E E O 
col is =  O O O O X X 
reverse diag is =  X E E E E O 
diag is =  O 
four.in.a.row(player=O, v=EEEEEEO)
four.in.a.row(player=O, v=OOOOXX)

当我给一个长度小于4的向量然后比较它以查看它是否包含rle或{的长度为4的向量时,我大多担心X将无效。 {1}}。如果答案不正确,请告诉我。