嗨我在编写tic tac toe游戏时遇到了一些麻烦,计算机通过选择0到1之间的数字来随机播放。我尝试了一个代码,但我得到了这样的虚幻情况:
O O O
X X X
O O O
我想在我的代码中加入一些内容,检查每次移动是否都有赢家。如果有人获胜,我希望游戏停止并告诉谁是赢家或者没有人获胜。而且我也不希望输出如下图所示。我只想要一块单板显示器(如果可能的话)。
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()
答案 0 :(得分:0)
您的Winner()
函数不会返回任何内容,因此默认情况下,无论输入是什么,它都会隐式返回False
。它应该包含return True
而不仅仅是True
(根本不做任何事情),例如:
def Winner(G):
if check(G, 0, 1, 2) or check(G, 0, 3, 6) or check(G, 0, 4, 8) or check(G, 1, 4, 7) or check(G, 2, 5, 8) or check(G, 2, 4, 6) or check(G, 3, 4, 5) or check(G, 6, 7, 8):
return True
else
return False
重点是应return True
。剩下的主要是风格偏好 - 我更喜欢单if
,有很多or
条件。其他人可能更喜欢使用if
的许多return True
语句。
(从技术上讲,你不需要else
部分,因为它无论如何都会有效地返回False
。但它会使它更具可读性。你也可以只删除{{ 1}}行,并反向缩进else
,这使得它成为整个函数的全部捕获而不是return False
。)
答案 1 :(得分:0)
代码:
import random
board = []
for x in range(9):
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])
def check(A, B, C, D):
if board[B] == A and board[C] == A and board[D] == A :
return True
def Winner(G):
ret = False
if check(G, 0, 1, 2):
ret = True
if check(G, 0, 3, 6):
ret = True
if check(G, 0, 4, 8):
ret = True
if check(G, 1, 4, 7):
ret = True
if check(G, 2, 5, 8):
ret = True
if check(G, 2, 4, 6):
ret = True
if check(G, 3, 4, 5):
ret = True
if check(G, 6, 7, 8):
ret = True
return ret
def filled():
ll = 0
for x in board:
if x == " ":
ll = 11 + 1
if ll > 0:
return False
else:
return True
done = False
turn = "O"
tie = False
while done == False:
place = random.randint(0, 8)
if board[place] == " ":
board[place] = turn
if Winner(turn) == True:
done = True
else:
if filled() == True:
done = True
tie = True
else:#change turn
if turn == "X":
turn = "O"
else:
turn = "X"
if tie == True:
print ("The game was a tie. No winner.")
show()
else:
print("~~~~~~~ the player '",turn,"' wins~~~~~~~")
show()
这应该可以满足您的所有要求。
在每次有效转弯后检查获胜者()和填充()。在每次成功循环之后转换循环,以防止重复。
注意:'O'首先,在循环开始之前将更改变量更改为'X'。