R/boardsize.R

Defines functions guess_boardsize

guess_boardsize <- function(boardsize, maxcoord)
{
  # guess boardsize, if needed
  #
  # Args:
  #   boardsize : character or numeric. current boardsize
  #   maxcoord  : maximum coordinate
  # Returns:
  #   integer

  if (!is.numeric(boardsize) && !is.character(boardsize))
    stop("boardsize must be character or number")

  # take the first element, just in case a vector is given
  boardsize <- stringr::str_extract(boardsize[1], "[0-9]+") %>% as.integer()
  if (is.na(boardsize)) {
    if (maxcoord > 19L)
      stop("Coordinates exceed 19, but the boardsize is not specified")
    cat("board size is set to 19\n")
    boardsize <- 19L
  } else if (boardsize < maxcoord) {
    warning("Coordinates exceed the specified board size")
    if (maxcoord <= 9L) {
      boardsize <- 9L
    } else if (maxcoord <= 13L) {
      boardsize <- 13L
    } else if (maxcoord <= 19L) {
      boardsize <- 19L
    } else {
      stop("Coordinates exceed 19, but the given boardsize is smaller")
    }
    cat("board size is set to", boardsize, "\n")
  }
  return(boardsize)
}

Try the gogamer package in your browser

Any scripts or data that you put into this service are public.

gogamer documentation built on May 29, 2017, 4:10 p.m.