我意识到很多其他人已经尝试过并且未能通过查看建议的问题来制作顽皮和十字架,但我认为他们不会遇到与我相同的问题。
这是我的代码:
import random, time, sys
raw3 = ["-", "-", "-"]
raw2 = ["-", "-", "-"]
raw1 = ["-", "-", "-"]
def draw():
str3 = ""
str2 = ""
str1 = ""
for i in raw3:
str3 = (str3 + i + " ")
for i in raw2:
str2 = (str2 + i + " ")
for i in raw1:
str1 = (str1 + i + " ")
str_board = str3+str("\n"*2)+str(str2)+str("\n"*2)+str(str1)
print(str_board)
def playerX_turn():
c_choice_in = input("Type the coordinates in the format y/x\n(e.g. 2/1)")
c_choice_list = list(c_choice_in)
c_choice_x = int(c_choice_list[0]); c_choice_y = int(c_choice_list[2])
print(c_choice_x, c_choice_y)
if c_choice_x == 3 and raw3[c_choice_y - 1] == "-":
raw3[c_choice_y - 1] = "X"
elif c_choice_x == 2 and raw2[c_choice_y - 1] == "-":
raw2[c_choice_y - 1] = "X"
elif c_choice_x == 1 and raw1[c_choice_y - 1] == "-":
raw1[c_choice_y - 1] = "X"
def playerO_turn():
c_choice_in = input("Type the coordinates in the format y/x\n(e.g. 2/1)")
c_choice_list = list(c_choice_in)
c_choice_x = int(c_choice_list[0]); c_choice_y = int(c_choice_list[2])
print(c_choice_x, c_choice_y)
if c_choice_x == 3 and raw3[c_choice_y - 1] == "-":
raw3[c_choice_y - 1] = "O"
elif c_choice_x == 2 and raw2[c_choice_y - 1] == "-":
raw2[c_choice_y - 1] = "O"
elif c_choice_x == 1 and raw1[c_choice_y - 1] == "-":
raw1[c_choice_y - 1] = "O"
def check4win():
winner = None
if winner == None or winner == False and raw3[0] == "X" or raw3[2] == "X" and raw2[1] == "X" and raw1[0] == "X" or raw1[2] == "X":
winner = "playerX"
column_checker = 0
for i in range(0,3):
if winner == None or winner == False and raw3[column_checker] == "X" and raw2[column_checker] == "X" and raw1[column_checker] == "X":
winner = "playerX"
else:
column_checker += 1
if winner == None or winner == False:
if raw3[0] == "X" and raw3[1] == "X" and raw3[2] == "X":
winner = "playerX"
elif raw2[0] == "X" and raw2[1] == "X" and raw2[2] == "X":
winner = "playerX"
elif raw1[0] == "X" and raw1[1] == "X" and raw1[2] == "X":
winner = "playerX"
### ### ### ###
if winner == None or winner == False and raw3[0] == "Y" or raw3[2] == "Y" and raw2[1] == "Y" and raw1[0] == "Y" or raw1[2] == "Y":
winner = "playerY"
column_checker = 0
for i in range(0,3):
if winner == None or winner == False and raw3[column_checker] == "Y" and raw2[column_checker] == "Y" and raw1[column_checker] == "Y":
winner = "playerY"
else:
column_checker += 1
if winner == None or winner == False:
if raw3[0] == "Y" and raw3[1] == "Y" and raw3[2] == "Y":
winner = "playerY"
elif raw2[0] == "Y" and raw2[1] == "Y" and raw2[2] == "Y":
winner = "playerY"
elif raw1[0] == "Y" and raw1[1] == "Y" and raw1[2] == "Y":
winner = "playerY"
### ### ### ###
if winner != None or winner != False:
if winner == "playerX":
print("X won the game!\n(Sorry, O!)")
elif winner == "playerY":
print("Y won the game!\n(Bad luck, X!)")
sys.exit()
def main():
game_over = False
while game_over == False:
playerX_turn()
draw()
check4win()
playerO_turn()
draw()
check4win()
main()
所以我的第一个问题是这是一个输出示例:
>>>
Type the coordinates in the format y/x
(e.g. 2/1)1,1
1 1
- - -
- - -
X - -
X won the game!
(Sorry, O!)
>>>
哪个显然不正确......
这在某个地方可能是一个愚蠢的错误,但是我太懒了,无法正确地查看它,但我已经对check4win()
函数进行了简要介绍,但是无济于事
另一个(不那么重要)的事情是我觉得这个代码可以做得更整洁......也许有main()
的东西?
答案 0 :(得分:0)
check4win
始终为True
:if winner != None or winner != False
无法False
。即使获胜者为None
或False
,至少有一个表达式会评估为True
。说到这一点,winner
似乎不能False
开始。
另一个不那么重要的事情:你可以通过避免复制/粘贴来制作更整洁的代码。对于初学者来说,你对玩家1和游戏2具有完全相同的功能 - 只需参数化他们的玩家编号和符号。
def player_turn(player_number, player_symbol):
do_stuff
同样适用于check4win
。另外,使用游戏板的实际多维数组,即
board = [["-"]*3]*3
允许您检查相对位置,而不必硬编码模式。
在旁注中,由于我不允许直接对实际问题发表评论:“我太懒了,不能正确地仔细研究”,认真吗?