简单,111行代码。
就是打印区,判断区,debug区,输入区和初始化。
废话不多说,代码!
d = open('debug.dat', 'rb').read().decode()
debug = bool(d)
if d != 'True' and d != 'False':
debug = False
list = [[None, None, None],[None, None, None],[None, None, None]]
def __init__():
global list
global debug
global d
d = open('debug.dat', 'rb').read().decode()
debug = bool(d)
if d != 'True' and d != 'False':
debug = False
list = [[None, None, None], [None, None, None], [None, None, None]]
def ying(b):
if b:
name = 'X'
elif b is False:
name = 'O'
else:
if debug:
print('debug::错误!\n')
return 0
print(f'{name}赢了!')
print('下一局...\n\n')
__init__()
def main():
while True:
# 行列输入
for i in range(2):
while True:
ni = None
if i == 0:
ni = 'O'
elif i == 1:
ni = 'X'
else:
if debug:
print('debug::错误!')
a_ = input(ni + " 输入行/列:")
a = a_.split('/')
try:
n = int(a[0]) - 1, int(a[1]) - 1
if list[n[0]][n[1]] is not False and not list[n[0]][n[1]]:
if n[0] >= 0 and n[1] >= 0:
list[n[0]][n[1]] = bool(i)
else:
print('超出范围!')
continue
else:
print('已有子')
continue
break
except IndexError:
print('超出范围!')
continue
except ValueError:
print('字符错误!应为digit/digit,而不是' + a_ + '.')
continue
# 判断是否赢
for i in range(len(list)):
# 横
if list[i][0] == list[i][1] == list[i][2] and list[i][0] is not None and list[i][1] is not None and list[i][2] is not None:
ying(list[i][0])
if debug:
print('debug::横')
continue
# 竖
elif list[0][i] == list[1][i] == list[2][i] and list[0][i] is not None and list[1][i] is not None and list[2][i] is not None:
ying(list[0][i])
if debug:
print('debug::竖')
continue
# 右下斜
elif list[0][0] == list[1][1] == list[2][2] and list[0][0] is not None and list[1][1] is not None and list[2][2] is not None:
ying(list[0][0])
if debug:
print('debug::右下')
continue
# 左上斜
elif list[0][2] == list[1][1] == list[2][0] and list[0][2] is not None and list[1][1] is not None and list[0][2] is not None:
ying(list[0][2])
if debug:
print('debug::左上')
continue
if debug:
print('debug::' + list)
# 打印井字旗
print('-------')
for i in list:
print('|', end = '')
for i2 in i:
if i2:
print('X', end = '|')
elif i2 is False:
print('O', end = '|')
else:
print(' ', end = '|')
print('\n-------')
# 判断是否平局
p = True
for i in list:
for i2 in i:
if not i2 or i2 is not False:
p = False
break
if p:
print('平局!')
__init__()
if __name__ == '__main__':
main()