def defend_button_clicked(self, row, col): messagebox.showinfo('Defend Grid', 'You clicked on: \nRow:' + str(row + 1) + '\nColumn:' + str(col + 1), icon='info') #Error checking, makes sure vessel is placed correctly. if col <= (grid.GRID_WIDTH - grid.VESSEL_SIZES[self.current_vessel]): for i in range(grid.VESSEL_SIZES[self.current_vessel]): self.defend_grid.grid[row][col + i] = grid.VESSEL_SIZES[ self.current_vessel] #Updates grid, and tells ai to make move. self.update_GUI(self.defend_grid.grid, self.defend_grid_buttons) t00_0_ai.get_location(self.current_vessel) self.current_vessel += 1 #If all the vessels are placed disable the defend grid, and enable the attack grid. if self.current_vessel == grid.NUM_OF_VESSELS: self.set_grid('normal', self.attack_grid_buttons) self.set_grid('disabled', self.defend_grid_buttons)
def defend_button_clicked(self,row,column) : direction = 'h' ## for now, default to horizontal size = grid.VESSEL_SIZES[self.next_vessel] # Check to make sure the vessel fits where the user has clicked. if (direction == 'h' and column + size > GRID_WIDTH or \ direction == 'v' and row + size > GRID_HEIGHT or \ self.defend_grid.has_overlap(self.next_vessel,row,column, direction)) : # For now, will do nothing. There really should be some sort of # error message. return # Add vessel both as indicated by user and let AI also insert. t00_0_ai.get_location(self.next_vessel) self.defend_grid.add_vessel(self.next_vessel, \ row, \ column, \ direction) # Update GUI to reflect data in defend grid for row_index in range(0,GRID_HEIGHT) : for column_index in range(0,GRID_WIDTH) : if (not self.defend_grid.is_empty(row_index, column_index)) : button = self.defend_buttons[row_index][column_index] button['text'] = self.defend_grid.grid[row_index][column_index] # Setup for next vessel to add to grid. self.next_vessel = self.next_vessel - 1 # All vessels are in the grid. Disable the defense grid buttons. if (self.next_vessel < 0) : for row_index in range(0,GRID_HEIGHT) : for column_index in range(0,GRID_WIDTH) : button = self.defend_buttons[row_index][column_index] button['state'] = 'disabled'
def main() : for index in range(0, grid.NUM_OF_VESSELS) : human.get_location(index) t00_0_ai.get_location(index) # t00_0_ai.attack_grid.drop_test_bombs() # human.attack_grid.drop_test_bombs() while (not has_winner()) : # user's turn row, column = human.get_choice() if (t00_0_ai.defend_grid.is_hit(row, column)) : print("HIT!!") else : print("You missed.") t00_0_ai.defend_grid.drop_bomb(human.attack_grid, row, column) print() #ai's turn row, column = t00_0_ai.get_choice() if (human.defend_grid.is_hit(row, column)) : print("You've been hit.") else : print("I missed.") human.defend_grid.drop_bomb(t00_0_ai.attack_grid, row, column) human.defend_grid.print_grid() announce_winner()
def defend_button_clicked(self, row, col): messagebox.showinfo('Defend Grid','You clicked on: \nRow:'+ str(row + 1) + '\nColumn:' + str(col + 1), icon = 'info') #Error checking, makes sure vessel is placed correctly. if col <= (grid.GRID_WIDTH - grid.VESSEL_SIZES[self.current_vessel]): for i in range(grid.VESSEL_SIZES[self.current_vessel]): self.defend_grid.grid[row][col + i] = grid.VESSEL_SIZES[self.current_vessel] #Updates grid, and tells ai to make move. self.update_GUI(self.defend_grid.grid, self.defend_grid_buttons) t00_0_ai.get_location(self.current_vessel) self.current_vessel += 1 #If all the vessels are placed disable the defend grid, and enable the attack grid. if self.current_vessel == grid.NUM_OF_VESSELS: self.set_grid('normal', self.attack_grid_buttons) self.set_grid('disabled', self.defend_grid_buttons)
def defend_button_clicked(self, row, column): direction = 'h' ## for now, default to horizontal size = grid.VESSEL_SIZES[self.next_vessel] # Check to make sure the vessel fits where the user has clicked. if (direction == 'h' and column + size > GRID_WIDTH or \ direction == 'v' and row + size > GRID_HEIGHT or \ self.defend_grid.has_overlap(self.next_vessel,row,column, direction)) : # For now, will do nothing. There really should be some sort of # error message. return # Add vessel both as indicated by user and let AI also insert. t00_0_ai.get_location(self.next_vessel) self.defend_grid.add_vessel(self.next_vessel, \ row, \ column, \ direction) # Update GUI to reflect data in defend grid for row_index in range(0, GRID_HEIGHT): for column_index in range(0, GRID_WIDTH): if (not self.defend_grid.is_empty(row_index, column_index)): button = self.defend_buttons[row_index][column_index] button['text'] = self.defend_grid.grid[row_index][ column_index] # Setup for next vessel to add to grid. self.next_vessel = self.next_vessel - 1 # All vessels are in the grid. Disable the defense grid buttons. if (self.next_vessel < 0): for row_index in range(0, GRID_HEIGHT): for column_index in range(0, GRID_WIDTH): button = self.defend_buttons[row_index][column_index] button['state'] = 'disabled'