15
2024
11

python编程学习-四子棋

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

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

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

from xdf import *

SW = 768
SH = 1024
m = 8
n = 8
CW = SW // m
CH = CW
R = CW // 2 - 2
OX = (SW - CW * m) / 2
OY = (SH - CH * n) / 2

def ij2xy(i, j):
    return [OX + i * CW + CW / 2, OY + (n - 1 - j) * CH + CH / 2]

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

fill("bg15")
qipan = []
for i in range(m):
    qipan.append([])
    for j in range(n):
        xy = ij2xy(i, j)
        qipan[i].append([
            0,
            circle(xy[0], xy[1], R, "white"),
            circle(xy[0], xy[1], R, "red"),
            circle(xy[0], xy[1], R, "green")
        ])

tfOver = text("", SW / 2, 100, 60, "red", "center")
isOver = False
player = 1
qiNum = 0

def win():
    global isOver
    tfOver.change("你赢了")
    tfOver.show()
    isOver = True

def lose():
    global isOver
    tfOver.change("我赢了")
    tfOver.show()
    isOver = True

def heqi():
    global isOver
    tfOver.change("和棋")
    tfOver.show()
    isOver = True

def reset():
    global isOver,player, qiNum
    isOver = False
    player = 1
    qiNum = 0
    tfOver.hide()
    for i in range(m):
        for j in range(n):
            qipan[i][j][0] = 0
            qipan[i][j][2].hide()
            qipan[i][j][3].hide()

def showQi1(i, j):
    qipan[i][j][0] = 1
    qipan[i][j][2].show()

def showQi2(i, j):
    qipan[i][j][0] = 2
    qipan[i][j][3].show()

drs = [[1, 0], [0, 1], [1, 1], [1, -1]]
def checkWin(i, j):
    global player
    for dr in drs:
        if checkDir(i, j, dr):
            if player == 1:
                win()
            else:
                lose()
            break

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

def getSameNum(i, j, dr, v):
    num = 0
    for ii in range(1, 4):
        i1 = i + dr[0] * ii
        j1 = j + dr[1] * ii
        if isNotOut(i1, j1) and qipan[i1][j1][0] == v:
            num += 1
        else:
            break
    return num

def checkDir(i, j, dr):
    return getSameNums(i, j, dr, qipan[i][j][0]) >= 3

def getSameNums(i, j, dr, v):
    dr1 = [-dr[0], -dr[1]]
    n1 = getSameNum(i, j, dr, v)
    n2 = getSameNum(i, j, dr1, v)
    return n1 + n2
    
def checkOver():
    global isOver, qiNum, m, n
    if not isOver and qiNum == m * n:
        heqi()

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

def turn1():
    ij = xy2ij(x, y)
    i = ij[0]
    if i >= 0 and i < m:
        putOn(i)

def putOn(i):
    global player,qiNum, isOver
    j = getEmpty(i)
    if j >= 0:
        if player == 1:
            showQi1(i, j)
            qiNum += 1
        else:
            showQi2(i, j)
            qiNum += 1
        checkWin(i, j)
        checkOver()
        if not isOver:
            if player == 1:
                player = 2
                delay(turn2, 2000)
            else:
                player = 1
                

def getEmpty(i):
    for j in range(n):
        if qipan[i][j][0] == 0:
            return j
    return -1

scoresTable = [0, 10, 50, 200, 10000]
def getScore(i, j):
    sc = 0
    num = 0
    for dr in drs:
        num = min(4, getSameNums(i, j, dr, 1))
        sc += scoresTable[num]
        num = min(4, getSameNums(i, j, dr, 2))
        sc += scoresTable[num] * 2
    return sc

def turn2():
    arr = getEmptys()
    maxSc = 0
    maxPos = arr[0]
    for pos in arr:
        sc = getScore(pos[0], pos[1])
        if sc > maxSc:
            maxSc = sc
            maxPos = pos
    putOn(maxPos[0])

def getEmptys():
    arr = []
    for i in range(m):
        j = getEmpty(i)
        if j >= 0:
            arr.append([i, j])
    return arr

reset()


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



« 上一篇下一篇 »

相关文章:

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

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)

python编程学习-乒乓  (2024-11-14 16:32:46)

python编程学习-一笔画关卡生成  (2024-11-14 16:32:6)

python编程学习-一笔画  (2024-11-14 16:31:27)

发表评论:

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