14
2024
11

python编程学习-五子棋

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

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

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

from xdf import *

N = 15
CW = 48
OX = (768 - CW * (N - 1)) / 2
OY = (1024 - CW * (N - 1)) / 2
R = CW / 2 - 8
circles = []
data = []
isOver = False
player = 1
total = N * N
neighbour = [
    [-1, -1],[-1, 0],[-1, 1],
    [0, -1],[0, 1],
    [1, -1],[1, 0],[1, 1]
]
curPutdown = []
tupleScoreTable = [
    7,
    35,
    800,
    15000,
    800000,
    15,
    400,
    1800,
    100000,
    0,
    0
]
scoreTable = []
TUPLE = [[[0, 0], [0, 1], [0, 2], [0, 3], [0, 4]],
        [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4]],
        [[0, 0], [1, 0], [2, 0], [3, 0], [4, 0]],
        [[0, 0], [1, -1], [2, -2], [3, -3], [4, -4]]];

def xy2ij(x, y):
    return [round((y - OY) / CW), round((x - OX) / CW)]

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

for i in range(N):
    xy = ij2xy(i, 0)
    line(xy[0], xy[1], xy[0] + CW * (N - 1), xy[1], 1)
    xy = ij2xy(0, i)
    line(xy[0], xy[1], xy[0], xy[1] + CW * (N - 1), 1)

for i in range(N):
    a = []
    b = []
    c = []
    for j in range(N):
        xy = ij2xy(i, j)
        a.append([
            circle(xy[0], xy[1], R, 'white', 'black'),
            circle(xy[0], xy[1], R, 'black', 'black'),
            circle(xy[0], xy[1], R, 'transparent', 'red'),
        ])
        b.append(0)
        c.append(0)
    scoreTable.append(c)
    circles.append(a)
    data.append(b)
    curPutdown = []

overTxt1 = text('你赢了', 768/2, 1024/2-100, 50, 'red', 'center')
overTxt2 = text('点击任意位置重新开始', 50, 'red', 'center')

def refresh():
    global circles, data, curPutdown
    for i in range(N):
        for j in range(N):
            a = circles[i][j]
            a[0].hide()
            a[1].hide()
            a[2].hide()
            tp = data[i][j]
            if tp == 1:
                a[0].show()
            elif tp == 2:
                a[1].show()
    if len(curPutdown):
        a = circles[curPutdown[0]][curPutdown[1]]
        a[2].show()

def setState(i, j, tp):
    data[i][j] = tp

def reset():
    global isOver, player, total, overTxt1, overTxt2, data
    for i in range(N):
        for j in range(N):
            setState(i, j, 0)
    isOver = False
    player = 1
    total = 60
    overTxt1.hide()
    overTxt2.hide()
    refresh()

def tap():
    global isOver, player, data
    if isOver:
        reset()
        return;
    if player == 1:
        ij = xy2ij(x, y)
        i = ij[0]
        j = ij[1]
        if canPut(i, j):
            putDown(i, j, 1)
            checkOver()
            if not isOver:
                player = 2
                aiTurn()

def canPut(i, j):
    if isOut(i, j):
        return False
    if not isEmpty(i, j):
        return False
    return True

def cloneData():
    global data
    a = []
    for i in range(N):
        b = []
        for j in range(N):
            b.append(data[i][j])
        a.append(b)
    return a

def putDown(i, j, tp):
    global total, curPutdown
    curPutdown = [i, j]
    setState(i, j, tp)
    total = total - 1
    refresh()

def isOut(i, j):
    return i < 0 or i >= N or j < 0 or j >= N

def checkOver():
    global total, isOver, overTxt1, overTxt2
    if checkIsOver():
        isOver = True
        if player == 1:
            overTxt1.change("你赢了")
        else:
            overTxt1.change("我赢了")
        overTxt1.show()
        overTxt2.show()

def checkIsOver():
    global total, isOver, curPutdown
    if total == 0:
        return True
    if total > N * N - 9:
        return False
    if len(curPutdown) == 0:
        return False
    return (checkFive(curPutdown[0], curPutdown[1], 0, 1) 
            or checkFive(curPutdown[0], curPutdown[1], 1, 0) 
            or checkFive(curPutdown[0], curPutdown[1], 1, 1)
            or checkFive(curPutdown[0], curPutdown[1], 1, -1))


def checkFive(i0, j0, di, dj):
    global data
    tp = data[i0][j0]
    a = [[i0,j0]]
    for i in range(5):
        ii = i0 + (i + 1) * di
        jj = j0 + (i + 1) * dj
        if isOut(ii, jj):
            break;
        else:
            if data[ii][jj] == tp:
                a.append([ii, jj])
    for i in range(5):
        ii = i0 - (i + 1) * di
        jj = j0 - (i + 1) * dj
        if isOut(ii, jj):
            break;
        else:
            if data[ii][jj] == tp:
                a.append([ii, jj])
    return len(a) >= 5

def isEmpty(i, j):
    global data
    return data[i][j] == 0

def aiTurn():
    delay(aiplay, 200)

def score():
    for i in range(N):
        for j in range(N):
            for k in range(len(TUPLE)):
                tp = TUPLE[k]
                checkTuple(i, j, tp)

def checkTuple(i, j, tp):
    global data
    arr = []
    for k in range(len(tp)):
        ii = i + tp[k][0]
        jj = j + tp[k][1]
        if isOut(ii, jj):
            return
        arr.append(data[ii][jj])
    white = hasNum(arr, 1)
    black = hasNum(arr, 2)
    score = 0
    if white == 0:
        if black == 0:
            score = tupleScoreTable[0]
        elif black == 1:
            score = tupleScoreTable[1]
        elif black == 2:
            score = tupleScoreTable[2]
        elif black == 3:
            score = tupleScoreTable[3]
        elif black == 4:
            score = tupleScoreTable[4]
    elif black == 0:
        if white == 1:
            score = tupleScoreTable[5]
        elif white == 2:
            score = tupleScoreTable[6]
        elif white == 3:
            score = tupleScoreTable[7]
        elif white == 4:
            score = tupleScoreTable[8]
    else:
        score = 0
    if score != 0:
        setScore(i, j, tp, score)

def setScore(i, j, tp, score):
    for k in range(len(tp)):
        ii = i + tp[k][0]
        jj = j + tp[k][1]
        scoreTable[ii][jj] += score

def hasNum(arr, ty):
    num = 0
    for i in range(len(arr)):
        if arr[i] == ty:
            num = num + 1
    return num

def maxScorePoint():
    global data,scoreTable
    for i in range(N):
        for j in range(N):
            scoreTable[i][j] = 0
    score()
    p = [-1, -1]
    for i in range(N):
        for j in range(N):
            if isEmpty(i, j):
                if p[0] == -1 or scoreTable[i][j] >= scoreTable[p[0]][p[1]]:
                    p[0] = i
                    p[1] = j
    return p

def aiplay():
    p = maxScorePoint()
    putDown(p[0], p[1], 2)
    checkOver()
    if not isOver:
        global player
        player = 1

reset()









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



« 上一篇下一篇 »

相关文章:

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:32:46)

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

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

python编程学习-woblox  (2024-11-14 16:30:47)

python编程学习-where-my-ice-cream  (2024-11-14 16:29:57)

发表评论:

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