globals [ destination_x ;; used to compute x location of current agent by go method destination_y ;; used to compute y location of current agent by go method ;; class_width ;; from interface ;; class_height ;; from interface ;; class_size ;; from interface class_width_offset class_height_offset done_so_far ] to setup clear-all set-default-shape turtles "person" crt 1 [ set color green setxy 0 -15 ] set class_width_offset -1 * ( class_width / 2 ) + 1 set class_height_offset -1 * ( class_height / 2 ) + 1 set done_so_far 0 end to go if done_so_far >= class_size [ stop ] find_seat set done_so_far done_so_far + 1 crt 1 [ set color white setxy destination_x destination_y ] tick end to find_seat_1 set destination_x ( class_width_offset + random class_width ) set destination_y ( class_height_offset + random class_height ) end to find_seat_2 let x_index 0 let y_index 0 let patch_empty 1 loop [ set destination_x class_width_offset + x_index set destination_y class_height_offset + y_index set patch_empty 1 ask patch destination_x destination_y [ if any? turtles-here [ set patch_empty 0 ] ] if patch_empty = 1 [ stop ] set x_index x_index + 1 if x_index >= class_width [ set x_index 0 set y_index y_index + 1 if y_index >= class_height [ ask patch 10 -15 [ set plabel-color yellow set plabel "CLASS TOO BIG" ] stop ] ] ] end to find_seat let x_index 0 let y_index 0 let best_x 0 let best_y 0 let best_score -1000 let curr_x 0 let curr_y 0 let curr_score -1000 loop [ set curr_x class_width_offset + x_index set curr_y class_height_offset + y_index set curr_score compute_score curr_x curr_y if ( curr_score > best_score ) [ set best_score curr_score set best_x curr_x set best_y curr_y ] set x_index x_index + 1 if x_index >= class_width [ set x_index 0 set y_index y_index + 1 if y_index >= class_height [ ifelse best_score = -1000 [ ask patch 10 -15 [ set plabel-color yellow set plabel "CLASS TOO BIG" ] stop ] [ set destination_x best_x set destination_y best_y stop ] ] ] ] end to-report compute_score_1 [ x_value y_value ] ;; same result as find_seat_2 let patch_empty 1 ask patch x_value y_value [ if any? turtles-here [ set patch_empty 0 ] ] if patch_empty = 0 [ report -1000 ] report 0 end to-report compute_score [ x_value y_value ] let patch_empty 1 ask patch x_value y_value [ if any? turtles-here [ set patch_empty 0 ] ] if patch_empty = 0 [ report -1000 ] ask patch x_value ( y_value - 1 ) [ if any? turtles-here [ set patch_empty 0 ] ] if patch_empty = 0 [ report -500 ] report 0 end