常量变量正在改变

时间:2013-07-23 19:45:58

标签: python

我正在重建我的战舰游戏,我有一个名为SEA的恒定变量,它拥有一块空板。但是,变量正在被修改,我不知道为什么(或在哪里)。我怀疑它是通过引用player_board传递的,当player_board被修改时,SEA也是如此。我如何阻止这种情况发生?这是我的代码。你会在底部看到我打印出SEA,它已被修改。

from random import randint
#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])

3 个答案:

答案 0 :(得分:4)

SEA及其成员是列表,Python中的列表是可变的。当您说player_radar = SEA等时,您没有复制SEA;你正在对它进行新的引用。您对player_radar所做的任何更改都将反映在SEA

copy.deepcopy通常用于递归复制嵌套的可变数据结构。然而,就个人而言,我更愿意只复制我知道我需要的层数。例如,要制作列表及其所有成员的副本,您可以执行以下操作:

player_radar = [sublist[:] for sublist in SEA]

这是list comprehension。每个子列表都使用[:]复制,每个子列表都是一个浅层副本。

答案 1 :(得分:1)

SEA是一个列表,因此请复制它:

player_radar = SEA[:]
player_board = SEA[:]
ai_radar = SEA[:]
ai_board = SEA[:]

或更深层次的副本,如果您需要。

编辑:通过“更深层次的副本”,我的意思是,如果您的列表包含,例如,其他列表,那么只需创建一个顶级副本将创建一个新列表,但其成员将引用与您相同的成员原始列表有,所以要创建一个深层副本,你还需要复制这些成员。

举例说明:

>>> list1 = [[1,2,3]]
>>> list2 = list1[:]    # Make a shallow copy
>>> print(list1)
[[1,2,3]]
>>> print(list2)
[[1,2,3]]
>>> list2[0][0] = 4     # Also changing list1's first member, here
>>> print(list2)
[[4,2,3]]
>>> print(list1)
[[4,2,3]]               # So list1 has also changed.

答案 2 :(得分:0)

Python变量是事物的名称,而不是放置东西的地方。

player_radar = SEA

不会使player_radar成为SEA的副本,就像在C ++中一样。相反,player_radarSEA都是同一列表对象的名称。如果您更改player_radar,您会在SEA以及引用同一对象的所有其他变量上看到更改。