我的单词搜索生成器中只使用了数组中的 1 个单词 [英] Only 1 word from the array is being used in my Word Search generator
问题描述
我在 youtube 上看到了一个单词搜索游戏教程,它在 python 控制台中进行了单词搜索,我试图将它带入 tkinter.我让它工作......有点.问题是在 GUI/程序中只使用了可能的 5 个单词中的 1 个.这在控制台版本中不是问题,因此我将链接两个版本的代码.在此先感谢您的帮助!
I saw a word search game tutorial on youtube that made a word search in the python console and I tried to take that into tkinter. I have it working... sort of. The problem is that only 1 word from the possible 5 is being used in the GUI/Program. This isn't a problem in the console version so I will link both version of code. Thanks in advance for any help!
我的代码:
import tkinter as tk
import random
import string
handle = open('dictionary.txt')
words = handle.readlines()
handle.close()
grid_size = 10
words = [ random.choice(words).upper().strip() \
for _ in range(5) ]
print ("The words are:")
print(words)
grid = [ [ '_' for _ in range(grid_size) ] for _ in range(grid_size) ]
orientations = [ 'leftright', 'updown', 'diagonalup', 'diagonaldown' ]
class Label(tk.Label):
def __init__(self, parent, **kwargs):
super().__init__(parent, **kwargs, font=("Courier", 44))
self.bind('<Button-1>', self.on_click)
class App(tk.Tk):
def __init__(self):
super().__init__()
for row in range(grid_size):
for column in range(grid_size):
for word in words:
word_length = len(word)
placed = False
while not placed:
orientation = random.choice(orientations)
if orientation == 'leftright':
step_x = 1
step_y = 0
if orientation == 'updown':
step_x = 0
step_y = 1
if orientation == 'diagonalup':
step_x = 1
step_y = -1
if orientation == 'diagonaldown':
step_x = 1
step_y = 1
x_position = random.randrange(grid_size)
y_position = random.randrange(grid_size)
ending_x = x_position + word_length*step_x
ending_y = y_position + word_length*step_y
if ending_x < 0 or ending_x >= grid_size: continue
if ending_y < 0 or ending_y >= grid_size: continue
failed = False
for i in range(word_length):
character = word[i]
new_position_x = x_position + i*step_x
new_position_y = y_position + i*step_y
character_at_new_position = grid[new_position_x][new_position_y]
if character_at_new_position != '_':
if character_at_new_position == character:
continue
else:
failed = True
break
if failed:
continue
else:
for i in range(word_length):
character = word[i]
new_position_x = x_position + i*step_x
new_position_y = y_position + i*step_y
grid[new_position_x][new_position_y] = character
if ( grid[row][column] == grid[new_position_x][new_position_y] ):
grid[row][column] = grid[new_position_x][new_position_y]
Label(self, text=character).grid(row=row, column=column)
placed = True
if ( grid[row][column] == '_' ):
txt = random.SystemRandom().choice(string.ascii_uppercase)
Label(self, text=txt).grid(row=row, column=column)
if __name__ == '__main__':
App().mainloop()
原始控制台版本代码:
import random
import string
from pprint import pprint
def mainfunc():
handle = open('dictionary.txt')
words = handle.readlines()
handle.close()
words = [ random.choice(words).upper().strip() \
for _ in range(5) ]
grid_size = 20
grid = [ [ '_' for _ in range(grid_size) ] for _ in range(grid_size) ]
def print_grid():
for x in range(grid_size):
print ('\t' *3 + ' '.join( grid[x] ))
orientations = [ 'leftright', 'updown', 'diagonalup', 'diagonaldown' ]
for word in words:
word_length = len(word)
placed = False
while not placed:
orientation = random.choice(orientations)
if orientation == 'leftright':
step_x = 1
step_y = 0
if orientation == 'updown':
step_x = 0
step_y = 1
if orientation == 'diagonalup':
step_x = 1
step_y = -1
if orientation == 'diagonaldown':
step_x = 1
step_y = 1
x_position = random.randrange(grid_size)
y_position = random.randrange(grid_size)
ending_x = x_position + word_length*step_x
ending_y = y_position + word_length*step_y
if ending_x < 0 or ending_x >= grid_size: continue
if ending_y < 0 or ending_y >= grid_size: continue
failed = False
for i in range(word_length):
character = word[i]
new_position_x = x_position + i*step_x
new_position_y = y_position + i*step_y
character_at_new_position = grid[new_position_x][new_position_y]
if character_at_new_position != '_':
if character_at_new_position == character:
continue
else:
failed = True
break
if failed:
continue
else:
for i in range(word_length):
character = word[i]
new_position_x = x_position + i*step_x
new_position_y = y_position + i*step_y
grid[new_position_x][new_position_y] = character
placed = True
for x in range(grid_size):
for y in range(grid_size):
if ( grid[x][y] == '_' ):
grid[x][y] = random.SystemRandom().choice(string.ascii_uppercase)
print_grid()
print ("The words are:")
pprint(words)
mainfunc()
dictionary.txt"的单词列表:此处
List of words for 'dictionary.txt': here
推荐答案
您有一个小缩进问题.
for word in words:
word_length = len(word)
placed = False
while not placed:
<rest of the code>
....
for 循环遍历每个单词,用于整个列表.完成后,word
已分配到列表中的最后一个单词.然后继续执行代码,只使用列表中的最后一个词.
The for loop is looping over each word, for the WHOLE LIST. When it is done word
has been assigned the last word in the list. It then continues with the code, only using the final word in your list.
只需缩进以开头的代码块
.
for word in words:
word_length = len(word)
placed = False
while not placed:
<rest of the code>
....
如果与您的控制台版本进行比较,您可以看到 while 循环在 for 循环内正确缩进,这就是它正常工作的原因.
If you compare to your console version, you can see the while loop is properly indented inside the for loop, which is why that works correctly.
这篇关于我的单词搜索生成器中只使用了数组中的 1 个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!