15
2024
11

python编程学习-对角棋

学习python编程,在新东方少儿编程官网的自由创作平台做的一些作品。

可以在线运行,效果如下(在新页面中打开链接):

代码如下(用到平台自定义的xdf库):

from xdf import *

SW = 768
SH = 1024
m = 3
n = 3
CW = 200
CH = CW
R = CW // 4
OX = (SW - CW * (m - 1)) / 2
OY = (SH - CH * (n - 1)) / 2

def ij2xy(i, j):
    return [OX + i * CW , OY + j * CH]

def xy2ij(x, y):
    return [int((x - OX) / CW + 0.5), int((y - OY) / CH + 0.5)]

def drawLine(i1, j1, i2, j2):
    xy1 = ij2xy(i1, j1)
    xy2 = ij2xy(i2, j2)
    line(xy1[0], xy1[1], xy2[0], xy2[1], 10, "black")

drawLine(0, 0, 0, 2)
drawLine(1, 0, 1, 2)
drawLine(2, 0, 2, 2)
drawLine(0, 0, 2, 0)
drawLine(0, 1, 2, 1)
drawLine(0, 2, 2, 2)
drawLine(0, 0, 2, 2)
drawLine(0, 2, 2, 0)

def drawQizi(co):
    return circle(0, 0, R, "red" if co == 1 else "green")
qz1 = drawQizi(1)
qz2 = drawQizi(1)
qz3 = drawQizi(1)
qz4 = drawQizi(2)
qz5 = drawQizi(2)
qz6 = drawQizi(2)
qiziArr = []
line(10)
kuang = circle(0, 0, R, "transparent", "blue")
data = []
isOver = False
player = 1
xuanzhong = []
overTf = text("我赢了", SW / 2, 100, 60, "red", "center")

def tap():
    global isOver, player
    if isOver:
        return reset()
    if player == 1:
        turn1()

def turn1():
    global xuanzhong, data, qiziArr
    ij = xy2ij(x, y)
    i = ij[0]
    j = ij[1]
    if i < 0 or i > 2 or j < 0 or j > 2:
        return
    if not xuanzhong:
        if data[i][j] == 1:
            choose(i, j)
    else:
        if data[i][j] == 1:
            choose(i, j)
        elif data[i][j] == 0:
            if (abs(xuanzhong[0] - i) == 1 or abs(xuanzhong[1] - j) == 1):
                moveTo(i, j)
                checkWin()
                global isOver, player
                if not isOver:
                    player = 2
                    turn2()

def getPath2(d, v):
    a0 = findPath(d, v)
    return a0[random(len(a0))-1]

def turn2():
    global data
    p = getPath2(data, 2)
    delay(choose, 1000, p[0][0], p[0][1])
    delay(moveTo2, 3000, p[1][0], p[1][1])

def moveTo2(i, j):
    moveTo(i, j)
    turn2Over()

def doMove(d, p):
    i1 = p[0][0]
    j1 = p[0][1]
    i = p[1][0]
    j = p[1][1]
    d[i][j] = d[i1][j1]
    d[i1][j1] = 0
def cloneData(d):
    a0 = [];
    for i in range(len(d)):
        a0.append([])
        for j in range(len(d[i])):
            a0[i].append(d[i][j])
    return a0

def turn2Over():
    checkWin()
    global isOver, player
    if not isOver:
        player = 1

def moveTo(i, j):
    global xuanzhong, data, qiziArr
    i1 = xuanzhong[0]
    j1 = xuanzhong[1]
    data[i][j] = data[i1][j1]
    data[i1][j1] = 0
    qiziArr[i][j] = xuanzhong[2]
    qiziArr[i1][j1] = None
    setPos(xuanzhong[2], i, j)
    setPos(kuang, -100, 0)

def choose(i, j):
    global xuanzhong, qiziArr
    xuanzhong = [i, j, qiziArr[i][j]]
    setPos(kuang, i, j)
    
def checkWin():
    global player, isOver
    if isWin(data, player):
        isOver = True
        if player == 1:
            overTf.change("你赢了")
        else:
            overTf.change("计算机赢了")

def isWin(d, v):
    return isXie(d, v) or isNoPath(d, 2 if v == 1 else 1)

def isXie(d, v):
    return (d[0][0] == v and d[1][1] == v and d[2][2] == v) or (d[2][0] == v and d[1][1] == v and d[0][2] == v)

def isNoPath(d, v):
    a0 = findPath(d, v)
    return len(a0) == 0

neighbors = [
    [[1, 0], [0, 1], [1, 1]],
    [[0, -1], [0, 1], [1, 0]],
    [[1, 0], [0, -1], [1, -1]],
    [[-1, 0], [1, 0], [0, 1]],
    [[-1, 0], [1, 0], [0, -1], [0, 1], [-1, -1], [1, 1], [-1, 1], [1, -1]],
    [[-1, 0], [1, 0], [0, -1]],
    [[-1, 0], [0, 1], [-1, 1]],
    [[0, -1], [0, 1], [-1, 0]],
    [[-1, 0], [-1, -1], [0, -1]]
]

def isNotOut(i, j):
    return i >= 0 and i < 3 and j >= 0 and j < 3

def findPath(d, v):
    a0 = []
    for i in range(3):
        for j in range(3):
            if d[i][j] == v:
                for ne in neighbors[i * 3 + j]:
                    i1= i + ne[0]
                    j1 = j + ne[1]
                    if d[i1][j1] == 0:
                        a0.append([[i, j], [i1, j1]])
    return a0

def setPos(qz, i, j):
    xy = ij2xy(i, j)
    qz.move(xy[0], xy[1])
    
def reset():
    global isOver, player, xuanzhong, data, qiziArr
    isOver = False
    player = 1
    data = [[1,0,2],[1,0,2],[1,0,2]]
    qiziArr = [[qz1, None, qz4], [qz2, None, qz5], [qz3, None,qz6]]
    kuang.move(-100, 0)
    overTf.change("")
    setPos(qz1, 0, 0)
    setPos(qz2, 1, 0)
    setPos(qz3, 2, 0)
    setPos(qz4, 0, 2)
    setPos(qz5, 1, 2)
    setPos(qz6, 2, 2)


reset()


也可以下载代码,本地python环境运行(用pygame封装了xdf库)。



« 上一篇下一篇 »

相关文章:

python编程学习-小学数学加减分解题  (2024-11-15 11:32:29)

python编程学习-密室逃生  (2024-11-15 11:31:35)

python编程学习-围住神经猫  (2024-11-15 11:31:6)

python编程学习-四子棋  (2024-11-15 11:30:32)

python编程学习-华容道  (2024-11-14 16:43:25)

python编程学习-动画  (2024-11-14 16:39:37)

python编程学习-别踩白块  (2024-11-14 16:38:59)

python编程学习-俄罗斯方块  (2024-11-14 16:38:19)

python编程学习-井字棋  (2024-11-14 16:34:9)

python编程学习-五子棋  (2024-11-14 16:33:23)

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。