def main(): for i in range(grid.NUM_OF_VESSELS): #human.get_location(i) grid.add_vessel(human.grid_defend, i, i, 0, 'h') ai.get_location(i) print("AI Grid:") grid.print_grid(ai.grid_defend) print("Human Grid:") grid.print_grid(human.grid_defend) while not (grid.all_vessels_sunk(ai.grid_defend) and grid.all_vessels_sunk(human.grid_defend)): human.enter_choice() row, col = human.get_choice() grid.drop_bomb(ai.grid_defend, human.grid_attack, row, col) row, col = ai.get_choice() grid.drop_bomb(human.grid_defend, ai.grid_attack, row, col) print("AI Grid:") grid.print_grid(ai.grid_defend) print("Human Grid:") grid.print_grid(human.grid_defend) print("AI Grid A:") grid.print_grid(ai.grid_attack) print("Human Grid A:") grid.print_grid(human.grid_attack)
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(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 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 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)
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(0,grid.GRID_WIDTH - 1 - grid.VESSEL_SIZES[index]) row = random.randint(0,grid.GRID_HEIGHT - 1) else: dir = 'v' col = random.randint(0,grid.GRID_WIDTH - 1) row = random.randint(0,grid.GRID_HEIGHT - 1 - grid.VESSEL_SIZES[index]) if grid.has_overlap(defend_grid, index, row, col, dir): get_location(index) else: defend_grid = grid.add_vessel(defend_grid, index, row, col, dir)