0

Copien este código en su R console, denle a enter y escriban lo que les pregunta el código. El juego es "N en raya" (en vez de 3 en Raya, N). "N" viene siendo el número de símbolos consecutivos necesarios para ganar. Ustedes son Player 1 and Player 2. Podrán poner "X", pero cuando vayan a poner la segunda posición, verán el error cuya solución no puedo encontrar. Este es el error: Error in if ((turn == 100) | (Winner1 == "Player1") | (Winner2 == "Player1") | : argumento tiene longitud cero. Soy nueva en esto.

Muchas gracias.

numberSymbols<-function(symbols)
  {
    symbols=readline(prompt="How many consecutive symbols are required to win? ")
   symbols=as.integer(symbols)
   if((symbols==1)|(symbols==2)|(symbols==3)|(symbols==4)|(symbols==5)|(symbols==6)|(symbols==7)|(symbols==8)|(symbols==9)|(symbols==10))
    return(symbols)

  else
     {
      print("Number of symbols incorrect")
      numberSymbols(symbols)
     }
}


Option2<- function()
{   
table=matrix("_", nrow=10, ncol=10)
rownames(table)=c("1","2","3","4","5","6","7","8","9","10")
colnames(table)=c("1","2","3","4","5","6","7","8","9","10")
symbols=numberSymbols(symbols)
Winner1="Anybody"
Winner2="Anybody"
Winner3="Anybody"
Winner4="Anybody"
countingsymbols=0
Player1="Ana"
Player2="Alexia"
turn=1

Row<-function(NRowPlayer)
{
NRowPlayer=readline(prompt="Please, insert a row position: ")
NRowPlayer=as.integer(NRowPlayer)
if((NRowPlayer==1)|(NRowPlayer==2)|(NRowPlayer==3)|(NRowPlayer==4)|(NRowPlayer==5)|(NRowPlayer==6)|(NRowPlayer==7)|(NRowPlayer==8)|(NRowPlayer==9)|(NRowPlayer==10)) return(NRowPlayer)
else
   {   repeat
        {  print("Number of row incorrect")
           NRowPlayer=readline(prompt="Please, insert again a row position: ") 
    NRowPlayer=as.integer(NRowPlayer)
           if((NRowPlayer==1)|(NRowPlayer==2)|(NRowPlayer==3)|(NRowPlayer==4)|(NRowPlayer==5)|(NRowPlayer==6)|(NRowPlayer==7)|(NRowPlayer==8)|(NRowPlayer==9)|(NRowPlayer==10)) break
                }
                              }
}

Column<-function(NColumnPlayer)
{
NColumnPlayer=readline(prompt="Please, insert a column position: ")
NColumnPlayer=as.integer(NColumnPlayer)
if((NColumnPlayer==1)|(NColumnPlayer==2)|(NColumnPlayer==3)|(NColumnPlayer==4)|(NColumnPlayer==5)|(NColumnPlayer==6)|(NColumnPlayer==7)|(NColumnPlayer==8)|(NColumnPlayer==9)|(NColumnPlayer==10)) return(NColumnPlayer)
else
   { repeat
      { print("Number of column incorrect")
        NColumnPlayer=readline(prompt="Please, insert again a column position: ") 
    NColumnPlayer=as.integer(NColumnPlayer)
        if((NRowPlayer==1)|(NRowPlayer==2)|(NRowPlayer==3)|(NRowPlayer==4)|(NRowPlayer==5)|(NRowPlayer==6)|(NRowPlayer==7)|(NRowPlayer==8)|(NRowPlayer==9)|(NRowPlayer==10)) break
                           }
                                      }
}



CheckWinnerXhorizontal<-function(Winner1)
{
r=1
c=1
Winner1="Anybody"
while(r<=nrow(table))
{
countingsymbols=0
    while(c<=ncol(table))
        {
        if(table[r,c]=="X") 
            {
            countingsymbols=countingsymbols+1
            if(countingsymbols==symbols)
                {cat("The winner is", Player1, "\n"); Winner1=Player1; r=11; c=11; return(Winner1)} else cat(" \n")} else countingsymbols=0; c=c+1   }
    r=r+1
    c=1

    }

}


CheckWinnerXvertical<-function(Winner2)
{
c =1
r=1
Winner2="Anybody"
while(c<=ncol(table))
{
countingsymbols=0
    while(r<=nrow(table))
        {
        if(table[r,c]=="X") 
            {
            countingsymbols=countingsymbols+1
            if(countingsymbols==symbols)
                {cat("The winner is", Player1, "\n");Winner2=Player1; r=11; c=11; return(Winner2)} else cat("\n ")} else countingsymbols=0; r=r+1   }
    r=1
    c=c+1
    }
}

CheckWinnerOhorizontal<-function(Winner3)
{
r=1
c=1
Winner3="Anybody"
while(r<=nrow(table))
{
countingsymbols=0
    while(c<=ncol(table))
        {
        if(table[r,c]=="O") 
            {
            countingsymbols=countingsymbols+1
            if(countingsymbols==symbols)
                {cat("The winner is", Player2, "\n");Winner3=Player2; r=11; c=11;return(Winner3)} else cat(" \n")} else countingsymbols=0; c=c+1   }
    r=r+1
    c=1

    }
}

CheckWinnerOvertical<-function(Winner4)
{
c=1
r=1
Winner4="Anybody"
while(c<=ncol(table))
{
countingsymbols=0
    while(r<=nrow(table))
        {
        if(table[r,c]=="O") 
            {
            countingsymbols=countingsymbols+1
            if(countingsymbols==symbols)
                {cat("The winner is ", Player2, "\n"); Winner4=Player2; r=11; c=11; return(Winner4)} else cat(" \n")} else countingsymbols=0; r=r+1   }
    r=1
    c=c+1

    }
}

repeat
{
if(turn%%2!=0)
  {
NRowPlayer=Row(NRowPlayer)
NColumnPlayer=Column(NColumnPlayer)

if(table[NRowPlayer,NColumnPlayer]!="_")
  {
  repeat
{ print("The square is not empty. Please, insert another coordinates")
    NRowPlayer=readline(prompt="Please, insert a row position: ")
    if((NRowPlayer==1)|(NRowPlayer==2)|(NRowPlayer==3)|(NRowPlayer==4)|(NRowPlayer==5)|(NRowPlayer==6)|(NRowPlayer==7)|(NRowPlayer==8)|(NRowPlayer==9)|(NRowPlayer==10)) cat(" ")
    else
       { repeat
           { print("Number of row incorrect")
             NRowPlayer=readline(prompt="Please, insert again a row position: ") 
             if((NRowPlayer==1)|(NRowPlayer==2)|(NRowPlayer==3)|(NRowPlayer==4)|(NRowPlayer==5)|(NRowPlayer==6)|(NRowPlayer==7)|(NRowPlayer==8)|(NRowPlayer==9)|(NRowPlayer==10)) break
                   }
                              }

    NColumnPlayer=readline(prompt="Please, insert a column position: ")
    if((NColumnPlayer==1)|(NColumnPlayer==2)|(NColumnPlayer==3)|(NColumnPlayer==4)|(NColumnPlayer==5)|(NColumnPlayer==6)|(NColumnPlayer==7)|(NColumnPlayer==8)|(NColumnPlayer==9)|(NColumnPlayer==10)) cat(" ")
    else
      { repeat
         { print("Number of column incorrect")
           NColumnPlayer=readline(prompt="Please, insert again a column position: ") 
           if((NRowPlayer==1)|(NRowPlayer==2)|(NRowPlayer==3)|(NRowPlayer==4)|(NRowPlayer==5)|(NRowPlayer==6)|(NRowPlayer==7)|(NRowPlayer==8)|(NRowPlayer==9)|(NRowPlayer==10)) break
                       }
                             }
 if((table[NRowPlayer,NColumnPlayer]!="X")&(table[NRowPlayer,NColumnPlayer]!="O")) break
          }     }  


 table[NRowPlayer,NColumnPlayer]="X"
print(table)
Winner1=CheckWinnerXhorizontal(Winner1)
Winner2=CheckWinnerXvertical(Winner2)
   }
else
  {
NRowPlayer=Row(NRowPlayer)
NColumnPlayer=Column(NColumnPlayer)

if(!table[NRowPlayer,NColumnPlayer]=="_")
  {
  repeat
{ print("The square is not empty. Please, insert another coordinates")
    NRowPlayer=readline(prompt="Please, insert a row position: ")
    if((NRowPlayer==1)|(NRowPlayer==2)|(NRowPlayer==3)|(NRowPlayer==4)|(NRowPlayer==5)|(NRowPlayer==6)|(NRowPlayer==7)|(NRowPlayer==8)|(NRowPlayer==9)|(NRowPlayer==10))
       cat(" ")
    else
       { repeat
           { print("Number of row incorrect")
             NRowPlayer=readline(prompt="Please, insert again a row position: ") 
             if((NRowPlayer==1)|(NRowPlayer==2)|(NRowPlayer==3)|(NRowPlayer==4)|(NRowPlayer==5)|(NRowPlayer==6)|(NRowPlayer==7)|(NRowPlayer==8)|(NRowPlayer==9)|(NRowPlayer==10)) break
                   }
                              }

    NColumnPlayer=readline(prompt="Please, insert a column position: ")
    if((NColumnPlayer==1)|(NColumnPlayer==2)|(NColumnPlayer==3)|(NColumnPlayer==4)|(NColumnPlayer==5)|(NColumnPlayer==6)|(NColumnPlayer==7)|(NColumnPlayer==8)|(NColumnPlayer==9)|(NColumnPlayer==10))
        cat(" ")
    else
      { repeat
         { print("Number of column incorrect")
           NColumnPlayer=readline(prompt="Please, insert again a column position: ") 
           if((NRowPlayer==1)|(NRowPlayer==2)|(NRowPlayer==3)|(NRowPlayer==4)|(NRowPlayer==5)|(NRowPlayer==6)|(NRowPlayer==7)|(NRowPlayer==8)|(NRowPlayer==9)|(NRowPlayer==10)) break
                       }
                             }
 if((table[NRowPlayer,NColumnPlayer]!="X")&(table[NRowPlayer,NColumnPlayer]!="O")) break
          }     }  


 table[NRowPlayer,NColumnPlayer]="O"
print(table)
Winner3=CheckWinnerOhorizontal(Winner3)
Winner4=CheckWinnerOvertical(Winner4)
  }

turn=turn+1
if((turn==100)|(Winner1==Player1)|(Winner2==Player1)|(Winner3==Player2)|(Winner4==Player2)) break
}

}
Option2()

1 respuesta 1

0

Aparentemente el error está en el último condicional de la función, seguramente porque alguna de las condiciones que evalúa debe tener alguna variable nula. Yo vi que todas estaban inicializadas, pero alguna de las funciones debe asignar un valor NULL inserta el siguiente código justo arriba de turn = turn + 1

    print(turn)
    print(Winner1)
    print(Winner2)
    print(Winner3)
    print(Winner4)
    print(Player1)
    print(Player2)

Y verifica el valor que tiene cada variable antes del condicional

1
  • Ya lo corregí. Eso era, gracias!
    – Ana Lib
    Commented el 29 mar. 2020 a las 21:25

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.