python - Constant Variable is being changed -
i'm remaking battleship game, , have constant variable called sea holds empty board. however, variable being modified, , don't know why (or where). suspect it's being passed reference player_board , when player_board modified, sea. how stop happening? here code. you'll see on bottom print out sea, , it's been modified.
from random import randint #constants , 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 variables last_ship = ship() #holds last ship made in make_ship() sea = [] # blank board x in range(size): sea.append([ocean] * size) #functions def print_board(): 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 unoccupied spot, 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: p in range(size): if not exists(ship_row+p, ship_col, temp): occupied = true else: 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" 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] = "<" p in range(size -2): last_ship.edit(ship_row, ship_col-p-1, "+") temp[ship_row][ship_col-p-1] = "+" return temp # make boards player_radar = sea player_board = sea ai_radar = sea ai_board = sea print_board() 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!" row in range(size): print " ".join(sea[row])
sea
, members lists, , lists in python mutable. when player_radar = sea
, etc., you're not making copy of sea
; you're making new reference it. changes make player_radar
reflected in sea
.
copy.deepcopy
used recursively copy nested mutable data structures. personally, however, prefer copy number of layers know i'll need. instance, make copy of list of lists , members, can this:
player_radar = [sublist[:] sublist in sea]
this list comprehension. each sublist copied using [:]
, makes shallow copy of each one.
Comments
Post a Comment