井字停止,不知道为什么。 Python 非常新,还没有看过 Tic Tac Toe 的任何代码示例
Tic Tac Toe stopping and not sure why. Very new to Python and have not looked at any code examples for Tic Tac Toe
这是我 Python 职业生涯中的一个非常早期的项目。只看了几个教程然后就跳进去了所以ELI5请。
它转了几圈,然后就死机了,我不知道为什么。它轮流然后说什么时候有赢家。有一个具有真实机器人逻辑的机器人。
游戏首先定义一个点数组,这是用户看到的。有 10 个点,所以我更容易使用阵列点 1 作为第一个点(我知道阵列使用不当)。 spotBooleans 有一个类似的东西,用于检查一个点是否被占用。 90 年代的数字是默认值。当玩家放置一个点时,它会将其变为 int 1,而敌人将变为 int 2。
大部分动作都在播放循环的底部,它会打印大部分数据并询问您要去哪里。然后它会在你离开后检查获胜者,并触发使用基本算法和逻辑尝试像人类一样获胜的机器人。目前没有任何东西可以阻止人类的行动。然后它会检查机器人是否获胜。
它在这里的某个地方停了下来,我找不到原因。
同样,我知道这是糟糕的代码。我知道一些 Java 但我目前还不擅长 Python。只是试图让逻辑正确,然后接下来会出现干净的代码以及简化的事情。
import random
import time
spots = ["", "-", "-", "-", "-", "-", "-", "-", "-", "-"]
spotBooleans = [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
start = False
whosturn = 1
while True:
spots = ["", "-", "-", "-", "-", "-", "-", "-", "-", "-"]
spotBooleans = [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
start = False
whosturn = 1
while start == False:
playerchoice = input("Would you like to be 'x' or 'o'")
x = False
o = False
if playerchoice == "x" or playerchoice == "X":
x = True
print("Player is X")
start = True
elif playerchoice == "o" or playerchoice == "O" or playerchoice == "0":
o = True
print("Player is O")
start = True
else:
print("Try again.\n\n")
start = False
print("Starting: ")
playing = True
def gorobot(whosturn):
global x
global spots
global spotBooleans
global playing
global start
if x:
robotsymbol = "o"
else:
robotsymbol = "x"
while whosturn == 2:
if spotBooleans[1] == spotBooleans[2] == 2:
if spots[3] == "-":
spots[3] = robotsymbol
spotBooleans[3] = 2
whosturn = 1
elif spotBooleans[3] == spotBooleans[2] == 2:
if spots[1] == "-":
spots[1] = robotsymbol
spotBooleans[1] = 2
whosturn = 1
elif spotBooleans[1] == spotBooleans[3] == 2:
if spots[2] == "-":
spots[2] = robotsymbol
spotBooleans[2] = 2
whosturn = 1
elif spotBooleans[4] == spotBooleans[5] == 2:
if spots[6] == "-":
spots[6] = robotsymbol
spotBooleans[6] = 2
whosturn = 1
elif spotBooleans[4] == spotBooleans[6] == 2:
if spots[5] == "-":
spots[5] = robotsymbol
spotBooleans[5] = 2
whosturn = 1
elif spotBooleans[5] == spotBooleans[6] == 2:
if spots[4] == "-":
spots[4] = robotsymbol
spotBooleans[4] = 2
whosturn = 1
elif spotBooleans[7] == spotBooleans[8] == 2:
if spots[9] == "-":
spots[9] = robotsymbol
spotBooleans[9] = 2
whosturn = 1
elif spotBooleans[7] == spotBooleans[9] == 2:
if spots[8] == "-":
spots[8] = robotsymbol
spotBooleans[8] = 2
whosturn = 1
elif spotBooleans[8] == spotBooleans[9] == 2:
if spots[7] == "-":
spots[7] = robotsymbol
spotBooleans[7] = 2
whosturn = 1
elif spotBooleans[1] == spotBooleans[5] == 2:
if spots[9] == "-":
spots[9] = robotsymbol
spotBooleans[9] = 2
whosturn = 1
elif spotBooleans[9] == spotBooleans[5] == 2:
if spots[1] == "-":
spots[1] = robotsymbol
spotBooleans[1] = 2
whosturn = 1
elif spotBooleans[9] == spotBooleans[1] == 2:
if spots[5] == "-":
spots[5] = robotsymbol
spotBooleans[5] = 2
whosturn = 1
elif spotBooleans[3] == spotBooleans[5] == 2:
if spots[7] == "-":
spots[7] = robotsymbol
spotBooleans[7] = 2
whosturn = 1
elif spotBooleans[7] == spotBooleans[5] == 2:
if spots[3] == "-":
spots[3] = robotsymbol
spotBooleans[3] = 2
whosturn = 1
elif spotBooleans[7] == spotBooleans[3] == 2:
if spots[5] == "-":
spots[5] = robotsymbol
spotBooleans[5] = 2
whosturn = 1
elif spotBooleans[3] == spotBooleans[6] == 2:
if spots[9] == "-":
spots[9] = robotsymbol
spotBooleans[9] = 2
whosturn = 1
elif spotBooleans[3] == spotBooleans[9] == 2:
if spots[6] == "-":
spots[6] = robotsymbol
spotBooleans[6] = 2
whosturn = 1
elif spotBooleans[6] == spotBooleans[9] == 2:
if spots[3] == "-":
spots[3] = robotsymbol
spotBooleans[3] = 2
whosturn = 1
elif spotBooleans[1] == spotBooleans[4] == 2:
if spots[7] == "-":
spots[7] = robotsymbol
spotBooleans[7] = 2
whosturn = 1
elif spotBooleans[1] == spotBooleans[7] == 2:
if spots[4] == "-":
spots[4] = robotsymbol
spotBooleans[4] = 2
whosturn = 1
elif spotBooleans[4] == spotBooleans[7] == 2:
if spots[1] == "-":
spots[1] = robotsymbol
spotBooleans[1] = 2
whosturn = 1
elif spotBooleans[2] == spotBooleans[5] == 2:
if spots[8] == "-":
spots[8] = robotsymbol
spotBooleans[8] = 2
whosturn = 1
elif spotBooleans[2] == spotBooleans[8] == 2:
if spots[5] == "-":
spots[5] = robotsymbol
spotBooleans[5] = 2
whosturn = 1
elif spotBooleans[5] == spotBooleans[8] == 2:
if spots[2] == "-":
spots[2] = robotsymbol
spotBooleans[2] = 2
whosturn = 1
else:
botMove = random.randint(0, 9)
if spots[botMove] == "-":
spots[botMove] = robotsymbol
spotBooleans[botMove] = 2
whosturn = 1
else:
print("The Bot is a moron! " + str(botMove))
def checkforwinners():
if spotBooleans[1] == spotBooleans[2] == spotBooleans[3]:
print("Winner!")
return True
elif spotBooleans[4] == spotBooleans[5] == spotBooleans[6]:
print("Winner!")
return True
elif spotBooleans[7] == spotBooleans[8] == spotBooleans[9]:
print("Winner!")
return True
elif spotBooleans[1] == spotBooleans[4] == spotBooleans[7]:
print("Winner!")
return True
elif spotBooleans[2] == spotBooleans[5] == spotBooleans[8]:
print("Winner!")
return True
elif spotBooleans[3] == spotBooleans[6] == spotBooleans[9]:
print("Winner!")
return True
elif spotBooleans[1] == spotBooleans[5] == spotBooleans[9]:
print("Winner!")
return True
elif spotBooleans[3] == spotBooleans[5] == spotBooleans[7]:
print("Winner!")
return True
else:
print("No Winners!")
print(spots)
print(spotBooleans)
return False
while playing:
print(str(spots[1]) + " " + str(spots[2]) + " " + str(spots[3]))
print(str(spots[4]) + " " + str(spots[5]) + " " + str(spots[6]))
print(str(spots[7]) + " " + str(spots[8]) + " " + str(spots[9]))
print("\n")
spotChoice = input("Where would you like to place your " + playerchoice + "?")
spot = int(spotChoice)
if spots[spot] == "-":
if x:
spots[spot] = "x"
spotBooleans[spot] = 1
if checkforwinners():
playing = False
start = False
break
whosturn = 2
gorobot(2)
else:
spots[spot] = "o"
spotBooleans[spot] = 1
if checkforwinners():
playing = False
start = False
break
whosturn = 2
gorobot(2)
if checkforwinners():
playing = False
start = False
在某些情况下,您的代码会导致 gorobot
函数内部出现无限循环。
例如,经过几次尝试,我得到了一个 spotBooleans
列表,如下所示:
[90, 1, 1, 93, 2, 1, 2, 97, 98, 99]
还有一个 spots
列表,如下所示:
['', 'x', 'x', '-', 'o', 'x', 'o', '-', '-', '-']
在该状态下,当代码进入 gorobot
内的 while 循环时,它会经过此代码路径。
elif spotBooleans[4] == spotBooleans[6] == 2:
if spots[5] == "-":
spots[5] = robotsymbol
spotBooleans[5] = 2
whosturn = 1
spotBooleans[4]
和spotBooleans[6]
都是2,所以进入了elif
块,但是spots[5]
是“x”,所以没有进入内层if
块。这意味着 whosturn
变量未更新,因此代码不会退出 while whosturn == 2:
循环。 spotBooleans
和 spots
列表没有更新,因此在下一轮循环中程序遵循完全相同的代码路径。
这是我 Python 职业生涯中的一个非常早期的项目。只看了几个教程然后就跳进去了所以ELI5请。
它转了几圈,然后就死机了,我不知道为什么。它轮流然后说什么时候有赢家。有一个具有真实机器人逻辑的机器人。
游戏首先定义一个点数组,这是用户看到的。有 10 个点,所以我更容易使用阵列点 1 作为第一个点(我知道阵列使用不当)。 spotBooleans 有一个类似的东西,用于检查一个点是否被占用。 90 年代的数字是默认值。当玩家放置一个点时,它会将其变为 int 1,而敌人将变为 int 2。
大部分动作都在播放循环的底部,它会打印大部分数据并询问您要去哪里。然后它会在你离开后检查获胜者,并触发使用基本算法和逻辑尝试像人类一样获胜的机器人。目前没有任何东西可以阻止人类的行动。然后它会检查机器人是否获胜。
它在这里的某个地方停了下来,我找不到原因。
同样,我知道这是糟糕的代码。我知道一些 Java 但我目前还不擅长 Python。只是试图让逻辑正确,然后接下来会出现干净的代码以及简化的事情。
import random
import time
spots = ["", "-", "-", "-", "-", "-", "-", "-", "-", "-"]
spotBooleans = [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
start = False
whosturn = 1
while True:
spots = ["", "-", "-", "-", "-", "-", "-", "-", "-", "-"]
spotBooleans = [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
start = False
whosturn = 1
while start == False:
playerchoice = input("Would you like to be 'x' or 'o'")
x = False
o = False
if playerchoice == "x" or playerchoice == "X":
x = True
print("Player is X")
start = True
elif playerchoice == "o" or playerchoice == "O" or playerchoice == "0":
o = True
print("Player is O")
start = True
else:
print("Try again.\n\n")
start = False
print("Starting: ")
playing = True
def gorobot(whosturn):
global x
global spots
global spotBooleans
global playing
global start
if x:
robotsymbol = "o"
else:
robotsymbol = "x"
while whosturn == 2:
if spotBooleans[1] == spotBooleans[2] == 2:
if spots[3] == "-":
spots[3] = robotsymbol
spotBooleans[3] = 2
whosturn = 1
elif spotBooleans[3] == spotBooleans[2] == 2:
if spots[1] == "-":
spots[1] = robotsymbol
spotBooleans[1] = 2
whosturn = 1
elif spotBooleans[1] == spotBooleans[3] == 2:
if spots[2] == "-":
spots[2] = robotsymbol
spotBooleans[2] = 2
whosturn = 1
elif spotBooleans[4] == spotBooleans[5] == 2:
if spots[6] == "-":
spots[6] = robotsymbol
spotBooleans[6] = 2
whosturn = 1
elif spotBooleans[4] == spotBooleans[6] == 2:
if spots[5] == "-":
spots[5] = robotsymbol
spotBooleans[5] = 2
whosturn = 1
elif spotBooleans[5] == spotBooleans[6] == 2:
if spots[4] == "-":
spots[4] = robotsymbol
spotBooleans[4] = 2
whosturn = 1
elif spotBooleans[7] == spotBooleans[8] == 2:
if spots[9] == "-":
spots[9] = robotsymbol
spotBooleans[9] = 2
whosturn = 1
elif spotBooleans[7] == spotBooleans[9] == 2:
if spots[8] == "-":
spots[8] = robotsymbol
spotBooleans[8] = 2
whosturn = 1
elif spotBooleans[8] == spotBooleans[9] == 2:
if spots[7] == "-":
spots[7] = robotsymbol
spotBooleans[7] = 2
whosturn = 1
elif spotBooleans[1] == spotBooleans[5] == 2:
if spots[9] == "-":
spots[9] = robotsymbol
spotBooleans[9] = 2
whosturn = 1
elif spotBooleans[9] == spotBooleans[5] == 2:
if spots[1] == "-":
spots[1] = robotsymbol
spotBooleans[1] = 2
whosturn = 1
elif spotBooleans[9] == spotBooleans[1] == 2:
if spots[5] == "-":
spots[5] = robotsymbol
spotBooleans[5] = 2
whosturn = 1
elif spotBooleans[3] == spotBooleans[5] == 2:
if spots[7] == "-":
spots[7] = robotsymbol
spotBooleans[7] = 2
whosturn = 1
elif spotBooleans[7] == spotBooleans[5] == 2:
if spots[3] == "-":
spots[3] = robotsymbol
spotBooleans[3] = 2
whosturn = 1
elif spotBooleans[7] == spotBooleans[3] == 2:
if spots[5] == "-":
spots[5] = robotsymbol
spotBooleans[5] = 2
whosturn = 1
elif spotBooleans[3] == spotBooleans[6] == 2:
if spots[9] == "-":
spots[9] = robotsymbol
spotBooleans[9] = 2
whosturn = 1
elif spotBooleans[3] == spotBooleans[9] == 2:
if spots[6] == "-":
spots[6] = robotsymbol
spotBooleans[6] = 2
whosturn = 1
elif spotBooleans[6] == spotBooleans[9] == 2:
if spots[3] == "-":
spots[3] = robotsymbol
spotBooleans[3] = 2
whosturn = 1
elif spotBooleans[1] == spotBooleans[4] == 2:
if spots[7] == "-":
spots[7] = robotsymbol
spotBooleans[7] = 2
whosturn = 1
elif spotBooleans[1] == spotBooleans[7] == 2:
if spots[4] == "-":
spots[4] = robotsymbol
spotBooleans[4] = 2
whosturn = 1
elif spotBooleans[4] == spotBooleans[7] == 2:
if spots[1] == "-":
spots[1] = robotsymbol
spotBooleans[1] = 2
whosturn = 1
elif spotBooleans[2] == spotBooleans[5] == 2:
if spots[8] == "-":
spots[8] = robotsymbol
spotBooleans[8] = 2
whosturn = 1
elif spotBooleans[2] == spotBooleans[8] == 2:
if spots[5] == "-":
spots[5] = robotsymbol
spotBooleans[5] = 2
whosturn = 1
elif spotBooleans[5] == spotBooleans[8] == 2:
if spots[2] == "-":
spots[2] = robotsymbol
spotBooleans[2] = 2
whosturn = 1
else:
botMove = random.randint(0, 9)
if spots[botMove] == "-":
spots[botMove] = robotsymbol
spotBooleans[botMove] = 2
whosturn = 1
else:
print("The Bot is a moron! " + str(botMove))
def checkforwinners():
if spotBooleans[1] == spotBooleans[2] == spotBooleans[3]:
print("Winner!")
return True
elif spotBooleans[4] == spotBooleans[5] == spotBooleans[6]:
print("Winner!")
return True
elif spotBooleans[7] == spotBooleans[8] == spotBooleans[9]:
print("Winner!")
return True
elif spotBooleans[1] == spotBooleans[4] == spotBooleans[7]:
print("Winner!")
return True
elif spotBooleans[2] == spotBooleans[5] == spotBooleans[8]:
print("Winner!")
return True
elif spotBooleans[3] == spotBooleans[6] == spotBooleans[9]:
print("Winner!")
return True
elif spotBooleans[1] == spotBooleans[5] == spotBooleans[9]:
print("Winner!")
return True
elif spotBooleans[3] == spotBooleans[5] == spotBooleans[7]:
print("Winner!")
return True
else:
print("No Winners!")
print(spots)
print(spotBooleans)
return False
while playing:
print(str(spots[1]) + " " + str(spots[2]) + " " + str(spots[3]))
print(str(spots[4]) + " " + str(spots[5]) + " " + str(spots[6]))
print(str(spots[7]) + " " + str(spots[8]) + " " + str(spots[9]))
print("\n")
spotChoice = input("Where would you like to place your " + playerchoice + "?")
spot = int(spotChoice)
if spots[spot] == "-":
if x:
spots[spot] = "x"
spotBooleans[spot] = 1
if checkforwinners():
playing = False
start = False
break
whosturn = 2
gorobot(2)
else:
spots[spot] = "o"
spotBooleans[spot] = 1
if checkforwinners():
playing = False
start = False
break
whosturn = 2
gorobot(2)
if checkforwinners():
playing = False
start = False
在某些情况下,您的代码会导致 gorobot
函数内部出现无限循环。
例如,经过几次尝试,我得到了一个 spotBooleans
列表,如下所示:
[90, 1, 1, 93, 2, 1, 2, 97, 98, 99]
还有一个 spots
列表,如下所示:
['', 'x', 'x', '-', 'o', 'x', 'o', '-', '-', '-']
在该状态下,当代码进入 gorobot
内的 while 循环时,它会经过此代码路径。
elif spotBooleans[4] == spotBooleans[6] == 2:
if spots[5] == "-":
spots[5] = robotsymbol
spotBooleans[5] = 2
whosturn = 1
spotBooleans[4]
和spotBooleans[6]
都是2,所以进入了elif
块,但是spots[5]
是“x”,所以没有进入内层if
块。这意味着 whosturn
变量未更新,因此代码不会退出 while whosturn == 2:
循环。 spotBooleans
和 spots
列表没有更新,因此在下一轮循环中程序遵循完全相同的代码路径。