Advertisement
applescript_superior

Word Search Solver

Sep 4th, 2021
4,417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. set array to {{"C", "A", "X", "L", "A", "Q", "B", "H", "Z", "X"}, {"E", "K", "M", "P", "Z", "D", "W", "B", "V", "B"}, {"O", "L", "P", "A", "N", "A", "N", "A", "B", "I"}, {"L", "L", "O", "K", "K", "Y", "S", "H", "Q", "Y"}, {"E", "K", "E", "R", "L", "W", "T", "Q", "E", "L"}, {"G", "I", "X", "O", "E", "R", "J", "K", "C", "Z"}, {"B", "J", "P", "D", "A", "S", "R", "U", "N", "X"}, {"M", "M", "W", "E", "E", "I", "S", "F", "A", "E"}, {"E", "S", "P", "Q", "U", "F", "P", "A", "D", "R"}, {"Z", "Y", "L", "C", "V", "V", "G", "Y", "C", "X"}}
  2. set word_bank to {"APPLE", "BANANA", "CASSEROLE", "DANCE", "EARTH"}
  3.  
  4. my main(array, word_bank)
  5.  
  6. on main(array, word_bank)
  7.     set x_quantity to (count items of array)
  8.     set y_quantity to (count lists of array)
  9.     set array_quantity to (x_quantity * y_quantity)
  10.    
  11.     set selected_x to 1
  12.     set selected_y to 1
  13.     set selected_word to 1
  14.     set selected_character to 1
  15.    
  16.     repeat array_quantity times
  17.         set selected_item to (item selected_x of (list selected_y of array))
  18.         set valid_positions to (my find_valid_positions(selected_x, selected_y, x_quantity, y_quantity))
  19.        
  20.         set current_word to (item selected_word of word_bank)
  21.        
  22.         repeat with selected_valid_position from 1 to (count valid_positions)
  23.             set current_valid_position to (item (item 1 of (item selected_valid_position of valid_positions)) of (list (item 2 of (item selected_valid_position of valid_positions)) of array))
  24.             if current_valid_position = (character selected_character of current_word) then
  25.                 display dialog current_valid_position & "; true"
  26.             else
  27.                 display dialog current_valid_position & "; false"
  28.             end if
  29.         end repeat
  30.        
  31.         # display dialog selected_item & return & "x: " & selected_x & ", y: " & selected_y
  32.        
  33.         set selected_x to (selected_x + 1)
  34.         if (((selected_x - 1) mod (count items of array)) = 0) then
  35.             set selected_y to (selected_y + 1)
  36.             set selected_x to 1
  37.         end if
  38.     end repeat
  39. end main
  40.  
  41. on find_valid_positions(x, y, x_quantity, y_quantity)
  42.     set t to {x, (y - 1)}
  43.     set tr to {(x + 1), (y - 1)}
  44.     set r to {(x + 1), y}
  45.     set br to {(x + 1), (y + 1)}
  46.     set b to {x, (y + 1)}
  47.     set bl to {(x - 1), (y + 1)}
  48.     set l to {(x - 1), y}
  49.     set tl to {(x - 1), (y - 1)}
  50.    
  51.     set positions to {t, tr, r, br, b, bl, l, tl}
  52.    
  53.     set valid_positions to {}
  54.     repeat with selected_position from 1 to (count positions)
  55.         if (((item 1 of (item selected_position of positions)) > 0) and ((item 1 of (item selected_position of positions)) < (x_quantity + 1)) and ((item 2 of (item selected_position of positions)) > 0) and ((item 2 of (item selected_position of positions)) < (y_quantity + 1))) then
  56.             set valid_positions to (valid_positions & {item selected_position of positions})
  57.         end if
  58.     end repeat
  59.    
  60.     return valid_positions
  61. end find_valid_positions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement