I am trying to implement a "Connect 4" game in R. First I print a board:
equals (PTI = "s") # square plot type x = delegate (1: 7, every = 6) y = delegate (1: 6, time = 7) symbols (x, y, intersections) = (1, bar = 42), inch = FALSE, # exclaims = f (0,8), yil = c (8,0), axis = falls, Xlab = '', ylab = '') To match Matrix format board = matrix (Representative (42), NRO = 6, NXL = 7,
# Flip Y axis I "Win" and "Four.in.a.row"
four.in.a.row = Function (player, v, debug = FALSE) {# TRUE If there are at least four in the row after playing the board (row, column). (Cat) (cat = "", "four. In .ero (player = ", player,", v = ", v,") \ n ")} (RL (V), any (length == 4 and value == player))} win = function (Player, board, line, call, debug = FALSE) {if (debug) {cat (sep = "", "won (player =", player, ", board = \ n") (board =), (row = ", Line =", row, ", col =", col, ")) \ n")} Winning. Board = [row,] cat ("row = win.", \ "\ N") winning.column = board [, col] cat ("col =", winning.column, "\ n") is victorious. .diagonal = board [row] + col (board) == line + cola] cat ("reverse die =", winning.versea deagonal, "\ n") victorious.degonal = x [row] Cola (board) == line-colonel] cat ("Diag is =", triumphant, Deogonal, "\ n") Returns (four in. Arrow (player, winning, OO, debug = TRUE) || Four.in .a.row (player, winning.column, debug = TRUE) || Four.in.a.row (bloom D, triumphant, deagonal, debug = TRUE) Four.in.a.row (player, winning.reverse.diagonal, debug = TRUE))}
and the largest. Arrows so that the pieces of the game will be "drop down" to the selected column
largest.mpi.o = function (board, cola, debug = TRUE) {if (debug) {cat (cp = "", "The largest board = \ n") print (board) cat (cp = "", call = ", call,") \ n ")} if (board [6, cola] ==" " } {Return (6)} and if (board [3, cola] = ("[board [5, cola] ==" ") {return (5)} and if (board [4, cola] ==" ) { Return (4)} and if (board [3, ==] "{return (3)} and if (board [2, cola] ==" ") {return (2)} and if (board [1, Cola] == "") {return (1) repeat {#], and if (board [1, cola]! {Return (tap)}}
and run the game
repeat {# (player == "X") {# man When the player makes the driver to repeat the trick, the player should take a turn to pull the player towards the empty player. # The player needs to click on the empty strip indicator = Identity (X, Y, N = 1) #, plot = FALSE col = X [index] Row = biggest Y.row (board, cola) If (board [ Line, cola] == "") {break}}} and {# computer player random move random. Col & lt; -sample (1: 7, size = 1) = Random. Colline = largest. Marti.ro (board, cola)} board [row, cola] = player print (board) text (x = cola, y = largest. Ample.ro (board, cola), labels = player) #make view looks On a coordinate, if (wins (player, board, line, call, debug = FALSE) and player == "X") (Cat ("X wins!" (Player, board, row, kernel, debug = FALSE) and Player == "Hey") (Breaking the cat ("o wins!") And if (board.is full (board)) {cat ("the board is completely (Player == "X"), yes = "o", not = "x")}
game "works" "With two exceptions - pieces are printed up one place on the board, although the matrix prints correctly, and the game does not seem to recognize the reverse diagonal win, I am very new to R and normal For programming in the form. I have run the code "hand" on paper several times And I can not see that I am where being wrong. Would appreciate any help!
Comments
Post a Comment