How can I catch checking the diagonals of a word search?

Asked

Viewed 162 times

-1

I’ve already checked some codes here on the site, but I’m not getting it. I am creating a program that receives a word hunt and shows in the terminal where the previously declared words are. However, I’m having trouble checking diagonally now. I have already tried to turn each diagonal line into a horizontal line, and so check if any word is present there.

I’m starting now in programming, I imagine my code is full of "errors", but I hope to improve. Thanks in advance.

I’ll post the entire code here:

##### CAÇA PALAVRAS #####
print("Caça Palavras")

# Funções:
def definirCaçaPalavra():
  indexLinhas = int(input("Quantas linhas serão necessárias? "))
  for i in range(0, indexLinhas):
    linha = input("Digite a {}ª linha: ".format(i+1))
    linhaLista = list(linha)
    caçaPalavras.append(linhaLista)

def definirPalavras():
  indexPalavras = int(input("Quantas palavras serão procuradas? "))
  for i in range(0, indexPalavras):
    palavra = input("Digite a {}ª palavra: ".format(i+1))
    palavraLista = list(palavra)
    palavras.append(palavraLista)

def verificarHorizontal():
  for palavra in palavras:
    numeroLinha = 1
    for linha in caçaPalavras:
      linhaString = ''.join(linha)
      palavraString = ''.join(palavra)
      if palavraString in linhaString:
        print("Palavra {} encontrada na horizontal na {}ª linha".format(palavraString, numeroLinha))
      else:
        numeroLinha += 1

def verificarHorizontalInversa():
  for palavra in palavras:
    numeroLinha = 1
    for linha in caçaPalavras:
      linhaString = ''.join(linha)
      palavraString = ''.join(palavra)
      palavraInversaString = ''.join(reversed(palavra))
      if palavraInversaString in linhaString:
        print("Palavra {} ({}) encontrada na horizontal inversa na {}ª linha".format(palavraInversaString, palavraString, numeroLinha))
      else:
        numeroLinha += 1

def verificarVertical():
  tamanhoLinha = len(caçaPalavras[0])
  for i in range(0, tamanhoLinha):
    linhaHorizontalVertical = []
    for linha in caçaPalavras:
      linhaHorizontalVertical.append(linha[i])
    linhaHorizontalVerticalString = ''.join(linhaHorizontalVertical)
    for palavra in palavras:
      palavraString = ''.join(palavra)
      if palavraString in linhaHorizontalVerticalString:
        print("Palavra {} encontrada na {}ª vertical".format(palavraString, i+1))

def verificarVerticalInversa():
  tamanhoLinha = len(caçaPalavras[0])
  for i in range(0, tamanhoLinha):
    linhaHorizontalVertical = []
    for linha in caçaPalavras:
      linhaHorizontalVertical.append(linha[i])
    linhaHorizontalVerticalString = ''.join(linhaHorizontalVertical)
    for palavra in palavras:
      palavraInversaString = ''.join(reversed(palavra))
      palavraString = ''.join(palavra)
      if palavraInversaString in linhaHorizontalVerticalString:
        print("Palavra {} ({}) encontrada na {}ª vertical inversa".format(palavraInversaString, palavraString, i+1))
# Transformar linha diagonal em uma horizontal        

For each letter, pick from the low line size +1.

def verificarDiagonalBaixo():
  

# Variáveis:
caçaPalavras = []
palavras = []

# Principal
definirCaçaPalavra()
definirPalavras()
print("********************")
verificarHorizontal()
print("********************")
verificarHorizontalInversa()
print("********************")
verificarVertical()
print("********************")
verificarVerticalInversa()
print("********************")
verificarDiagonalBaixo()

One of the things I tried but didn’t work was:

def verificarDiagonalBaixo():
  for linha in caçaPalavras:
    for letra in linha:
      contador = 1
      linhaHorizontalDiagonal = []
      linhaHorizontalDiagonal.append(letra)
      for linha in caçaPalavras:
        if contador < len(linha):
          linhaHorizontalDiagonal.append(linha[contador])
        contador += 1
      print(linhaHorizontalDiagonal)

I think maybe it’s an idea of what I’m trying to do.

This here I tried too, and he took the first diagonal. Maybe I’m on the right track:

def verificarDiagonalBaixo():
  for linha in caçaPalavras:
    
      contador = 0
      linhaHorizontalDiagonal = []
      for linha in caçaPalavras:
        if contador < len(linha):
          linhaHorizontalDiagonal.append(linha[contador])
        contador += 1
  print(linhaHorizontalDiagonal)

1 answer

0

let’s assume you want to look at the diagonal from top to bottom, from left to right. You can’t do a for diagonal in ... Because there is nowhere to look for the diagonal, then we will have to start looking at the first letter on the line 0 column 0 (0,0), the second on line 1, column 1 (1,1), etc. If the word has N letters we end in (N-1,N-1). Right? If we haven’t found the word, let’s start searching now with the first letter in line 0 column 1 (0.1) and follow up (0,1)+(N-1,N-1)=(N-1,N). There is a limit that (0,i)+(N-1,N-1) can’t leave the edge of your line, so N-1+i must be less than or equal to len(caçaPalavras[0]), that is to say i<len(caçaPalavras[0])-N.

Let’s start the code. Assuming your word has 4 letters the code below will look at the first 4 letters of the diagonal starting at (0.0):

diagonal = [caçaPalavras[k][k] for k in range(4)]

If we want to get her started on (0,i) and use an N word so let’s do:

N = len(palavra)
for i in range(len(caçaPalavras[0])-N):
    diagonal = [caçaPalavras[k][k+i] for k in range(N)]

to look at the other lines you will need another for and exchange the code above for

N = len(palavra)
for j in range(len(caçapalavras)-N:
    for i in range(len(caçaPalavras[j])-N):
        diagonal = [caçaPalavras[k+j][k+i] for k in range(N)]

what you want to compare is if your word equals to ''.join(diagonal). For the diagonals in other senses you need to exchange the signs in [k+i][k+j] and set the limits of his for But if I tell you here how to do it I’ll be solving the problem for you. I think from here you can already break your head to solve. Any questions ask in the comments.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.