random arrangement of words on the field

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Timofey

need to make the arrangement of words on the field 10x10, so that if the words intersect, then only with the same letters and so that a copy of the letter is not created at the intersection.

extends Spatial

var words
var way = ['leftToRight', 'rightToLeft', 'upToDown', 'downToUp']

func chooseWords(amount, theme):
    var words = []

    var file = File.new()
    file.open(variables.dictionariesTres.get(theme), File.READ)
    while !file.eof_reached():
        words.append(file.get_line())
    file.close()

    var selected = []
    for number in range(amount):
	    while true:
		    var select = words[randi()%len(words)]
		    if not(select in selected):
			    selected.append(select)
			    break
    return selected

func chooseTheme():
    return variables.dictionaries[randi()%len(variables.dictionaries)]

func setWordsOnField(words):
    var way = 'leftToRight'
    var wordsPositions = []

for word in words:
	var position = [randi()%10, randi()%10]
	
	while position in wordsPositions or position[0] + len(word) > 9:
		position = [randi()%10, randi()%10]
	if way == 'leftToRight':
		wordsPositions.append(position)
	
	for index in range(len(word)):
		var letter = load(variables.alphabetObj[word[index].to_upper()]).instance()
		letter.translate(Vector3((position[0] + index) * 3, 0, position[1] * 3))
		add_child(letter)
	
func _ready():
    randomize()

    var theme = chooseTheme()
    $theme.text = theme

    words = chooseWords(3, theme)

    setWordsOnField(words)

the problem is that words sometimes intersect to form a mixture of several letters.
I will be glad if, in addition to the direction from left to right, you do others, I also thought about the same directions only on the diagonal.

link to the project if needed: https://drive.google.com/file/d/1AH9ojG7YO9AWBuzBFCmbxX6mtIyYmysB/view?usp=sharing

:bust_in_silhouette: Reply From: AlexTheRegent

You can merge selected words in one array (i.e. “home”, “chair”, “box” will become “homechairbox”) and shuffle this array using shuffle method of Array. Then you can place letters on rectangular or square grid, for example. Or you can use more complex algorithms to place letters without overlapping in random form.

although it did not help diagonally and with the intersection of words, thanks for this too

Timofey | 2021-01-22 16:00