gpt4 book ai didi

python - 在矩阵中生成数独谜题而不是绘制 GUI

转载 作者:行者123 更新时间:2023-12-04 09:01:28 24 4
gpt4 key购买 nike

该程序的概念是创建一个可以通过 pygame 解决的数独谜题。我被卡住的地方是这个错误:

Traceback (most recent call last):
File "C:\Users\Lenovo\Desktop\Sudoku Solver\GUI.py", line 307, in <module>
main()
File "C:\Users\Lenovo\Desktop\Sudoku Solver\GUI.py", line 245, in main
board = Grid(9, 9, 540, 540)
File "C:\Users\Lenovo\Desktop\Sudoku Solver\GUI.py", line 105, in __init__
self.cubes = [[Cube(self.board[i][j], i, j, width, height) for j in range(cols)] for i in range(rows)]
File "C:\Users\Lenovo\Desktop\Sudoku Solver\GUI.py", line 105, in <listcomp>
self.cubes = [[Cube(self.board[i][j], i, j, width, height) for j in range(cols)] for i in range(rows)]
File "C:\Users\Lenovo\Desktop\Sudoku Solver\GUI.py", line 105, in <listcomp>
self.cubes = [[Cube(self.board[i][j], i, j, width, height) for j in range(cols)] for i in range(rows)]
TypeError: 'int' object is not subscriptable
[Finished in 0.6s with exit code 1]
但有趣的是,当我传入一个手工制作的矩阵时,没有错误。我可以复制生成的矩阵并将其传入,它会起作用,但制作一个像下面这样的函数来生成一个不会。
def generate_board():
base = 3
side = base*base

def pattern(r,c): return (base*(r%base)+r//base+c)%side

from random import sample
def shuffle(s): return sample(s,len(s))
rBase = range(base)
rows = [ g*base + r for g in shuffle(rBase) for r in shuffle(rBase) ]
cols = [ g*base + c for g in shuffle(rBase) for c in shuffle(rBase) ]
nums = shuffle(range(1,base*base+1))

# produce board using randomized baseline pattern
board = [ [nums[pattern(r,c)] for c in cols] for r in rows ]

squares = side*side
empties = squares * 3//4
for p in sample(range(squares),empties):
board[p//side][p%side] = 0

numSize = len(str(side))
for line in board: return line


generated_board = generate_board()
为了更好地了解程序,请查看问题所在的部分,即为每个 self.cubes 创建一个立方体对象的 GRID 类的初始化:
class Grid:
board = generated_board


def __init__(self, rows, cols, width, height):
self.rows = rows
self.cols = cols
self.cubes = [[Cube(self.board[i][j], i, j, width, height) for j in range(cols)] for i in range(rows)]
self.width = width
self.height = height
self.model = None
self.selected = None
我不认为这是必需的,但这是在 Grid 类的初始化中以 9x9 方式创建的 Cube 类。
    class Cube:
rows = 9
cols = 9

def __init__(self, value, row, col, width ,height):
self.value = value
self.temp = 0
self.row = row
self.col = col
self.width = width
self.height = height
self.selected = False

def draw(self, win):
fnt = pygame.font.SysFont("comicsans", 40)

gap = self.width / 9
x = self.col * gap
y = self.row * gap

if self.temp != 0 and self.value == 0:
text = fnt.render(str(self.temp), 1, (128,128,128))
win.blit(text, (x+5, y+5))
elif not(self.value == 0):
text = fnt.render(str(self.value), 1, (0, 0, 0))
win.blit(text, (x + (gap/2 - text.get_width()/2), y + (gap/2 - text.get_height()/2)))

if self.selected:
pygame.draw.rect(win, (255,0,0), (x,y, gap ,gap), 3)

def set(self, val):
self.value = val

def set_temp(self, val):
self.temp = val
所以再次传入一个板子
  board = [
[7, 8, 0, 4, 0, 0, 1, 2, 0],
[6, 0, 0, 0, 7, 5, 0, 0, 9],
[0, 0, 0, 6, 0, 1, 0, 7, 8],
[0, 0, 7, 0, 4, 0, 2, 6, 0],
[0, 0, 1, 0, 5, 0, 9, 3, 0],
[9, 0, 4, 0, 6, 0, 0, 0, 5],
[0, 7, 0, 3, 0, 0, 0, 1, 2],
[1, 2, 0, 0, 0, 7, 4, 0, 0],
[0, 4, 9, 2, 0, 6, 0, 0, 7]
]
会做这项工作,董事会在网格类中传递。但是在课外生成一个,然后即使生成的板采用这种确切的格式,也不会将其插入上面的错误中。请任何有这种问题或知道如何调试的人来帮助我,非常感谢

最佳答案

当您生成板时,您只返回第一行而不是整个板。
在您的代码中进行此更改:

def generate_board():
.........
# for line in board: return line # remove this line
return board # return full board

关于python - 在矩阵中生成数独谜题而不是绘制 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63548488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com