단어 검색을 코딩하고/단어 생성기를 찾으려고하는데 일부 부품이 붙어 있습니다. 이 코드의 목적은 사용자 입력에서 차원과 단어를 가져 와서 임의의 단어 검색/단어 찾기를 만드는 것입니다.단어 검색/단어 생성기 찾기 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])
모두 똑같은 것으로 보이는 4 가지 기능이 있습니다 (사소한 차이가 아닌 경우). 공통 코드를 단일 기능으로 리팩터링해야합니다. –
안녕하세요 - 지나치게 비판적으로 보려고하지는 않지만 분명히 숙제 문제이며 언젠가 다른 사람에게 유용 할 수있는 특정 질문을 공식화하려고해야합니다. 또한 변수 이름을 사용하면 코드를 이해하기가 어려워집니다 (예 : 'num_words'대신 'nowords'). 즉, 나는 너에게 조금의 도움을 줄 수 있는지 살펴볼 것이다. –
아! 80 자 라인의 장점! – joaquin