五子棋还是有很多大佬发的,今天我也发一篇萌新写的小五子棋 这个是终端版的,如果有问题请大家指出,我去修改
代码如下:
代码语言:javascript复制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]) - 1high_num = int(coordinate[1]) - 1if screen[width_num][high_num][1]!=' ':print('你输入的地方已经有棋子了,请重新输入')continuescreen[width_num][high_num] = ' ' '@' ' |'breakscreen_print() def user_white():while True:coordinate = input("白方请输入你想填下子的坐标 例1,2或者5,5")coordinate = coordinate.split(',')width_num = int(coordinate[0]) - 1high_num = int(coordinate[1]) - 1if screen[width_num][high_num][1] != ' ':print('你输入的地方已经有棋子了,请重新输入')continuescreen[width_num][high_num] = ' ' '#' ' |'breakscreen_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 Falsefor 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 Falsefor 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 Falsefor 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 Falsereturn 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_changescreen_change = int(input('请输入你设定的棋盘大小:'))into()screen_print()while eeferee():user_black()if not eeferee():breakuser_white()print('游戏结束') if __name__=='__main__':main()
效果图如下:
以上就是本文的全部内容