除了一些问题,我的Tic tac toe代码工作正常。在板上标记点时,用户可以覆盖并将自己的标记放在同一位置。即使我有代码应该解决这个问题。这是我的代码。请根据需要修正任何其他错误。我几乎没有上课。
board = [['-','-','-']
,['-','-','-']
,['-','-','-']]
player1 = 'X'
player2 = 'O'
win = False
turns = 0
player1= str(input("Whats ur name+"))
player2= str(input("Whats ur name"))
def checkwin(player):
for c in range(0,3):
if board[c][0] == player and board[c][1] == player and board[c][2] == player:
print "*********\n\n%s wins\n\n*********" % player
playerwin = True
return playerwin
elif board[0][c] == player and board[1][c] == player and board[2][c] == player:
print "*********\n\n%s wins\n\n*********" % player
playerwin = True
return playerwin
#check for diagonal win
elif board[0][0] == player and board[1][1] == player and board[2][2] == player:
print "*********\n\n%s wins\n\n*********" % player
playerwin = True
return playerwin
#check for diagonal win (right to left)
elif board[0][2] == player and board[1][1] == player and board[2][0] == player:
print "*********\n\n%s wins\n\n*********" % player
playerwin = True
return playerwin
else:
playerwin = False
return playerwin
def playerturn(player):
print "%s's turn" % player
turn = 1
while(turn):
print "Select column [1-3]: ",
col = int(raw_input()) - 1
print "Select row [1-3]: ",
row = int(raw_input()) - 1
if board[row][col] == "X" or board[row][col] == "O":
print "Already taken!"
else:
board[row][col] = player
turn = 0
def printboard():
print board[0]
print board[1]
print board[2]
printboard()
while(win == False):
playerturn(player1)
turns += 1
printboard()
if checkwin(player1) == True: break
if turns == 9:
print "This game is a draw!"
break
playerturn(player2)
turns += 1
printboard()
checkwin(player2)
if checkwin(player2) == True: break
答案 0 :(得分:0)
您的问题是您已将播放器名称放入阵列中并检查阵列中是否有“X”或“O”。一个简单的解决方案是将if条件更改为... == player1而不是“X”。 如果这样做,您还必须调整绘图功能。
board = [['-','-','-']
,['-','-','-']
, ['-','-','-']]
#player1 = 'X'
#player2 = 'O'
win = False
turns = 0
player1= raw_input("Whats ur name+")
player2= raw_input("Whats ur name")
def checkwin(player):
for c in range(0,3):
if board[c][0] == player and board[c][1] == player and board[c][2] == player:
print "*********\n\n%s wins\n\n*********" % player
playerwin = True
return playerwin
elif board[0][c] == player and board[1][c] == player and board[2][c] == player:
print "*********\n\n%s wins\n\n*********" % player
playerwin = True
return playerwin
#check for diagonal win
elif board[0][0] == player and board[1][1] == player and board[2][2] == player:
print "*********\n\n%s wins\n\n*********" % player
playerwin = True
return playerwin
#check for diagonal win (right to left)
elif board[0][2] == player and board[1][1] == player and board[2][0] == player:
print "*********\n\n%s wins\n\n*********" % player
playerwin = True
return playerwin
else:
playerwin = False
return playerwin
def playerturn(player):
print "%s's turn" % player
turn = 1
while(turn):
print "Select column [1-3]: ",
col = int(raw_input()) - 1
print "Select row [1-3]: ",
row = int(raw_input()) - 1
if board[row][col] == player1 or board[row][col] == player2:
print "Already taken!"
else:
board[row][col] = player
turn = 0
def printboard():
print ["X" if x == player1 else "O" if x == player2 else "-" for x in board[0]]
print ["X" if x == player1 else "O" if x == player2 else "-" for x in board[1]]
print ["X" if x == player1 else "O" if x == player2 else "-" for x in board[2]]
printboard()
while(win == False):
playerturn(player1)
turns += 1
printboard()
if checkwin(player1) == True: break
if turns == 9:
print "This game is a draw!"
break
playerturn(player2)
turns += 1
printboard()
checkwin(player2)
if checkwin(player2) == True: break
我还将str(input())
替换为raw_input()
以允许字符串作为名称。