I'm trying to build a system to randomly generate ship positions for a basic battleship game. To represent the grid I'm using nested list. However once the function has finished the matrix appears to revert back to what it was before I ran the function.
As you can see by running this code, the whole matrix works as intended when printed within the function but afterwards not. It's my first program I've tried building on my own and I've never gotten stuck for this long before. Thanks
import random
row, column = 10, 10;
Matrix = [[0 for x in range(row)] for y in range(column)]
ships = {'battleship':5, 'cruiser':4,'destroyer':3}
any_left = 1
def position_ship(type):
row, column = 10, 10;
Matrix = [[0 for x in range(row)] for y in range(column)]
ship_size_to_assign = type
start_pointV = 5 ### random.randint(0,10)
start_pointH = 5 ### random.randint(0,10)
start_point_direction = 1 ###random.randint(0,4)
print start_pointV
print start_pointH
print start_point_direction
start_point_direction = 1
if start_point_direction == 1:
n = 0
if (start_pointV + type) <= 10:
while ship_size_to_assign != 0:
Matrix[(start_pointH-1)][(start_pointV+n-1)] = 1
print "----------"
print Matrix[(start_pointH-1)][(start_pointV+n-1)]
print "----"
n = n + 1
ship_size_to_assign = ship_size_to_assign - 1
if start_point_direction == 2:
print "/////"
n = 0
if (start_pointH + type) <= 10:
while ship_size_to_assign != 0:
Matrix[start_pointH+n-1][start_pointV-1] = 1
n = n + 1
ship_size_to_assign = ship_size_to_assign - 1
if start_point_direction == 3:
print "/////"
n = 0
if (start_pointV - type) > 0:
while ship_size_to_assign != 0:
Matrix[start_pointH-1][start_pointV-n-1] = 1
n = n + 1
ship_size_to_assign = ship_size_to_assign - 1
if start_point_direction == 4:
print "/////"
n = 0
if (start_pointH - type) > 0:
while ship_size_to_assign != 0:
Matrix[start_pointH-1][start_pointV+n-1] = 1
n = n + 1
ship_size_to_assign = ship_size_to_assign - 1
print "####"
print Matrix[0]
print Matrix[1]
print Matrix[2]
print Matrix[3]
print Matrix[4]
print Matrix[5]
print Matrix[6]
print Matrix[7]
print Matrix[8]
print Matrix[9]
position_ship(5)
print "/////"
print Matrix[0]
print Matrix[1]
print Matrix[2]
print Matrix[3]
print Matrix[4]
print Matrix[5]
print Matrix[6]
print Matrix[7]
print Matrix[8]
print Matrix[9]