2012-12-27 5 views
0

단어 검색을 코딩하고/단어 생성기를 찾으려고하는데 일부 부품이 붙어 있습니다. 이 코드의 목적은 사용자 입력에서 차원과 단어를 가져 와서 임의의 단어 검색/단어 찾기를 만드는 것입니다.단어 검색/단어 생성기 찾기 Python

문제는 문자가 때때로 서로를 덮어 쓰는 것입니다. '-'

lines[0][randCoO[0] - (i)][randCoO[1]] == '-': 

그 라인은 기능에 속한 U

공간이 채워 (비어있는 경우 테스트하기위한 것입니다

A : 시도하고이를 중지하려면 내 기능의 코드 줄이

,536,913,632 : 나는 시도하고 읽을 아마 길고 성가신로 내 전체 코드를 넣어 자제하지만,이 내 코드 시도하고 나에게

도와주세요하는데있어 알고)

10

try: 
    dim=int(raw_input('How many letters high and long do you want your find-a-word? (It has to be <= 79) ')) 
except ValueError: 
     raise SystemExit, 'Dude, thats not a number!' 

if dim > 79: 
    raise SystemExit, "\n That number is bigger than 79. It won't print to well. \n" 


count=0 

allwords = raw_input('What words do you want in your find-a-word?\n(Enter them all together seperated by a comma)') 

allwords = allwords.upper() 
allwords = allwords.split(', ') 
nowords = len(allwords) 

while count < nowords: 
    if len(allwords[count])>dim: 
     raise SystemExit, "\nYour find-a-word is not big enough for one or more of your words.\nIt is doomed to fail! " 
    count = count + 1 

    randletter = [] 
    import random 
    randrange = random.randrange 
    alphabet = ['A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] 

    all_words = ' '.join(allwords) 

    print '\n\nTry to find these words in the word find-a-word\n\n' 
    count = 0 
    while count < nowords: 
     print allwords[count],'\n' 
     count=count+1 

     drctn = ['U','D','L','R','UL','UR','DL','DR'] 

     randdrctnlist = [] 

for i in range(nowords): 
    randdrctn = drctn[randrange(0,4)] 
    randdrctnlist.append(randdrctn) 

    d = {} 

for i in range(dim): 
    d['line' + str(i + 1)] = ['-']*dim 

    lines = [] 
    lines.append(d.values()) 
    lines.sort() 

    CoO1 = [] 
    CoO2 = [] 
    CoO = [] 

for i in range(dim): 
    CoO1.append(i) 
    CoO2.append(i)  

    CoO.append(CoO1) 
    CoO.append(CoO2) 

    count = 0 

def U(word, wordno, count, dim, lines): 
    stop = 0 
    randCoO = [] 
    while count == 0: 
     rand = CoO[0][randrange(0,dim)] 
     randCoO.append(rand) 
     rand = CoO[1][randrange(0,dim)] 
     randCoO.append(rand) 
     for i in range(len(word[wordno])): 
      if randCoO[0] >= (len(word[wordno]) - 1) and lines[0][randCoO[0] - (i)][randCoO[1]] == '-': 
       count = 1    
       lines[0][randCoO[0]][randCoO[1]] = word[wordno][0] 
       while count < len(allwords[wordno]): 
        lines[0][randCoO[0] - (count)][randCoO[1]] = word[wordno][count] 
        count = count + 1     
      else: 
       print randCoO 
       randCoO = [] 
       stop = stop + 1 
       print stop 
       if stop > dim*dim: 
        raise SystemExit, "\nThe find-a-word is probably impossible to make\nwithout overwriting already placed letters.\nPlease try it again. If this has show up many times in a row try making the dimensions of you find-a-word bigger\n(Or you're just unlucky!)\n" 
       break 

def D(word, wordno, count, dim, lines): 
    stop = 0 
    randCoO = [] 
    while count == 0: 
     rand = CoO[0][randrange(0,dim)] 
     randCoO.append(rand) 
     rand = CoO[1][randrange(0,dim)] 
     randCoO.append(rand) 
     for i in range(len(word[wordno])): 
      if dim - randCoO[0] > len(word[wordno]) - 1 and lines[0][randCoO[0] + (i)][randCoO[1]] == '-': 
       count = 1 
       lines[0][randCoO[0]][randCoO[1]] = word[wordno][0] 
       while count < len(allwords[wordno]): 
        lines[0][randCoO[0] + (count)][randCoO[1]] = word[wordno][count] 
        count = count + 1 
      else: 
       print randCoO 
       randCoO = [] 
       stop = stop + 1 
       print stop 
       if stop > dim*dim: 
        raise SystemExit, "\nThe find-a-word is probably impossible to make\nwithout overwriting already placed letters.\nPlease try it again. If this has show up many times in a row try making the dimensions of you find-a-word bigger\n(Or you're just unlucky!)\n" 
       break 

def L(word, wordno, count, dim, lines): 
    stop = 0 
    randCoO = [] 
    while count == 0: 
     rand = CoO[0][randrange(0,dim)]        
     randCoO.append(rand) 
     rand = CoO[1][randrange(0,dim)] 
     randCoO.append(rand) 
     for i in range(len(word[wordno])): 
      if dim - randCoO[1] > (len(word[wordno]) - 1) and lines[0][randCoO[0]][randCoO[1] + (i)] == '-': 
       count = 1 
       lines[0][randCoO[0]][randCoO[1]] = word[wordno][0] 
       while count < len(allwords[wordno]): 
        lines[0][randCoO[0]][randCoO[1] + (count)] = word[wordno][count] 
        count = count + 1 
      else: 
       print randCoO 
       randCoO = [] 
       stop = stop + 1 
       print stop 
       if stop > dim*dim: 
        raise SystemExit, "\nThe find-a-word is probably impossible to make\nwithout overwriting already placed letters.\nPlease try it again. If this has show up many times in a row try making the dimensions of you find-a-word bigger\n(Or you're just unlucky!)\n" 
       break 

def R(word, wordno, count, dim, lines): 
    stop = 0 
    randCoO = [] 
    while count == 0: 
     rand = CoO[0][randrange(0,dim)]        
     randCoO.append(rand) 
     rand = CoO[1][randrange(0,dim)] 
     randCoO.append(rand) 
     for i in range(len(word[wordno])): 
      if randCoO[1] >= (len(word[wordno]) - 1) and lines[0][randCoO[0]][randCoO[1] + (i)] == '-': 
       count = 1 
       lines[0][randCoO[0]][randCoO[1]] = word[wordno][0] 
       while count < len(allwords[wordno]): 
        lines[0][randCoO[0]][randCoO[1] - (count)] = word[wordno][count] 
        count = count + 1 
      else: 
       print randCoO 
       randCoO = [] 
       print stop 
       stop = stop + 1 
       if stop > dim*dim: 
        raise SystemExit, "\nThe find-a-word is probably impossible to make\nwithout overwriting already placed letters.\nPlease try it again. If this has show up many times in a row try making the dimensions of you find-a-word bigger\n(Or you're just unlucky!)\n" 
       break 

for i in range(nowords): 
    count = 0 
    print randdrctnlist 
    if randdrctnlist[i] == 'U': 
     U(allwords, i, count, dim, lines) 
    if randdrctnlist[i] == 'D': 
     D(allwords, i, count, dim, lines) 
    if randdrctnlist[i] == 'L': 
     L(allwords, i, count, dim, lines) 
    if randdrctnlist[i] == 'R': 
     R(allwords, i, count, dim, lines) 

for i in range(dim): 
    print ' '.join(lines[0][i]) 
+0

모두 똑같은 것으로 보이는 4 가지 기능이 있습니다 (사소한 차이가 아닌 경우). 공통 코드를 단일 기능으로 리팩터링해야합니다. –

+0

안녕하세요 - 지나치게 비판적으로 보려고하지는 않지만 분명히 숙제 문제이며 언젠가 다른 사람에게 유용 할 수있는 특정 질문을 공식화하려고해야합니다. 또한 변수 이름을 사용하면 코드를 이해하기가 어려워집니다 (예 : 'num_words'대신 'nowords'). 즉, 나는 너에게 조금의 도움을 줄 수 있는지 살펴볼 것이다. –

+0

아! 80 자 라인의 장점! – joaquin

답변

1

나는 기본적인 알고리즘을 다시 생각할 필요가 있다고 생각한다. 코드에서 임의의 방향으로 단어를 만든 다음 충돌을 관리하려고합니다. 대신 모든 단어 사이의 모든 교차점을 고려한 다음 가능한 모든 겹치기를 만들고 결과에서 임의로 선택할 수 있습니다.

예를 들어 'FROG'는 'DOG'와 'GOG'가 교차하며 'G'는 7 방향과 'O'는 6 방향과 교차 할 수 있습니다. 'CAT'와 ('DOG'또는 'FROG')에는 공통 문자가 없으므로 전혀 교차 할 수 없습니다. 지금까지의 '기존 솔루션'의 각 조합을 시도한 다음 재귀 적으로 솔루션을 호출하고 퍼즐의 크기 내에 맞는 가능한 모든 다른 조합에 새 단어를 추가하면 모든 가능한 단어 찾기가 가능합니다. 주어진 단어.

이 치수를 줄이기 위해 먼저 교차하지 않는 단어를 배치해야한다고 생각합니다. 모두 맞지 않으면 오류가 발생하여 종료 될 수 있습니다. 그런 다음 크기가 내림차순으로 단어를 처리하십시오.

임의로 선택하거나 해결책을 찾자 마자 종료 할 수 있습니다. 답을 다른 특성 (예 : 전체 치수를 더 많이 사용하도록 펼쳐지는 것처럼)을 원할 경우, 조합을 시도하는 방식을 재배치하고 주어진 첫 번째 솔루션을 선택할 수 있다고 생각합니다.

+0

모두 감사합니다 :) – user1912132