【发布时间】:2021-12-16 10:21:59
【问题描述】:
在学校开始了 Python 课程的编程,对 Python 很陌生。我制作了一个基本的 4 连击游戏,游戏区域为 4x4。但我不明白我的获胜系统在垂直方向上如何或为什么没有按我的预期工作。
如果 X 和 O 在任何行中彼此相邻,则当前打印 win。请不要评论太复杂。任何改进表示赞赏。
. | . | . | .
. | . | . | .
. | . | . | .
X | O | . | .
import os
def cls():
os.system('cls')
def rows():
cls()
print(*row4, sep=" | ")
print(*row3, sep=" | ")
print(*row2, sep=" | ")
print(*row1, sep=" | ")
def check_ver():
for x in range(4):
for j in range(4):
if ("X" in row1[j][x]) & ("X" in row2[j][x]) & ("X" in row3[j][x]) & ("X" in row4[j][x]):
print("X, Win")
break
else:
print("Fel igen!")
row1 = ['.', '.', '.', '.']
row2 = ['.', '.', '.', '.']
row3 = ['.', '.', '.', '.']
row4 = ['.', '.', '.', '.']
while True:
rows()
check_ver()
col = int(input('X, which column? '))
cls()
if col == "":
break
elif col >= 5 or col < 1:
print("Error")
continue
else:
col = col - 1
if row1[col] == '.':
row1[col] = 'X'
rows()
else:
if row2[col] == '.':
row2[col] = 'X'
rows()
else:
if row3[col] == '.':
row3[col] = 'X'
rows()
else:
if row4[col] == '.':
row4[col] = 'X'
rows()
col = int(input('O, which column? '))
if col >= 5 or col < 1:
print("Error")
continue
else:
col = col - 1
if row1[col] == '.':
row1[col] = 'O'
rows()
else:
if row2[col] == '.':
row2[col] = 'O'
rows()
else:
if row3[col] == '.':
row3[col] = 'O'
rows()
else:
if row4[col] == '.':
row4[col] = 'O'
rows()
【问题讨论】:
-
为什么会有嵌套循环
for x in range(4):-for j in range(4):?我确信这段代码不会像你想象的那样做。内部循环没有意义。
标签: python python-3.x matrix