2012-12-27 62 views
0

我想编码单词搜索/找到一个单词生成器,我被困在一些部分。此代码的目的是从用户输入中提取维度和单词,并生成随机单词搜索/查找单词。单词搜索/查找单词生成器蟒蛇

问题是,这些字母偶尔会互相覆盖。试图阻止这个,我有我的功能这行代码:

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

该行所属的函数U

其目的是测试如果空间是空的(通过填充的“ - ” )

我知道我应该尝试和把我的整个代码,因为它可能是漫长而令人讨厌的阅读,但不要请尽量帮助我

这是我的代码:

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

你有四个函数看起来完全相同(如果不是,则有细微差别) - 你应该将通用代码重构为单个函数。 –

+0

嘿,伙计 - 不要过于苛刻,但这显然是一个家庭作业问题,你需要尝试制定一个具体的问题,可能有一天对别人有用。你的变量名也会让你很难理解你的代码,例如'nowords'而不是'num_words'。这就是说,我会看看能否给你一点帮助。 –

+0

啊! 80字符线的好处! – joaquin

回答

1

我认为你需要重新思考基本算法。你的代码试图在随机方向创建单词,然后管理冲突。相反,您可以考虑所有单词之间的所有交叉点,然后构建所有可能的重叠,并从结果中随机选择。例如:'FROG'用两个字母与'DOG'相交,因此它们可以在'G'和'O'上从7个方向相交。 'CAT'和('DOG'或'FROG')没有共同的字母,所以它们将不能相交。如果您尝试每种'现有解决方案到目前为止'的每种组合,然后递归地调用您的解决方案并添加一个新的单词,以适合谜题尺寸的所有可能的其他组合,您将可以找到所有可能的单词给定的话。

为了减少这种维度,我认为你应该首先放置不相交的单词。如果他们不适合,可以退出时出错。然后按大小顺序处理单词。

然后,您可以随机挑选,也可以在找到解决方案时立即退出。如果你想让你的答案具有不同的特征(比如更广泛地使用整个给定的尺寸),我认为你可以重新排列你尝试组合的方式,并选择第一个解决方案。

+0

谢谢大家:) – user1912132