手动使用python写一个五子棋程序

finish = False
flagNum = 1
flagch = "*"  # 当前下棋者棋子
x = 0  # 横坐标
y = 0  # 纵坐标
print("简易五子棋")
# 棋盘初始化
checkerboard = []
for i in range(10):
    checkerboard.append([])
    for j in range(10):
        checkerboard[i].append('-')

def msg():
    # 输出胜利的棋盘
    print("------------")
    print("   1  2  3  4  5  6  7  8  9  10")
    for i in range(len(checkerboard)):
        print(chr(i + ord('A')) + " ", end=' ')  # 输出列标号
        for j in range(len(checkerboard[i])):
            print(checkerboard[i][j] + " ", end=' ')
        print()  # 换行
    print("-------------------------------")
    # 输出赢家
    if (flagNum == 1):
        print("*棋获胜")
    else:
        print("o棋获胜")
while not finish:
    print("\033[1;30;46m--------------------------------")
    print("   1  2  3  4  5  6  7  8  9  10")
    for i in range(len(checkerboard)):
        print(chr(i + ord('A')) + " ", end=' ')
        for j in range(len(checkerboard[i])):
            print(checkerboard[i][j] + " ", end=' ')
        print()
    print("--------------------------------\033[0m")

    # 判断当前下棋者
    if flagNum == 1:
        flagch = "*"
        print("请*输入棋子坐标")
    else:
        flagch = "o"
        print("请o输入棋子坐标")

    # 输入棋子坐标
    str = input()
    ch = str[0]
    x = ord(ch) - 65
    y = int(str[1]) - 1

    # 判断坐标有没有出界
    if (x < 0 or x > 9 or y < 0 or y > 9):
        print("输入坐标有误,请重新输入")
        continue

    # 判断坐标上是否有棋子
    if (checkerboard[x][y] == '-'):
        if (flagNum == 1):
            checkerboard[x][y] = '*'
        else:
            checkerboard[x][y] = 'o'
    else:
        print("您输入的已经有棋子了,请重新输入")
        continue

    # 判断棋子左侧
    if (y - 4 >= 0):
        if (checkerboard[x][y - 1] == flagch
                and checkerboard[x][y - 2] == flagch
                and checkerboard[x][y - 3] == flagch
                and checkerboard[x][y - 4] == flagch
        ):
            finish = True
            msg()

    # 判断棋子右侧
    if (y + 4 <= 9):
        if (checkerboard[x][y + 1] == flagch
                and checkerboard[x][y + 2] == flagch
                and checkerboard[x][y + 3] == flagch
                and checkerboard[x][y + 4] == flagch
        ):
            finish = True
            msg()

    # 判断棋子上方
    if (x - 4 >= 0):
        if (checkerboard[x - 1][y] == flagch
                and checkerboard[x - 2][y] == flagch
                and checkerboard[x - 3][y] == flagch
                and checkerboard[x - 4][y] == flagch
        ):
            finish = True
            msg()

    # 判断棋子下方
    if (x + 4 <= 9):
        if (checkerboard[x + 1][y] == flagch
                and checkerboard[x + 2][y] == flagch
                and checkerboard[x + 3][y] == flagch
                and checkerboard[x + 4][y] == flagch
        ):
            finish = True
            msg()

    # 判断棋子右上方
    if (x - 4 >= 0 and y - 4 >= 0):
        if (checkerboard[x - 1][y - 1] == flagch
                and checkerboard[x - 2][y - 2] == flagch
                and checkerboard[x - 3][y - 3] == flagch
                and checkerboard[x - 4][y - 4] == flagch
        ):
            finish = True
            msg()

    # 判断棋子右下方
    if (x + 4 <= 9 and y + 4 <= 9):
        if (checkerboard[x + 1][y - 1] == flagch
                and checkerboard[x + 2][y - 2] == flagch
                and checkerboard[x + 3][y - 3] == flagch
                and checkerboard[x + 4][y - 4] == flagch
        ):
            finish = True
            msg()

    # 判断棋子左上方
    if (x - 4 >= 0 and y + 4 <= 9):
        if (checkerboard[x - 1][y + 1] == flagch
                and checkerboard[x - 2][y + 2] == flagch
                and checkerboard[x - 3][y + 3] == flagch
                and checkerboard[x - 4][y + 4] == flagch
        ):
            finish = True
            msg()

    # 判断棋子左下方
    if (x + 4 <= 9 and y + 4 <= 9):
        if (checkerboard[x + 1][y + 1] == flagch
                and checkerboard[x + 2][y + 2] == flagch
                and checkerboard[x + 3][y + 3] == flagch
                and checkerboard[x + 4][y + 4] == flagch
        ):
            finish = True
            msg()


    flagNum *= -1 #更换下棋者身份

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页