绘制任意格数的棋盘进行五子棋游戏
代码显示:
# 本程序是为了实现一个任意大小的棋盘
def draw_board(t, array):
# 绘制出第一行数字
for i in range(t):
print(" " + str(i + 1) + " ", end="")
print()
for j in range(t):
# 绘制第一行上边框
print("+-----" * t, end="")
print("+")
# 绘制字符所在的行
for h in range(t):
print("| " + str(array[j][h]) + " ", end="")
print("| " + str(j + 1))
# 绘制最后一行边框:
print("+-----" * t, end="")
print("+")
if __name__ == '__main__':
t = int(input("请输入任意一个数值t,范围为0-9,我们将生成一个t*t大小的棋盘"))
arr = [[" " for i in range(t)] for j in range(t)]
draw_board(t, arr)
red = True
cishu=0
print(str(cishu % 2 + 1) + "号请准备")
while 1:
row, col = input("请依次输入横纵坐标,坐标用空格隔开,输入完毕点击enter键: ").split()
row, col = int(row)-1, int(col)-1
if 0<row<=t and 0<col<=t:
if arr[row][col] == " ":
if red == True:
arr[row][col] = "X"
red = False
else:
arr[row][col] = "O"
red = True
draw_board(t, arr)
print(str((cishu + 1) % 2 + 1) + "号请准备")
cishu += 1
if cishu == t * t:
print("本轮结束,平局@——@")
break
else:
print("数值输入错误,请重新输入!!!!!")
结果显示:
输入数字6,生成一个六行六列的棋盘