class game:
gamestillgoing=True
if gamestillgoing:
def __init__(self, board, turn):
self.board = ["#", "-", "-", "-",
"-", "-", "-",
"-", "-", "-"]
self.turn = input("Choose your turn X or O?\n")
print("Player {} goes first!".format(turn))
def displayboard(self):
print(self.board[1] + "|" + self.board[2] + "|" + self.board[3])
print(self.board[4] + "|" + self.board[5] + "|" + self.board[6])
print(self.board[7] + "|" + self.board[8] + "|" + self.board[9])
game.displayboard(self)
def choosepositionX(self):
position = int(input("Choose the position from 1-9\n"))
position = int(position)
if self.turn == "X":
self.board[position] = "X"
game.displayboard(self)
else:
while not self.turn == "X":
print("Choose the valid input.")
def choosepositonO(self):
position = int(input("Choose the position from 1-9\n"))
position = int(position)
if self.turn == "O":
self.board[position] = "O"
game.displayboard(self)
else:
while not self.turn == "O":
print("Choose the valid input.")
def checkwinrow(self):
row1 = self.board[1] == self.board[2] == self.board[3]
row2 = self.board[4] == self.board[5] == self.board[6]
row3 = self.board[7] == self.board[8] == self.board[9]
if row1 or row2 or row3 == "X":
print("Player X has won!\n ")
gamestillgoing = False
elif row1 or row2 or row3 == "O":
print("Player O has won!\n")
gamestillgoing = False
else:
gamestillgoing = True
def checkwincolumn(self):
column1 = self.board[1] == self.board[4] == self.board[7]
column2 = self.board[2] == self.board[5] == self.board[8]
column3 = self.board[3] == self.board[6] == self.board[9]
if column1 or column2 or column3 == "X":
print("Player X has won!\n")
gamestillgoing = False
elif column1 or column2 or column3 == "O":
print("Player O has won!\n")
gamestillgoing = False
else:
gamestillgoing = True
def checkwindiagonal(self):
diagonal1 = self.board[1] == self.board[5] == self.board[9]
diagonal2 = self.board[3] == self.board[5] == self.board[7]
if diagonal1 or diagonal2 == "X":
print("Player X has won!\n")
gamestillgoing = False
elif diagonal1 or diagonal2 == "O":
print("Player O has win!\n")
gamestillgoing = False
else:
gamestillgoing = True
def checktie(self):
row1 = self.board[1] == self.board[2] == self.board[3]
row2 = self.board[4] == self.board[5] == self.board[6]
row3 = self.board[7] == self.board[8] == self.board[9]
column1 = self.board[1] == self.board[4] == self.board[7]
column2 = self.board[2] == self.board[5] == self.board[8]
column3 = self.board[3] == self.board[6] == self.board[9]
diagonal1 = self.board[1] == self.board[5] == self.board[9]
diagonal2 = self.board[3] == self.board[5] == self.board[7]
if row1 == row2 == row3 or column1 == column2 == column3 or diagonal1 == diagonal2 != "X" or "O":
print("The game is Tie!")
gamestillgoing = False
else:
gamestillgoing = True
def playerturn(self):
if self.turn == "X":
game.choosepositionX(self)
elif self.turn == "O":
game.choosepositonO(self)
else:
"Choose the valid Input!\n"
game.playerturn(self)
else:
gamestillgoing=False
Start=game()
Start()
[当我尝试运行此游戏时,它说最后两行有错误。谁能帮我解决这个问题?我只需要最后两行中错误的解决方案(即Start = game()开始())主要问题在于调用类。
[不,您通常不能在Python中“调用”一个类。但是您可以调用其方法(包括其构造函数)。 (如果您确实必须调用一个类或其实例,请单击you have some extra work to do。
您的代码中有许多语法,逻辑和体系结构错误。
您将gamestillgoing
用作类属性(而不是成员属性)在技术上不是不可能的,但不建议这样做。你应该使gamestillgoing
成为成员,或者更好的是成为属性。
您在displayboard()
中具有无限递归。
您有2-5个空格的缩进。您应该始终缩进4个空格。
您不能只给班级打电话一次。您需要重复调用playerturn()
方法,直到gamestillgoing
为False
。
我清理了这些问题。这远不是一个可行的游戏(例如,它还没有交替的回合-您需要在每个玩家回合之后这样做),但这足以让您摆脱困境。
class game:
def __init__(self):
self._gamestillgoing = True
self.board = ["#", "-", "-", "-",
"-", "-", "-",
"-", "-", "-"]
self.turn = input("Choose your turn X or O?\n")
print("Player {} goes first!".format(self.turn))
@property
def gamestillgoing(self):
return self._gamestillgoing
def displayboard(self):
print(self.board[1] + "|" + self.board[2] + "|" + self.board[3])
print(self.board[4] + "|" + self.board[5] + "|" + self.board[6])
print(self.board[7] + "|" + self.board[8] + "|" + self.board[9])
def choosepositionX(self):
position = int(input("Choose the position from 1-9\n"))
position = int(position)
if self.turn == "X":
self.board[position] = "X"
game.displayboard(self)
else:
while not self.turn == "X":
print("Choose the valid input.")
def choosepositonO(self):
position = int(input("Choose the position from 1-9\n"))
position = int(position)
if self.turn == "O":
self.board[position] = "O"
game.displayboard(self)
else:
while not self.turn == "O":
print("Choose the valid input.")
def checkwinrow(self):
row1 = self.board[1] == self.board[2] == self.board[3]
row2 = self.board[4] == self.board[5] == self.board[6]
row3 = self.board[7] == self.board[8] == self.board[9]
if row1 or row2 or row3 == "X":
print("Player X has won!\n ")
self._gamestillgoing = False
elif row1 or row2 or row3 == "O":
print("Player O has won!\n")
self._gamestillgoing = False
else:
self._gamestillgoing = True
def checkwincolumn(self):
column1 = self.board[1] == self.board[4] == self.board[7]
column2 = self.board[2] == self.board[5] == self.board[8]
column3 = self.board[3] == self.board[6] == self.board[9]
if column1 or column2 or column3 == "X":
print("Player X has won!\n")
self._gamestillgoing = False
elif column1 or column2 or column3 == "O":
print("Player O has won!\n")
self._gamestillgoing = False
else:
self._gamestillgoing = True
def checkwindiagonal(self):
diagonal1 = self.board[1] == self.board[5] == self.board[9]
diagonal2 = self.board[3] == self.board[5] == self.board[7]
if diagonal1 or diagonal2 == "X":
print("Player X has won!\n")
self._gamestillgoing = False
elif diagonal1 or diagonal2 == "O":
print("Player O has win!\n")
self._gamestillgoing = False
else:
self._gamestillgoing = True
def checktie(self):
row1 = self.board[1] == self.board[2] == self.board[3]
row2 = self.board[4] == self.board[5] == self.board[6]
row3 = self.board[7] == self.board[8] == self.board[9]
column1 = self.board[1] == self.board[4] == self.board[7]
column2 = self.board[2] == self.board[5] == self.board[8]
column3 = self.board[3] == self.board[6] == self.board[9]
diagonal1 = self.board[1] == self.board[5] == self.board[9]
diagonal2 = self.board[3] == self.board[5] == self.board[7]
if row1 == row2 == row3 or column1 == column2 == column3 or diagonal1 == diagonal2 != "X" or "O":
print("The game is Tie!")
self._gamestillgoing = False
else:
self._gamestillgoing = True
def playerturn(self):
if self.turn == "X":
self.choosepositionX()
elif self.turn == "O":
self.choosepositonO()
else:
"Choose the valid Input!\n"
self.playerturn(self)
g=game()
while g.gamestillgoing:
g.playerturn()