参考链接: Python程序使用numpy打印NxN的棋盘图案
绘制棋盘
利用字符串在命令行中打印出一个棋盘,可以用于实现五子棋,四连环游戏等
截图
实现1
def qipan():
#棋盘的参数
rows,columns = 4,4
data = [[-1 for i in range(columns)] for j in range(rows)]
#棋盘格子的具体位置
#data[0] = [1,0,0,1]
data[1] = [0,1,1,0]
#data[2] = [1,0,1,0]
#data[3] = [0,1,1,0]
#开始绘制棋盘o
print('-'*9)
for i in range(rows):
print('|',end='')
for j in data[i]:
if not(j == 0 or j == 1):
print(' |',end='')
else:
print(f'{j}|',end='')
print()
print('-'*9)
实现2
def func():
#棋盘的参数
rows,columns = 4,4
data = [[-1 for i in range(columns)] for j in range(rows)]
#棋盘格子的具体位置
#data[0] = [1,0,0,1]
data[1] = [0,1,1,0]
#data[2] = [1,0,1,0]
#data[3] = [0,1,1,0]
#开始绘制棋盘
a = itertools.chain.from_iterable(data)
print('-'*9)
for no,i in zip(itertools.count(1),itertools.product("|",a)):
temp = i[1]
if not(temp == 0 or temp == 1):
temp = ' '
print(f'{i[0]}{temp}',end='')
if no %4 == 0:
print('|n' '-'*9)