0

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]
SiHa
  • 7,830
  • 13
  • 34
  • 43
  • 1
    Because the `Matrix` outside your function is not the same as the `Matrix` inside your function. The latter is a local variable, the former is a global variable. They just happen to share the same name. – juanpa.arrivillaga Jun 29 '17 at 07:03
  • Also, as an aside, you have a Python *list*, not an array. – juanpa.arrivillaga Jun 29 '17 at 07:04
  • So, the fundamental issue here is that you have to understand *scope*. It is critical to grok it before trying to continue on your journey. The answers [here](https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules) veer off into advanced territory, but you can hopefully give it a read, and if you don't get it at first, keep coming back to it. – juanpa.arrivillaga Jun 29 '17 at 07:06
  • Thanks, I got it to work, I think I kinda understand the rules about scoping. – pythonstranglesme Jun 29 '17 at 09:38

0 Answers0