嗨我在编码tic tac toe游戏时遇到了一些麻烦,其中计算机通过选择0到1之间的数字随机播放。我尝试了一个代码,但是我得到了如下代表的虚幻情形:Tic Tac Toe WinCheck Def
OOO
XXX
OOO
我希望把我的代码,检查是否存在一举一动赢家东西。如果有人获胜,我希望比赛停止并告诉谁是胜利者,或者谁没有赢。而且我也不希望输出如下图所示。我只想要一块显示板(如果可能的话)。
The output of this programme is that.
这里是我的代码:
import random
board = []
line= 3
col = 3
for i in range (line) :
for j in range (col):
board.append(" ")
def show():
print(board[0],'|',board[1],'|',board[2])
print('--------------')
print(board[3],'|',board[4],'|',board[5])
print('--------------')
print(board[6],'|',board[7],'|',board[8])
print()
# Verify the possibility of position to take.
B=1
C=2
D=3
def check(A, B, C, D):
if board[B] == A and board[C] == A and board[D] == A :
return True
# Win conditions
def Winner(G):
if check(G, 0, 1, 2):
True
if check(G, 0, 3, 6):
True
if check(G, 0, 4, 8):
True
if check(G, 1, 4, 7):
True
if check(G, 2, 5, 8):
True
if check(G, 2, 4, 6):
True
if check(G, 3, 4, 5):
True
if check(G, 6, 7, 8):
True
player1 = "O"
player2 = "X"
play=random.randint(0,1)
if play == 1:
player1 = "X"
player2 = "O"
while True:
random.seed()
place=random.randint(0,8)
if board[place] != "O" and board[place] != "X" :
board[place] = player1
if Winner(player1)==True:
print("~~~~~~~ the player ",player1," wins~~~~~~~")
break;
while True:
random.seed()
place=random.randint(0,8)
if board[place] != "O" and board[place] != "X" :
board[place] = player2
if Winner(player2)==True:
print("~~~~~~~ the player ",player2," wins~~~~~~~")
break;
break;
else:
print("This position is already taken")
show()
作为一个声明''真''完全没有。你需要''返回True''才能真正表明胜利。 (另外,在循环中调用''random.seed()''是一个非常糟糕的主意 - 每次运行一个程序就足够了,或者是零,因为这已经完成了。) – jasonharper