欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用python实现双人五子棋(终端版)

程序员文章站 2022-03-08 15:50:29
五子棋还是有很多大佬发的,今天我也发一篇萌新写的小五子棋这个是终端版的,如果有问题请大家指出,我去修改代码如下:screen = []screen_change=0def into():#初始空白棋盘 for i in range(screen_change): list_width=[] for j in range(screen_change): list_width.append(' '+' |') scr...

五子棋还是有很多大佬发的,今天我也发一篇萌新写的小五子棋
这个是终端版的,如果有问题请大家指出,我去修改
代码如下:

screen = []
screen_change=0

def into():#初始空白棋盘
    for i in range(screen_change):
        list_width=[]
        for j in range(screen_change):
            list_width.append('  '+'  |')
        screen.append(list_width)

def user_black():
    while True:
        coordinate = input("黑方请输入你想填下子的坐标  例1,2或者5,5")
        coordinate=coordinate.split(',')
        width_num = int(coordinate[0]) - 1
        high_num = int(coordinate[1]) - 1
        if screen[width_num][high_num][1]!=' ':
            print('你输入的地方已经有棋子了,请重新输入')
            continue
        screen[width_num][high_num] = ' ' + '@' + '  |'
        break
    screen_print()

def user_white():
    while True:
        coordinate = input("白方请输入你想填下子的坐标  例1,2或者5,5")
        coordinate = coordinate.split(',')
        width_num = int(coordinate[0]) - 1
        high_num = int(coordinate[1]) - 1
        if screen[width_num][high_num][1] != ' ':
            print('你输入的地方已经有棋子了,请重新输入')
            continue
        screen[width_num][high_num] = ' ' + '#' + '  |'
        break
    screen_print()

def eeferee():#判断输赢
    #判断行
    for i in range(screen_change):
        for j in range(screen_change-4):
            if screen[i][j][1]==screen[i][j+1][1]==screen[i][j+2][1]==screen[i][j+3][1]==screen[i][j+4][1] and screen[i][j][1]!=' ':
                return False
    for i in range(screen_change - 4):
        for j in range(screen_change):
            if screen[i][j][1]==screen[i+1][j][1]==screen[i+2][j][1]==screen[i+3][j][1]==screen[i+4][j][1] and screen[i][j][1]!=' ':
                return False
    for i in range(screen_change - 4):
        for j in range(screen_change - 4):
            if screen[i][j][1]==screen[i+1][j+1][1]==screen[i+2][j+2][1]==screen[i+3][j+3][1]==screen[i+4][j+4][1] and screen[i][j][1]!=' ':
                return False
        for k in range(4,screen_change):
            if k>=4:
                if screen[i][k][1] == screen[i+1][k-1][1] == screen[i+2][k-2][1] == screen[i+3][k-3][1] == screen[i+4][k-4][1] and screen[i][k][1] != ' ':
                    return False
    return True

def screen_print():#打印棋盘
    print('——' * (screen_change*3+1))
    for i in range(screen_change):
        print('|',end=' ')
        for j in range(screen_change):
            print(screen[i][j],end=' ')
        print()
        print('——'*(screen_change*3+1))

def main():
    global screen_change
    screen_change = int(input('请输入你设定的棋盘大小:'))
    into()
    screen_print()
    while eeferee():
        user_black()
        if not eeferee():
            break
        user_white()
    print('游戏结束')

if __name__=='__main__':
    main()

效果图如下:
用python实现双人五子棋(终端版)

Python问题解答私信我

本文地址:https://blog.csdn.net/Miku_wx/article/details/111831268

相关标签: python