猫史档案馆


【Python作品分享】数独【作品秀】

用户:学霸猫【招铸洲】学霸猫【招铸洲】查看:1 回复:4 评论:1 创建时间:2022-12-06T10:14:06


【作品展示】

center_image

 

【作品介绍】

WASD移动

数字键填写

 

【作品源代码】

import random
import turtle
import tkinter
import easygui
import time


def sum(ll, find=0, zb=[[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]):
    global playing
    global x, y
    ret = True
    s = [0, 0, 0, 0, 0, 0, 0, 0, 0]
    for i in range(len(ll)):
        if not ll[i] == 0:
            s[ll[i]-1] += 1
            if s[ll[i]-1] == 2:
                if find == 1 and cw[zb[i][0]][zb[i][1]] == 1:
                    x, y = zb[i]
                ret = False
        if playing and ll[i] == 0:
            if find == 1:
                x, y = zb[i]
            ret = False
    return ret


def pd():
    global sudoku
    global playing
    for i in range(9):
        if not sum(sudoku[i]):
            if playing:
                print("\r{}".format("false"), end='')
            return False
    d = [0, 0, 0, 0, 0, 0, 0, 0, 0]
    for i in range(9):
        for j in range(9):
            d[j] = sudoku[j][i]
        if not sum(d):
            if playing:
                print("\r{}".format("false"), end='')
            return False
    f = 0
    for i in range(3):
        for j in range(3):
            f = 0
            for k in range(i*3, i*3+3):
                for l in range(j*3, j*3+3):
                    d[f] = sudoku[k][l]
                    f += 1
            if not sum(d):
                if playing:
                    print("\r{}".format("false"), end='')
                return False
    if playing:
        print("\r{}".format("false"), end='')
    return True


def find(a):
    global sudoku
    global playing
    for i in range(9):
        if not sum(sudoku[i], 1, [[i, 0], [i, 1], [i, 2], [i, 3], [i, 4], [i, 5], [i, 6], [i, 7], [i, 8]]):
            if playing:
                print("\r{}".format("false"), end='')
    d = [0, 0, 0, 0, 0, 0, 0, 0, 0]
    for i in range(9):
        for j in range(9):
            d[j] = sudoku[j][i]
        if not sum(d, 1, [[0, i], [1, i], [2, i], [3, i], [4, i], [5, i], [6, i], [7, i], [8, i]]):
            if playing:
                print("\r{}".format("false"), end='')
    f = 0
    for i in range(3):
        for j in range(3):
            f = 0
            for k in range(i*3, i*3+3):
                for l in range(j*3, j*3+3):
                    d[f] = sudoku[k][l]
                    f += 1
            if not sum(d):
                if playing:
                    print("\r{}".format("false"), end='')
    if playing:
        print("\r{}".format("true"), end='')
    writemap()


def diedai(n):
    global sudoku
    global seeds
    for i in range(9):
        sudoku[n//9][n % 9] = (i+(seeds % (n+9) % 9)) % 9+1
        if pd():
            if n == 80:
                return True
            if diedai(n+1) == True:
                return True
    sudoku[n//9][n % 9] = 0
    return False


def w(letter):
    global sudoku
    global seeds
    global cw
    if(cw[x][y] == 1):
        sudoku[x][y] = letter
    if(pd()):
        turtle.bye()
    writemap()


def p(letter):
    global sudoku
    global seeds
    global cw
    global x
    global y
    if letter == 's':
        x += 1
        if x > 8:
            x -= 1
    elif letter == 'a':
        y -= 1
        if y < 0:
            y += 1
    elif letter == 'w':
        x -= 1
        if x < 0:
            x += 1
    elif letter == 'd':
        y += 1
        if y > 8:
            y -= 1
    print('\r{}'.format(str(x)+'|'+str(y)), end='')
    writemap()


def writemap():
    global sudoku
    global seeds
    global cw
    global x
    global y
    turtle.clear()
    turtle.tracer(True)
    turtle.tracer(False)
    for i in range(10):
        if i % 3 == 0:
            turtle.pensize(5)
        else:
            turtle.pensize(2)
        turtle.penup()
        turtle.goto(25*i, 0)
        turtle.pendown()
        turtle.goto(25*i, -225)
    for i in range(10):
        if i % 3 == 0:
            turtle.pensize(5)
        else:
            turtle.pensize(2)
        turtle.penup()
        turtle.goto(0, -25*i)
        turtle.pendown()
        turtle.goto(225, -25*i)
    turtle.fillcolor('red')
    turtle.penup()
    turtle.goto(25*y, -25*x)
    turtle.begin_fill()
    turtle.goto(25*y, -25*x-25)
    turtle.goto(25*y+25, -25*x-25)
    turtle.goto(25*y+25, -25*x)
    turtle.end_fill()
    for i in range(9):
        for j in range(9):
            turtle.goto(10+25*j, -20-25*i)
            if sudoku[i][j] != 0:
                if cw[i][j] == 1:
                    turtle.write('*'+str(sudoku[i][j]))
                else:
                    turtle.write(sudoku[i][j])


def k1():
    w(1)


def k2():
    w(2)


def k3():
    w(3)


def k4():
    w(4)


def k5():
    w(5)


def k6():
    w(6)


def k7():
    w(7)


def k8():
    w(8)


def k9():
    w(9)


def kw():
    p('w')


def ka():
    p('a')


def ks():
    p('s')


def kd():
    p('d')


def kf():
    find(1)


global sudoku
global seeds
global cw
global x
global y
global playing
playing = False
turtle.hideturtle()
turtle.fillcolor("red")
t = turtle.Pen()
t.screen.onkey(k1, '1')
t.screen.onkey(k2, '2')
t.screen.onkey(k3, '3')
t.screen.onkey(k4, '4')
t.screen.onkey(k5, '5')
t.screen.onkey(k6, '6')
t.screen.onkey(k7, '7')
t.screen.onkey(k8, '8')
t.screen.onkey(k9, '9')
t.screen.onkey(kw, 'w')
t.screen.onkey(ka, 'a')
t.screen.onkey(ks, 's')
t.screen.onkey(kd, 'd')
t.screen.onkey(kf, 'f')
t.screen.listen()
chu = easygui.choicebox("请选择打乱类型:\n\n1.随机\n2.种子", "数独游戏", ['1', '2'])
if(chu == '1'):
    seeds = random.randint(1, 999999)
else:
    seeds = int(easygui.enterbox("请输入种子码:"))
sudoku = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0]]
cw = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0]]
diedai(0)
for i in range(15):  # 难度
    x, y = random.randint(0, 8), random.randint(0, 8)
    sudoku[x][y] = 0
    cw[x][y] = 1
turtle.speed(0)
turtle.delay(0)
turtle.tracer(False)
turtle.pensize(5)
turtle.hideturtle()
x = 0
y = 0
writemap()
playing = True
tick = time.time()
t.screen.mainloop()
tick = time.time()-tick
if pd():
    easygui.msgbox("恭喜过关!\n你的用时为:"+str(int(tick)))
else:
    easygui.msgbox("很遗憾,还原失败")

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
活泼的小电鼠S0fF活泼的小电鼠S0fF

厉害emotion_good

点赞0


评论


l一只皮卡丘ll一只皮卡丘l

emotion_good

点赞0


评论


mornwindmornwind

其实代码有一些冗余

比如这里:

cw = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0]]

完全可以改成:

cw=[[0 for i in range(9)] for j in range 9]

 

这里:

def k1():
    w(1)


def k2():
    w(2)


def k3():
    w(3)


def k4():
    w(4)

t.screen.onkey(k1, '1')
t.screen.onkey(k2, '2')
t.screen.onkey(k3, '3')
t.screen.onkey(k4, '4' 

可以改成

strings=["1","2","3","4"]
lst=[]
for i in strings:
    def func(a):
        w(int(i))
    lst.append(w)
for i in range(len(lst)):
    t.screen.onkey(lst[i],strings(i))

 

还有一些这里不废话了

另外就是turtle做游戏并不是最优选,可以尝试其他库

以及对于游戏面向对象编程比面向过程编程更清晰明了而且方便运维

点赞0


评论


mornwindmornwind

对一个优秀的算法而言一点格式问题无伤大雅

点赞0


评论