【发布时间】:2015-04-13 12:00:14
【问题描述】:
我正在尝试为井字游戏编写代码,但在模板上打印“X”时遇到困难...我的板已设置为具有 x 轴和 y 轴,我正在尝试要求用户输入 x 值和 y 值并使用该 x 和 y 值在板的正确区域打印“X”...有人可以帮我吗?
def printboard(board):
print(" 0 1 2 ")
print()
print(" 0 " + board[0][0] + " | " + board[0][1] +" | " + board[0][2] +" ")
print(" ---+---+---")
print(" 1 " + board[1][0] + " | " + board[1][1] +" | " + board[1][2] +" ")
print(" ---+---+---")
print(" 2 " + board[2][0] + " | " + board[2][1] +" | " + board[2][2] +" ")
print()
def isBoardFull(board):
print(board)
if board[0][0]== board[0][1] == board[0][2] != " "\
or board[1][0]== board[1][1] == board[1][2] !=" "\
or board[2][0]== board[2][1] == board[2][2] !=" "\
or board[0][0]== board[1][0] == board[2][0] != " "\
or board[0][1]== board[1][1] == board[2][1] !=" "\
or board[0][2]== board[1][2] == board[2][2] !=" "\
or board[0][0]== board[1][1]== board[2][2] !=" "\
or board [0][2]== board[1][1]== board[2][0] !=" ":
print("board is full")
return isBoardFull==True
else:
return isBoardFull==False
def isHorizontalTicTacToe(board):
if board[0][0]== board[0][1] == board[0][2] != " "\
or board[1][0]== board[1][1] == board[1][2] !=" "\
or board[2][0]== board[2][1] == board[2][2] !=" ":
print("You won!")
#isHorizontalTicTacToe([[" "," ", " "],["X", "X", "X"],[" ", " ", " "]])
def isVerticalTicTacToe(board):
if board[0][0]== board[1][0] == board[2][0] != " "\
or board[0][1]== board[1][1] == board[2][1] !=" "\
or board[0][2]== board[1][2] == board[2][2] !=" ":
print("You won!")
#isVerticalTicTacToe([[" ","X", " "],[" ", "X", " "],[" ", "X", " "]])
def isDiagonalTicTacToe(board):
if board[0][0]== board[1][1]==board[2][2] !=" "\
or board [0][2]== board[1][1]==board[2][0] !=" ":
print("You won!")
#isDiagonalTicTacToe([[" "," ", "X"],[" ", "X", " "],["X", " ", " "]])
def playerMove(board):
printboard([[" "," ", " "],[" ", " ", " "],[" ", " ", " "]])
print("Player 1 will be X")
print("Player 2 will be O")
playerOneH=int(input("Player One, enter X coordinates of move "))
playerOneV=int(input("Player One, enter Y coordinates of move "))
printboard[playerOneH,playerOneV]="X"
playerMove(1)
def playOneGame():
printboard([[" "," ", " "],[" ", " ", " "],[" ", " ", " "]])
#playerOneH=input("Player One, enter a horizontal coordinates for the first move ")
#playerOneV=input("Now enter a vertical coordinate for the first move ")
#playOneGame()
def main():
# this function should contain a loop that runs playOneGame until the user says he does not want to play anymore
pass
【问题讨论】:
-
我注意到的一件事:
return isBoardFull == True可能不会像您认为的那样做。它将函数与布尔值 True 进行比较,并返回结果 (False)。只需适当输入return True或return False。
标签: python input tic-tac-toe