0 votes

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

Godot version 3.2.2.stable.official
in Engine by (154 points)

1 Answer

+1 vote
Best answer

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.

by (1,370 points)
selected by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.