将矩阵打印到游戏板并识别矩阵中的反向对角线时出错

时间:2014-10-27 15:04:26

标签: r matrix

我试图实施一个" Connect 4" R中的游戏 我首先按如下方式打印一块板:

par(pty="s") # square plot type
x = rep(1:7, each = 6)
y = rep(1:6, times = 7)
symbols(x, y, squares = rep(1, times=42),
        inches=FALSE, # match squares to axes
        xlim=c(0,8),
        ylim=c(8,0),
        axes = FALSE,
        xlab = '', ylab = '') # flip y axis to match matrix format
board = matrix(rep("", 42), nrow = 6, ncol = 7
然后我写了助手功能"赢了"和" four.in.a.row"

four.in.a.row = function(player, v, debug=FALSE) {
  #  TRUE if board contains at least four in a row after play in position (row, column). 
  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, row, col, debug=FALSE) {
  if (debug) {
    cat(sep="", "won(player=", player, ", board=\n")
    print(board)
    cat(sep="", ", row=", row, ", col=", col, ")\n")
  }
  winning.row=board[row,]
  cat("row is = ", winning.row, "\n")
  winning.column=board[,col]
  cat("col is = ", winning.column, "\n")
  winning.reverse.diagonal=board[row(board) + col(board) == row + col]
  cat("reverse diag is = ", winning.reverse.diagonal, "\n")
  winning.diagonal=x[row(board) - col(board) == row - col]
  cat("diag is = ", winning.diagonal, "\n")
  return(four.in.a.row(player,winning.row,debug=TRUE)  ||
           four.in.a.row(player,winning.column,debug=TRUE)  ||
           four.in.a.row(player,winning.diagonal,debug=TRUE) ||
           four.in.a.row(player,winning.reverse.diagonal,debug=TRUE))
}

和largest.empty.row这样游戏棋子就会掉线"掉#34;到所选列的底部

largest.empty.row = function(board, col, debug=TRUE) {
  if (debug) {
    cat(sep="", "largest.empty.row(board=\n")
    print(board)
    cat(sep="", ", col=", col, ")\n")
  }
  if (board[6,col] ==""){

    return (6)
  }
  else if (board[5,col] ==""){
    return (5)
  }
  else if (board[4,col] ==""){
    return (4)
  }
  else if (board[3,col] ==""){
    return (3)
  }
  else if (board[2,col] ==""){
    return (2)
  }
  else if (board[1,col] ==""){
    return (1)
  }
  else if (board[1,col] !=""){
    return (NULL)
  }
}

并运行游戏

repeat { # let current player take a turn
  if (player == "X") { # human player makes move
    repeat { # require player to click on empty square
      index = identify(x, y, n=1) #, plot=FALSE
      col = x[index]
      row=largest.empty.row(board,col)
      if (board[row, col] == "") {
        break
      }
    }
  }  else { # computer player makes random move
    random.col<-sample(1:7, size=1)
    col = random.col
    row=largest.empty.row(board,col)
  }
  board[row, col] = player
  print(board)
  text(x=col, y=largest.empty.row(board,col), labels=player)#make plays visible. At a coordinate, draw a label
  if ((won(player, board, row, col, debug=FALSE)) & player == "X") {
    cat("X WINS!")
    break
  }
  else if ((won(player, board, row, col, debug=FALSE)) & player == "O") {
    cat("O WINS!")
    break
  }
  else if (board.is.full(board)){
    cat("Board is full.")
    break
  }
  player = ifelse(test=(player == "X"), yes="O", no="X")
}

游戏&#34;工作&#34;,有两个例外 - 棋子在棋盘上打印一个空格,虽然矩阵打印正确,但游戏似乎没有识别反向对角线胜利。我对R和编程很新。我已经手动运行了代码&#34;#34;在纸上几次,看不出我可能会出错的地方。任何帮助将不胜感激!

0 个答案:

没有答案