python - Python 3井字游戏的问题
问题描述
- 玩家 xyz 赢得的输出在赢得一次机会后被打印出来。
- 即使玩家 2 获胜,输出也会显示玩家 1 获胜 我已经尝试了一些尝试并尝试自己解决它,但直到现在都没有运气。此外,如果您有任何建议可以使代码更高效或更美观,请将其放在下面。我的代码:
def printrow(r1,r2,r3):
print(r1)
print(r2)
print(r3)
def empty(inp):
if inp!='-':
print("Please enter a position which is empty")
else:
return 'valid'
def user1(inp):
if inp not in range(1,10):
print("Please enter a number in the range of 1 to 9")
else:
if inp==1:
if empty(row1[0])=='valid':
row1[0]='X'
printrow(row1,row2,row3)
elif inp==2:
if empty(row1[1])=='valid':
row1[1]='X'
printrow(row1,row2,row3)
elif inp==3:
if empty(row1[2])=='valid':
row1[2]='X'
printrow(row1,row2,row3)
elif inp==4:
if empty(row2[0])=='valid':
row2[0]='X'
printrow(row1,row2,row3)
elif inp==5:
if empty(row2[1])=='valid':
row2[1]='X'
printrow(row1,row2,row3)
elif inp==6:
if empty(row2[2])=='valid':
row2[2]='X'
printrow(row1,row2,row3)
elif inp==7:
if empty(row3[0])=='valid':
row3[0]='X'
printrow(row1,row2,row3)
elif inp==8:
if empty(row3[1])=='valid':
row3[1]='X'
printrow(row1,row2,row3)
elif inp==9:
if empty(row3[2])=='valid':
row3[2]='X'
printrow(row1,row2,row3)
def user2(inp):
if inp not in range(1, 10):
print("Please enter a number in the range of 1 to 9")
else:
if inp == 1:
if empty(row1[0]) == 'valid':
row1[0] = '0'
printrow(row1,row2,row3)
elif inp == 2:
if empty(row1[1]) == 'valid':
row1[1] = '0'
printrow(row1,row2,row3)
elif inp == 3:
if empty(row1[2]) == 'valid':
row1[2] = '0'
printrow(row1,row2,row3)
elif inp == 4:
if empty(row2[0]) == 'valid':
row2[0] = '0'
printrow(row1,row2,row3)
elif inp == 5:
if empty(row2[1]) == 'valid':
row2[1] = '0'
printrow(row1,row2,row3)
elif inp == 6:
if empty(row2[2]) == 'valid':
row2[2] = '0'
printrow(row1,row2,row3)
elif inp == 7:
if empty(row3[0]) == 'valid':
row3[0] = '0'
printrow(row1,row2,row3)
elif inp == 8:
if empty(row3[1]) == 'valid':
row3[1] = '0'
printrow(row1,row2,row3)
elif inp == 9:
if empty(row3[2]) == 'valid':
row3[2] = '0'
printrow(row1,row2,row3)
def check(r1,r2,r3):
if r1[0] == 'X' and r1[1] == 'X' and r1[2] == 'X':
return "Congratulations Player 1! You won."
elif r1[0] == 'X' and r2[0] == 'X' and r3[0] == 'X':
return "Congratulations Player 1! You won."
elif r1[0] == 'X' and r2[1] == 'X' and r3[2] == 'X':
return "Congratulations Player 1! You won."
elif r1[1] == 'X' and r2[1] == 'X' and r3[1] == 'X':
return "Congratulations Player 1! You won."
elif r1[2] == 'X' and r2[2] == 'X' and r3[2] == 'X':
return "Congratulations Player 1! You won."
elif r1[2] == 'X' and r2[1] == 'X' and r3[0] == 'X':
return "Congratulations Player 1! You won."
elif r2[0] == 'X' and r2[1] == 'X' and r2[2] == 'X':
return "Congratulations Player 1! You won."
elif r3[0] == 'X' and r3[1] == 'X' and r3[2] == 'X':
return "Congratulations Player 1! You won."
elif r1[0] == '0' and r1[1] == '0' and r1[2] == '0':
return "Congratulations Player 2! You won."
elif r1[0] == '0' and r2[0] == '0' and r3[0] == '0':
return "Congratulations Player 2! You won."
elif r1[0] == '0' and r2[1] == '0' and r3[2] == '0':
return "Congratulations Player 2! You won."
elif r1[1] == '0' and r2[1] == '0' and r3[1] == '0':
return "Congratulations Player 2! You won."
elif r1[2] == '0' and r2[2] == '0' and r3[2] == '0':
return "Congratulations Player 2! You won."
elif r1[2] == '0' and r2[1] == '0' and r3[0] == '0':
return "Congratulations Player 2! You won."
elif r2[0] == '0' and r2[1] == '0' and r2[2] == '0':
return "Congratulations Player 2! You won."
elif r3[0] == '0' and r3[1] == '0' and r3[2] == '0':
return "Congratulations Player 2! You won."
print("[1,2,3]\n[4,5,6]\n[7,8,9]")
count=0; winner=False
row1=['-','-','-']
row2= ['-','-','-']
row3=['-','-','-']
print("Welcome to Tic Tac Toe")
print("Player 1 will be X and will start the game, and player 2 will be 0 and play second.")
while winner == False:
if count<9:
count+=1
if type(check(row1,row2,row3))==str:
print(check(row1,row2,row3))
else:
inp=int(input("Player 1: Where do you want to insert the next X? "))
user1(inp)
inp=int(input("Player 2: Where do you want to insert the next 0? "))
user2(inp)
解决方案
问题#1:调用后循环不会停止check()
,所以游戏会继续。您可以调用break
退出循环并结束游戏。
问题 2:我尝试运行您的代码,它说玩家 2 赢了。
Player 1: Where do you want to insert the next X? 4
['X', 'X', '0']
['X', '0', '-']
['-', '-', '-']
Player 2: Where do you want to insert the next 0? 7
['X', 'X', '0']
['X', '0', '-']
['0', '-', '-']
Congratulations Player 2! You won.
Congratulations Player 2! You won.
Congratulations Player 2! You won.
Congratulations Player 2! You won.
Congratulations Player 2! You won.
Congratulations Player 2! You won.
一般反馈:当代码重复多次时,这是一个令人担忧的迹象。编写起来需要更多的努力,更难理解,而且更容易出现错误。一个好的经验法则是,当您发现自己多次复制/粘贴代码时,请改用循环!在这种情况下,您可能不需要所有这些if
s。而且您可能不需要复制播放器 1 和播放器 2 的代码 - 您能想出一种方法来为他们两个重用相同的代码吗?也许是一个功能?
推荐阅读
- javascript - 一种在悬停而不是单击时触发子菜单的方法?
- python - 将 pandas 数据框中的值添加到列表中
- google-app-maker - Google 应用制作工具表查询 _notContains 不起作用
- minecraft-forge - 项目 forge 1.12.2 (intellij idea) 加载模型/纹理的问题
- c# - 如何检测烛台图中的异常值
- c# - List.Find 在 C# 代码中返回错误
- ios - 为什么我的视图控制器没有填满整个屏幕?
- c++ - 算法设计:在 C++ 中用边界数字表示 2D 网格的最佳方式?
- r - 如何用 IQR 内的值替换多个变量和个体的异常值
- swift - Swift:对 AnyObject 声明感到困惑