Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | nationality |
Probably a stupid question. I’m trying to write a program which will solve a puzzle in the fewest number of steps. But it’s hard for me to make an array that it won’t overwrite! Code in question:
var puzzle = [2,1,3,4,5,6,7,8,9]
var solveseed = [1,2,3,4,5,6,7,8,9]
var workingpuzzle = []
var ntries = 3
func _ready():
puzzle.shuffle()
for i in ntries:
regenerate_solveseed()
sort_array(puzzle)
func sort_array(oldpuzzle: Array):
var steps = 0
workingpuzzle = oldpuzzle
#ax = newpuzzle.size()
if workingpuzzle.size() > 9:
print_to_console("ERROR: Too many terms in array.")
elif workingpuzzle.size() < 9:
print_to_console("ERROR: Not enough terms in array.")
print_to_console(String(workingpuzzle))
for i in solveseed:
var x = workingpuzzle.find(i)
var y = find_correct_place(i)
if x != -1 && x != y:
steps += 1
var swappedterm = workingpuzzle[y]
print_to_console(String(steps) + ": " + String(i) + " swapped with " + String(swappedterm))
swap_terms(workingpuzzle,i,swappedterm)
print_to_console(String(workingpuzzle))
I tried to make a decoy array with “working puzzle” but “puzzle” just gets solved, which is not what I want.