Question

I have the following program:

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))

And I receive this error (while I don't expect it):

> 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

Is there a quick way to step through/breakpoint stopifnot or any other method to trace what's wrong? Beside do you know why I get the wrong answer?

Était-ce utile?

La solution

You could do

debugonce(won)

before rerunning the particular call that gives an unexpected output, and walk through the code.

That's one approach among multiple debug methods, see debug, browser, recover...

Most likely the problem comes from won always returning FALSE: the four.in.a.row calls above return(FALSE) will not make the function exit and return(TRUE). Instead, you want to make it return:

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))

Or this if you find it easier to understand:

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)

I also notice won is using x when it should use the board argument.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top