python做本地双人井字旗游戏(不需要pygame,网络连接)

简单,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 listglobal debugglobal dd = open('debug.dat', 'rb').read().decode()debug = bool(d)if d != 'True' and d != 'False':debug = Falselist = [[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 0print(f'{name}赢了!')print('下一局...\n\n')__init__()
def main():while True:# 行列输入for i in range(2):while True:ni = Noneif 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]) - 1if 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('超出范围!')continueelse:print('已有子')continuebreakexcept IndexError:print('超出范围!')continueexcept 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::左上')continueif 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 = Truefor i in list:for i2 in i:if not i2 or i2 is not False:p = Falsebreakif p:print('平局!')__init__()
if __name__ == '__main__':main()

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部