[python]代码库
import random
print(" 1 | 2 | 3 ")
print("-----------")
print(" 4 | 5 | 6 ")
print("-----------")
print(" 7 | 8 | 9 ")
coin = random.randint(0,1)
chessList = list(range(10))
count = 0
while True:
count += 1
if coin == 0:
while True:
user = input("玩家出子:")
idx = random.randint(0,len(chessList) - 1)
if int(user) == chessList[int(user)]:
chessList[int(user)] = "o"
coin = 1
break
else:
print("此处有子,请重新出子!")
elif coin == 1:
print("电脑出子:")
while True:
idx = random.randint(0,len(chessList) - 1)
if idx == chessList[idx]:
chessList[idx] = "x"
coin = 0
break
print(" " + str(chessList[1]) + " | " + str(chessList[2]) + " | " + str(chessList[3]) + " ")
print("-----------")
print(" " + str(chessList[4]) + " | " + str(chessList[5]) + " | " + str(chessList[6]) + " ")
print("-----------")
print(" " + str(chessList[7]) + " | " + str(chessList[8]) + " | " + str(chessList[9]) + " ")
win1 = chessList[1] == chessList[2] == chessList[3]
win2 = chessList[4] == chessList[5] == chessList[6]
win3 = chessList[7] == chessList[8] == chessList[9]
win4 = chessList[1] == chessList[4] == chessList[7]
win5 = chessList[2] == chessList[5] == chessList[8]
win6 = chessList[3] == chessList[6] == chessList[9]
win7 = chessList[1] == chessList[5] == chessList[9]
if win1 or win2 or win3 or win4 or win5 or win6 or win7 or win8:
if coin == 1:
print("玩家获胜")
else:
print("电脑获胜")
break
elif count == 9:
print("和棋")
break