def get_location(index): #Setting globals for editing global x global y global direction #Selecting Random Values for row, column and direction x = random.randint(1, 10) y = random.randint(1, 10) direction = random.randint(1, 2) #Loop sets a number to a character for place vessel to 'decode' the direction if direction == 1: direction = 'v' else: direction = 'h' #This loop makes sure the end of the vessel does not go over the edge of the board. if (direction == 'h') and (-1 < x < (grid.GRID_WIDTH - grid.VESSEL_SIZE[index]) ) and (-1 < y < grid.GRID_HEIGHT): #If has_overlap returns True then get location is called. if grid.has_overlap(index, x, y, direction): get_location(index) #If it returns not true(false), then we place the vessel. if not grid.has_overlap(index, x, y, direction): grid.place_vessel(index, direction, x, y) #Same as previous function, except with vertical/y direction. elif direction == 'v' and (-1 < y < (grid.GRID_HEIGHT - grid.VESSEL_SIZE[index]) ) and (-1 < x < grid.GRID_WIDTH): if grid.has_overlap(index, x, y, direction): get_location(index) if not grid.has_overlap(index, x, y, direction): grid.place_vessel(index, direction, x, y) #Recursive statement, to retry if fails. else: get_location(index)
def validate_location(index): global x # this function can edit x global y # this function can edit y # check if string is a single letter or smaller than a three digit number, if not, asks for input again, then checks #check if strings are appropriate length if (len(x) == 1) and (len(y) < 3): x = (ord(x) - ord('A')) y = (int(y) - 1) # checks if location is on the board and if the ship will fit, if not asks for input again and checks again if (direction == 'h') and (-1 < x < (grid.GRID_WIDTH - grid.VESSEL_SIZE[index]) ) and (-1 < y < grid.GRID_HEIGHT): if grid.has_overlap(index, x, y, direction): print("Location Invalid") get_location(index) if not grid.has_overlap(index, x, y, direction): grid.place_vessel(index, direction, x, y) elif direction == 'v' and (-1 < y < (grid.GRID_HEIGHT - grid.VESSEL_SIZE[index]) ) and (-1 < x < grid.GRID_WIDTH): if grid.has_overlap(index, x, y, direction): print("Location Invalid") get_location(index) if not grid.has_overlap(index, x, y, direction): grid.place_vessel(index, direction, x, y) else: print('The location you entered is not on the board!') get_location(index) validate_location(index) # recursive else: print( 'Please enter a LETTER (between A-J) and a NUMBER (between 1-10!)') get_location(index) validate_location(index) # recursive return
def get_location(index): #Setting globals for editing global x global y global direction #Selecting Random Values for row, column and direction x = random.randint(1, 10) y = random.randint(1, 10) direction = random.randint(1,2) #Loop sets a number to a character for place vessel to 'decode' the direction if direction == 1: direction = 'v' else: direction = 'h' #This loop makes sure the end of the vessel does not go over the edge of the board. if (direction == 'h') and (-1 < x < (grid.GRID_WIDTH - grid.VESSEL_SIZE[index])) and (-1 < y < grid.GRID_HEIGHT): #If has_overlap returns True then get location is called. if grid.has_overlap(index, x, y, direction): get_location(index) #If it returns not true(false), then we place the vessel. if not grid.has_overlap(index, x, y, direction): grid.place_vessel(index, direction, x, y) #Same as previous function, except with vertical/y direction. elif direction == 'v' and (-1 < y < (grid.GRID_HEIGHT - grid.VESSEL_SIZE[index])) and (-1 < x < grid.GRID_WIDTH): if grid.has_overlap(index, x, y, direction): get_location(index) if not grid.has_overlap(index, x, y, direction): grid.place_vessel(index, direction, x, y) #Recursive statement, to retry if fails. else: get_location(index)
def validate_location(index): global x # this function can edit x global y # this function can edit y # check if string is a single letter or smaller than a three digit number, if not, asks for input again, then checks #check if strings are appropriate length if (len(x) == 1) and (len(y) < 3): x = (ord(x) - ord('A')) y = (int(y) - 1) # checks if location is on the board and if the ship will fit, if not asks for input again and checks again if (direction == 'h') and (-1 < x < (grid.GRID_WIDTH - grid.VESSEL_SIZE[index])) and (-1 < y < grid.GRID_HEIGHT): if grid.has_overlap(index, x, y, direction): print("Location Invalid") get_location(index) if not grid.has_overlap(index, x, y, direction): grid.place_vessel(index, direction, x, y) elif direction == 'v' and (-1 < y < (grid.GRID_HEIGHT - grid.VESSEL_SIZE[index])) and (-1 < x < grid.GRID_WIDTH): if grid.has_overlap(index, x, y, direction): print("Location Invalid") get_location(index) if not grid.has_overlap(index, x, y, direction): grid.place_vessel(index, direction, x, y) else: print('The location you entered is not on the board!') get_location(index) validate_location(index) # recursive else: print('Please enter a LETTER (between A-J) and a NUMBER (between 1-10!)') get_location(index) validate_location(index) # recursive return