def validate_location(index, row, col, dir): # Converting row, and column into values that are workable using numbers. col = ord(col.upper()) - ord("A") row = int(row) - 1 # Checks if horizontal and if the column and row don't go over if (dir == "h") and (-1 < col < (grid.GRID_WIDTH - grid.VESSEL_SIZE[index])) and (-1 < row < grid.GRID_HEIGHT): # Checks if it is overlapping with another vessel, if so, it gets input, otherwise it sends to grid module. if not grid.has_overlap(grid_defend, index, row, col, dir): grid.add_vessel(grid_defend, index, row, col, dir) else: print("This ship overlaps with another!") get_location(index) # Similar to above but, vertical. elif dir == "v" and (-1 < row < (grid.GRID_HEIGHT - grid.VESSEL_SIZE[index])) and (-1 < col < grid.GRID_WIDTH): if not grid.has_overlap(grid_defend, index, row, col, dir): grid.add_vessel(grid_defend, index, row, col, dir) else: print("This ship overlaps with another!") get_location(index) # If neither are true, theres more then one mistake with the location, asks user to try again. else: print("Not a valid location!") # Recurse get_location(index) print()
def get_location(index): global defend_grid dir = random.randint(0, 1) # if horizontal if dir == 0: # place on a valid spot (taking ship size into account) dir = 'h' col = random.randint(1, grid.GRID_WIDTH - 2 - grid.VESSEL_SIZES[index]) row = random.randint(1, grid.GRID_HEIGHT - 2) else: dir = 'v' col = random.randint(1, grid.GRID_WIDTH - 2) row = random.randint(1, grid.GRID_HEIGHT - 2 - grid.VESSEL_SIZES[index]) # dont let any ships touch if grid.has_overlap(defend_grid, index, row, col, dir) or grid.has_overlap( defend_grid, index, row, col - 1, dir) or grid.has_overlap( defend_grid, index, row, col + 1, dir) or grid.has_overlap( defend_grid, index, row - 1, col, dir) or grid.has_overlap( defend_grid, index, row + 1, col, dir): get_location(index) else: defend_grid = grid.add_vessel(defend_grid, index, row, col, dir)
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
def get_location(index): global defend_grid dir = random.randint(0,1) # if horizontal if dir == 0: # place on a valid spot (taking ship size into account) dir = 'h' col = random.randint(1,grid.GRID_WIDTH - 2 - grid.VESSEL_SIZES[index]) row = random.randint(1,grid.GRID_HEIGHT - 2) else: dir = 'v' col = random.randint(1,grid.GRID_WIDTH - 2) row = random.randint(1,grid.GRID_HEIGHT - 2 - grid.VESSEL_SIZES[index]) # dont let any ships touch if grid.has_overlap(defend_grid, index, row, col, dir) or grid.has_overlap(defend_grid, index, row, col - 1, dir) or grid.has_overlap(defend_grid, index, row, col + 1, dir) or grid.has_overlap(defend_grid, index, row - 1, col, dir) or grid.has_overlap(defend_grid, index, row + 1, col, dir): get_location(index) else: defend_grid = grid.add_vessel(defend_grid, index, row, col, dir)
def validate_location(index, row, col, dir): #Converting row, and column into values that are workable using numbers. col = (ord(col.upper()) - ord('A')) row = (int(row) - 1) #Checks if horizontal and if the column and row don't go over if (dir == 'h') and (-1 < col < (grid.GRID_WIDTH - grid.VESSEL_SIZE[index])) and (-1 < row < grid.GRID_HEIGHT): #Checks if it is overlapping with another vessel, if so, it gets input, otherwise it sends to grid module. if not grid.has_overlap(grid_defend, index, row, col, dir): grid.add_vessel(grid_defend, index, row, col, dir) else: print('This ship overlaps with another!') get_location(index); #Similar to above but, vertical. elif dir == 'v' and (-1 < row < (grid.GRID_HEIGHT - grid.VESSEL_SIZE[index])) and (-1 < col < grid.GRID_WIDTH): if not grid.has_overlap(grid_defend, index, row, col, dir): grid.add_vessel(grid_defend, index, row, col, dir) else: print('This ship overlaps with another!') get_location(index); #If neither are true, theres more then one mistake with the location, asks user to try again. else: print('Not a valid location!') #Recurse get_location(index); print()
def get_location(vessel_index) : global column global row global direction name = grid.VESSEL_NAMES[vessel_index] size = grid.VESSEL_SIZES[vessel_index] print('Enter placement of your', name, '(', size, 'spaces)') column = input('\tLeft Column (A-J): ') row = int(input('\tTop Row (1-10): ')) direction = input('\tDirection (h)orizontal or (v)ertical: ') column = ord(column) - ord('A') row = row - 1 if(column >= grid.GRID_WIDTH or column < 0) : print('The column entered is invalid, it must be a value between A and J.') get_location(vessel_index) elif(row >= grid.GRID_HEIGHT or row < 0) : print('The row entered is invalid, it must be between 1 and 10.') get_location(vessel_index) else : if (direction == 'h' and column + size > grid.GRID_WIDTH) : print('Invalid column. The vessel is directed horizontally to the right') print('and does not fit on the grid with this starting column.') get_location(vessel_index) elif (direction == 'v' and row + size > grid.GRID_HEIGHT) : print('Invalid row. The vessel is directed vertically downwards') print('and does not fit on the grid with this starting now.') get_location(vessel_index) else : if (direction != 'h' and direction != 'v') : print('The direction entered is invalid, it must be either (h)orizontal or (v)ertical.') get_location(vessel_index) else : overlap = grid.has_overlap(vessel_index, row, column, direction, defend_grid) if(overlap == True) : print('The vessel entered overlaps another one! Please place your new vessel elsewhere.') get_location(vessel_index) else : grid.add_vessel(vessel_index, row, column, direction, defend_grid) grid.print_grid(defend_grid)
def get_location(vessel_index) : global column global row global direction import random name = grid.VESSEL_NAMES[vessel_index] size = grid.VESSEL_SIZES[vessel_index] column = random.choice('ABCDEFGHIJ') row = random.randint(1,10) direction = random.choice('hv') column = ord(column) - ord('A') row = row - 1 if(column >= grid.GRID_WIDTH or column < 0) : get_location(vessel_index) elif(row >= grid.GRID_HEIGHT or row < 0) : get_location(vessel_index) else : if (direction == 'h' and column + size > grid.GRID_WIDTH) : get_location(vessel_index) elif (direction == 'v' and row + size > grid.GRID_HEIGHT) : get_location(vessel_index) else : if (direction != 'h' and direction != 'v') : get_location(vessel_index) else : overlap = grid.has_overlap(vessel_index, row, column, direction, defend_grid) if(overlap == True) : get_location(vessel_index) else : grid.add_vessel(vessel_index, row, column, direction, defend_grid) grid.print_grid(defend_grid)