在创建一个战舰游戏时,需要定义一个恒量变量 SEA 来保存空白棋盘。但是,发现这个变量被修改了,而代码并没有对它进行修改。怀疑这是因为在将 SEA 传递给其他变量时,它作为引用被传递,导致对其他变量的修改也影响到了 SEA。
解决方法:
- 使用浅拷贝:
player_radar = SEA[:]
这种方法创建了一个新的列表 player_radar,它包含 SEA 中元素的副本。这样,对 player_radar 的修改不会影响 SEA。
- 使用深度拷贝:
import copy
player_radar = copy.deepcopy(SEA)
这种方法创建了一个新的列表 player_radar,它包含 SEA 中所有元素的副本,包括嵌套列表。这样,对 player_radar 的修改不会影响 SEA。
- 使用不可变类型:
SEA = tuple([OCEAN] * SIZE)
将 SEA 定义为一个元组而不是列表。元组是不可变类型,因此无法修改。
代码示例:
from random import randint
import copy
#Constants and globals
OCEAN = "O"
FIRE = "X"
HIT = "*"
SIZE = 10
SHIPS = [5, 4, 3, 3, 2]
player_radar = []
player_board = []
player_ships = []
ai_radar = []
ai_board = []
ai_ships = []
#Classes
class Ship(object):
def set_board(self, b):
self.ship_board = b
def edit(self, row, col, x):
self.ship_board[row][col] = x
def __repre__(self):
return self.ship_board
#Set up variables
last_ship = Ship() #Holds the last ship made in make_ship()
SEA = [] # Blank Board
for x in range(SIZE):
SEA.append([OCEAN] * SIZE)
#Functions
def print_board():
for row in range(SIZE):
print " ".join(player_radar[row]), "||" , " ".join(player_board[row])
def random_row(is_vertical, size):
if is_vertical:
return randint(0, SIZE - size)
else:
return randint(0, SIZE -1)
def random_col(is_vertical, size):
if is_vertical:
return randint(0, SIZE - 1)
else:
return randint(size-1, SIZE -1)
def exists(row, col, b): # true if ocean
if row < 0 or row >= SIZE:
return 0
elif col < 0 or col >= SIZE:
return 0
if b[row][col] == OCEAN:
return 1
else:
return 0
def make_ship(size, board):
#Find an unoccupied spot, then place ship on board
#Also put ship in last_ship
temp = []
temp = board
is_vertical = randint(0, 1) # vertical ship if true
occupied = True
while(occupied):
occupied = False
ship_row = random_row(is_vertical, size)
ship_col = random_col(is_vertical, size)
if is_vertical:
for p in range(size):
if not exists(ship_row+p, ship_col, temp):
occupied = True
else:
for p in range(size):
if not exists(ship_row, ship_col-p, temp):
occupied = True
#Place ship on boards
last_ship.set_board(SEA)
if is_vertical:
last_ship.edit(ship_row, ship_col, "^")
last_ship.edit(ship_row+size-1, ship_col, "v")
temp[ship_row][ship_col] = "^"
temp[ship_row+size-1][ship_col] = "v"
for p in range(size -2):
last_ship.edit(ship_row+p+1, ship_col, "+")
temp[ship_row+p+1][ship_col] = "+"
else:
last_ship.edit(ship_row, ship_col, ">")
last_ship.edit(ship_row, ship_col-size+1, "<")
temp[ship_row][ship_col] = ">"
temp[ship_row][ship_col-size+1] = "<"
for p in range(size -2):
last_ship.edit(ship_row, ship_col-p-1, "+")
temp[ship_row][ship_col-p-1] = "+"
return temp
# Make the boards
player_radar = SEA[:]
player_board = SEA[:]
ai_radar = SEA[:]
ai_board = SEA[:]
print_board()
for x in SHIPS:
player_board = make_ship(x, player_board)
#player_ships.append(last_ship)
#ai_board = make_ship(x, ai_board)
#ai_ships.append(last_ship)
print "Let's play Battleship!"
for row in range(SIZE):
print " ".join(SEA[row])