如何将数据永久存储在 class python 中
How do I permanently store data in a class python
我必须在 Python 中创建一个推箱子游戏,并且我在我的推箱子 class 中定义了方法 find_player() 、 complete() 、 get_steps () 和移动() 。我需要创建一个 restart() 方法和一个 undo() 方法,但我不知道该怎么做。我找不到存储原始板或以前的板的方法。我尝试在 __init__
中定义另一个板,但它只是用 self.__board
更新而不是保存初始板。我还尝试在 __init__
中制作一个列表,并尝试将每个移动的“棋盘”附加到列表中,但它会更改列表中的每个棋盘。如果有人可以提供帮助,我附上了我的代码。
class Sokoban:
"""Your code here"""
def __init__(self, board):
self.__board = board
self.__original_board = board
self.__steps = 0
self.__last_step = []
self.__position = (0, 0)
def restart(self):
first_board = self.__original[0]
new_board = []
for i in range(len(first_board)):
new_board.append(first_board[i])
print(self.__original)
return Sokoban(self.__original_board)
def undo(self):
return
def main(board):
game = Sokoban(board)
message = 'Press w/a/s/d to move, r to restart, or u to undo'
print(message)
while not game.complete():
print(game)
move = input('Move: ').lower()
while move not in ('w', 'a', 's', 'd', 'r', 'u'):
print('Invalid move.', message)
move = input('Move: ').lower()
if move == 'r':
game.restart()
elif move == 'u':
game.undo()
else:
game.move(move)
print(game)
print(f'Game won in {game.get_steps()} steps!')
test_board = [
['*', '*', '*', '*', '*', '*', '*', '*'],
['*', ' ', ' ', ' ', ' ', ' ', ' ', '*'],
['*', 'P', ' ', '#', ' ', ' ', ' ', '*'],
['*', '*', '*', '*', '*', ' ', '#', '*'],
['*', 'o', ' ', ' ', ' ', ' ', ' ', '*'],
['*', ' ', ' ', ' ', ' ', ' ', 'o', '*'],
['*', '*', '*', '*', '*', '*', '*', '*']
]
main(test_board)
如果您不想更改相同的值(一直到 test_board
),则需要(深)复制面板列表列表。
import copy
# ...
def __init__(self, board):
self.__board = copy.deepcopy(board)
self.__original_board = copy.deepcopy(board)
除此之外,也许 .restart()
应该只重置游戏对象的状态,而不是 return 一个新对象?
def __init__(self, board):
self.__original_board = copy.deepcopy(board)
self.restart()
# ...
def restart(self):
self.__steps = 0
self.__last_step = []
self.__position = (0, 0)
self.__board = copy.deepcopy(self.__original_board)
这样,一个简单的 game.restart()
调用就可以了。
我必须在 Python 中创建一个推箱子游戏,并且我在我的推箱子 class 中定义了方法 find_player() 、 complete() 、 get_steps () 和移动() 。我需要创建一个 restart() 方法和一个 undo() 方法,但我不知道该怎么做。我找不到存储原始板或以前的板的方法。我尝试在 __init__
中定义另一个板,但它只是用 self.__board
更新而不是保存初始板。我还尝试在 __init__
中制作一个列表,并尝试将每个移动的“棋盘”附加到列表中,但它会更改列表中的每个棋盘。如果有人可以提供帮助,我附上了我的代码。
class Sokoban:
"""Your code here"""
def __init__(self, board):
self.__board = board
self.__original_board = board
self.__steps = 0
self.__last_step = []
self.__position = (0, 0)
def restart(self):
first_board = self.__original[0]
new_board = []
for i in range(len(first_board)):
new_board.append(first_board[i])
print(self.__original)
return Sokoban(self.__original_board)
def undo(self):
return
def main(board):
game = Sokoban(board)
message = 'Press w/a/s/d to move, r to restart, or u to undo'
print(message)
while not game.complete():
print(game)
move = input('Move: ').lower()
while move not in ('w', 'a', 's', 'd', 'r', 'u'):
print('Invalid move.', message)
move = input('Move: ').lower()
if move == 'r':
game.restart()
elif move == 'u':
game.undo()
else:
game.move(move)
print(game)
print(f'Game won in {game.get_steps()} steps!')
test_board = [
['*', '*', '*', '*', '*', '*', '*', '*'],
['*', ' ', ' ', ' ', ' ', ' ', ' ', '*'],
['*', 'P', ' ', '#', ' ', ' ', ' ', '*'],
['*', '*', '*', '*', '*', ' ', '#', '*'],
['*', 'o', ' ', ' ', ' ', ' ', ' ', '*'],
['*', ' ', ' ', ' ', ' ', ' ', 'o', '*'],
['*', '*', '*', '*', '*', '*', '*', '*']
]
main(test_board)
如果您不想更改相同的值(一直到 test_board
),则需要(深)复制面板列表列表。
import copy
# ...
def __init__(self, board):
self.__board = copy.deepcopy(board)
self.__original_board = copy.deepcopy(board)
除此之外,也许 .restart()
应该只重置游戏对象的状态,而不是 return 一个新对象?
def __init__(self, board):
self.__original_board = copy.deepcopy(board)
self.restart()
# ...
def restart(self):
self.__steps = 0
self.__last_step = []
self.__position = (0, 0)
self.__board = copy.deepcopy(self.__original_board)
这样,一个简单的 game.restart()
调用就可以了。