根据您的代码,遍历二维列表piece从piece[0][0]到piece[2][2]的方法如下:
# 导入模块
import random, os
# 声明变量和初始化列表
xx = -114514
xy = -114514
ox = -114514
oy = -114514
piece = [
[['-'],['-'],['-']],
[['-'],['-'],['-']],
[['-'],['-'],['-']],
]
def xxy():
global xx, xy # 声明全局变量
xx = int(input('请玩家一输入棋子的X坐标:'))
xy = int(input('请玩家一输入棋子的Y坐标:'))
if piece[xx][xy] == '✖' or piece[xx][xy] == '⬤':
print('不可下子……')
xxy()
else:
piece[xx][xy] = '✖'
def oxy():
global ox, oy # 声明全局变量
ox = int(input('请玩家一输入棋子的X坐标:'))
oy = int(input('请玩家一输入棋子的Y坐标:'))
if piece[ox][oy] == '✖' or piece[ox][oy] == '⬤':
print('不可下子……')
oxy()
else:
piece[ox][oy] = '⬤'
def draw():
os.system('clear')
for row in piece:
for col in row:
print(col[0], end=' ')
print()
def traverse_piece():
for mapx in range(3):
for mapy in range(3):
draw()
print()
# 主程序入口
run = True
while run:
traverse_piece()
xxy()
oxy()
这样修改后,每次遍历二维列表piece时会先清空屏幕,然后打印整个棋盘,接着进行玩家输入并更新棋盘上的棋子位置。持续循环直到满足退出条件。 |