猫史档案馆


海龟围棋.

用户:天气圣盐天气圣盐查看:0 回复:1 评论:0 创建时间:2022-07-06T19:37:50


# -*- coding: utf-8 -*-
import turtle
import time

pen = turtle.Pen()
pen.speed(0)
turtle.bgcolor("#F4C79E")
turtle.setup(1050, 1050)
turtle.title("中国围棋")
pen.hideturtle()
turtle.tracer(False)
cspace = 20
grid = 50
strblack = "black"
strwhite = "white"
strempty = "empty"
currrole = strblack
opprole = strwhite
gamestatus = True
array = []
checkedarray = []
rolearray = []
opparray = []

life_count = 0


def newchess(role, x, y):
    """
    落子
    :param pix:
    :param role:
    :return:
    """
    pen.penup()
    pen.setposition(x, y)
    pen.pendown()
    pen.color(role)
    pen.dot(30)
    array.append({"pix": (x, y), "role": role, "flag": 1})
    turtle.update()
    print("new chess: (", x, y, "), role:", role, ",flag = 1")


def refreshchess():
    for i in array:
        pen.penup()
        pen.setposition(i["pix"][0], i["pix"][1])
        pen.pendown()
        pen.color(i["role"])
        pen.dot(30)
    turtle.update()


def removechess(role, x, y):
    array.remove({"pix": (x, y), "role": role, "flag": 1})
    #turtle.update()
    print("removed chess: (", x, y, "), role:", role, ",flag = 1")


def gamestart():
    global gamestatus
    gamestatus = True
    array.clear()
    pen.reset()
    pen.penup()
    pen.setposition(-200, 150)
    pen.width(30)
    pen.color(strwhite)
    pen.pendown()
    pen.write("Game is starting", font=("Arial", 50, "bold"))
    time.sleep(1)
    pen.undo()
    """
    j = 3
    for i in range(4):
        pen.penup()
        pen.setposition(-25, 0)
        pen.width(50)
        pen.color("red")
        pen.pendown()
        pen.write(j,font = ("Arial",100,"bold"))
        time.sleep(1)
        pen.undo()
        j = j - 1
    """
    draw()


def gameover(role):
    time.sleep(2)
    pen.clear()
    pen.penup()
    pen.setposition(-250, 150)
    pen.width(20)
    pen.color(strwhite)
    pen.pendown()
    pen.write("Game is over, the Winner is ", font=("Arial", 35, "bold"))
    time.sleep(1)
    pen.undo()
    print("Game is over, the Winner is ", role)
    pen.penup()
    pen.setposition(-50, 0)
    pen.width(20)
    pen.color("Red")
    pen.pendown()
    pen.write(role, font=("Arial", 50, "bold"))
    time.sleep(3)
    pen.undo()
    gamestart()


def draw():
    """
    绘制棋盘
    :return:
    """
    #绘制网格边框
    pen.penup()
    pen.setposition(-500, 500)
    pen.pendown()
    pen.color("#6E3F25")
    pen.width(30)
    pen.hideturtle()

    for x in range(1, 5):
        if x % 2 != 0:
            pen.forward(1000)
        else:
            pen.forward(1000)
        pen.right(90)

    #绘制网格
    for i in range(1, 20):
        #画15条竖线
        pen.right(90)
        pen.penup()
        pen.setposition((50*i-500), 450)
        pen.width(2)
        pen.pendown()
        pen.forward(900)
        #画15条横线
        pen.left(90)
        pen.penup()
        pen.setposition(-450, (50*i-500))
        pen.width(2)
        pen.pendown()
        pen.forward(900)

    dot = [(300, 300), (-300, 300), (300, -300), (0, 300),
           (300, 0), (0, -300), (-300, 0), (0, 0), (-300, -300)]
    for i in dot:
        pen.penup()
        pen.setposition(i)
        pen.pendown()
        pen.color(strblack)
        pen.dot(10)

    refreshchess()
    turtle.update()
#draw()


def findpix(n):
    pixdata = 0
    a = n % grid
    num = n//grid
    if a > 25:
        pixdata = (num + 1)*grid
    elif a >= 0:
        pixdata = num * grid

    if pixdata > 450:
        print("Illegal Position#1")
        return 450
    elif pixdata < -450:
        print("Illegal Position#2")
        return -450
    else:
        return pixdata

#检查棋子队列中该坐标是否有棋子, 无棋子 = 0; 黑棋子 = 1; 白棋子 = -1;


def posstatus(x, y):
    for v in array:
        if v["pix"] == (x, y):
            if v["flag"] == 1:
                return v["role"]
            else:
                return strempty
    return strempty

#检查该位坐标是否为空,如是,则允许落子


def confirmpos(role, x, y):
    status = posstatus(x, y)
    print("confirmchess")
    if status == strempty:
        newchess(role, x, y)
    else:
        print("The current position %d,%d is not available!" % x % y)


def relatedpos(x, y):
    relatedpix = []
    pos_flag = 0
    if x + 50 <= 450:
        pos_flag = posstatus((x + 50), y)
        relatedpix.append(((x + 50), y, pos_flag))
    if x - 50 >= -450:
        pos_flag = posstatus((x - 50), y)
        relatedpix.append(((x - 50), y, pos_flag))
    if y + 50 <= 450:
        pos_flag = posstatus(x, (y + 50))
        relatedpix.append((x, (y + 50), pos_flag))
    if y - 50 >= -450:
        pos_flag = posstatus(x, (y - 50))
        relatedpix.append((x, (y - 50), pos_flag))
    return relatedpix


def checkopp(role, x, y):
    print("-----checkopp----", role, x, y)
    aroundpos = relatedpos(x, y)
    if role == strblack:
        for i in aroundpos:
            print("checkopp#aroundpos: ", i)
            if i[2] == strwhite:  # 需要检查异色棋子是否存活
                print("checkopp#白色棋子位置:", i[0], i[1])
                if (i[0], i[1]) not in opparray:
                    opparray.append((i[0], i[1]))

    elif role == strwhite:
        for i in aroundpos:
            if i[2] == strblack:  # 需要检查异色棋子是否存活
                print("checkopp#黑色棋子位置:", i[0], i[1])
                if (i[0], i[1]) not in opparray:
                    opparray.append((i[0], i[1]))


def check(role, x, y):
    global life_count, level
    aroundpos = relatedpos(x, y)
    print("-----check----", role, x, y)
    if (x, y) not in rolearray:
        rolearray.append((x, y))
    if role == strblack:
        for i in aroundpos:
            print("aroundpos: ", i)
            if (i[0], i[1]) not in checkedarray:
                #存入已检查的位置
                checkedarray.append((i[0], i[1]))
                if i[2] == strempty:  # 是否有活气
                    life_count += 1
                    print("1#life_count = ", life_count,
                          "Pix is ", i[0], i[1], "Role =", role)
                elif i[2] == strblack and role == strblack:  # 是否有同色棋子
                    print("黑色棋子位置:", i[0], i[1])
                    if (i[0], i[1]) not in rolearray:
                        rolearray.append((i[0], i[1]))
                    check(role, i[0], i[1])

    elif role == strwhite:
        for i in aroundpos:
            print("aroundpos: ", i)
            if (i[0], i[1]) not in checkedarray:
                #存入已检查的位置
                checkedarray.append((i[0], i[1]))
                if i[2] == strempty:  # 是否有活气
                    life_count += 1
                    print("1#life_count = ", life_count,
                          "Pix is ", i[0], i[1], "Role =", role)
                elif i[2] == strwhite and role == strwhite:  # 是否有同色棋子
                    print("白色棋子位置:", i[0], i[1])
                    if (i[0], i[1]) not in rolearray:
                        rolearray.append((i[0], i[1]))
                    check(role, i[0], i[1])


def remove(role):
    for i in rolearray:
        removechess(role, i[0], i[1])


def judgement(role, x, y):
    pass


def click(x, y):
    """
    鼠标点击事件
    :param x:
    :param y:
    :return:
    """
    global currrole, gamestatus, opprole
    global life_count, checkedarray, rolearray, opparray
    #try :
    if gamestatus:
        px = findpix(x)
        py = findpix(y)

        confirmpos(currrole, px, py)

        print("==========check opp chess======")

        opparray.clear()
        checkopp(currrole, px, py)
        print("------opparray: ", opparray)
        #检查对手棋子,是否被杀喵
        for i in opparray:
            life_count = 0
            checkedarray.clear()
            rolearray.clear()
            check(opprole, i[0], i[1])
            print("2#checkedarray:", checkedarray)
            print("2#rolearray:", rolearray)
            print("2#:life_count", life_count)
            if life_count == 0:
                remove(opprole)

        life_count = 0
        checkedarray.clear()
        rolearray.clear()
        print("==========check role chess======")
        check(currrole, px, py)
        print("1#checkedarray:", checkedarray)
        print("1#rolearray:", rolearray)
        #print("1#opparray:",opparray)
        print("1#:life_count", life_count)
        if life_count == 0:
            remove(currrole)
        """
        #检查对手棋子,是否被杀喵
        for i in opparray:
            life_count = 0
            checkedarray.clear()
            rolearray.clear()
            check(opprole, i[0], i[1])
            print("2#checkedarray:", checkedarray)
            print("2#rolearray:", rolearray)
            print("2#:life_count", life_count)
            if life_count == 0:
                remove(opprole)
        """
        time.sleep(1)
        if judgement(currrole, px, py):
            gamestatus = False
            print("Game Over,%s is winner" % currrole)
            gameover(currrole)
        else:
            if currrole == strblack:
                currrole = strwhite
                opprole = strblack
            else:
                currrole = strblack
                opprole = strwhite

    else:
        print("game is over")
    #except TypeError:
        #print("Illegal Position!#3")
    pen.reset()
    draw()


gamestart()
turtle.onscreenclick(click)

turtle.done()


回复

上一页1 页 / 共 1下一页
天气圣盐天气圣盐

转载

点赞0


评论