#Issues with refactoring code. Unnecessary list items added to code from unknown source.

1 messages · Page 1 of 1 (latest)

barren prism
#

I'm having trouble getting a function I'm using for refactoring my code to work properly and realized I had this whole other problem with my original_moves variable. It goes up higher than the total number of "original moves" that I have. original_moves is a list of data for a given chess piece move before the function adds more move locations.

There should be 8 regular moves in here, but instead, it extends past that number and goes up continuously once the range for the multiplicity goes past 2. Which is where the move dictionary should exponentially increase in moves.

#

my code:

code:

var original_moves = []
    
    
    for move_name in _discreet_move_dict:
        multiplicity_move_names.append(move_name)
        original_moves.append(_discreet_move_dict[move_name])
        print("original moves at start: %d " % [len(original_moves)])
    
    # level is the current iteration of the multiplicity
    for level in range(piece.multiplicity-1):
        for move_name in _discreet_move_dict:
            move_copy_move_number = 0
            multiplicity_iterations += 1
            
            multiplicity_moves = original_moves
                
            if level > 0:
#                var i = 0
#                for move in multiplicity_move_queue:
#                    if not move in multiplicity_moves:
#                        multiplicity_moves.append(move)
#                        #print(multiplicity_move_names_queue)
#                        multiplicity_move_names.append(multiplicity_move_names_queue[i])
#                    i += 1
                multiplicity_moves = add_non_duplicate_items_to_list(multiplicity_moves, multiplicity_move_queue)
                multiplicity_move_names = add_non_duplicate_items_to_list(multiplicity_move_names, multiplicity_move_names_queue)
#
                multiplicity_move_queue = []
                multiplicity_move_names_queue = []
            
            
            discreet_move_values = _discreet_move_dict[move_name]
            new_discreet_move_dict[move_name] = _discreet_move_dict[move_name]
            
            
            #print(j)
            print("original moves: %d " % [len(original_moves)])
#

data printed out:


original moves at start: 1 
original moves at start: 2 
original moves at start: 3 
original moves at start: 4 
original moves at start: 5 
original moves at start: 6 
original moves at start: 7 
original moves at start: 8 
original moves: 8 
original moves: 8 
original moves: 8 
original moves: 8 
original moves: 8 
original moves: 8 
original moves: 8 
original moves: 8 
original moves: 57
elder dagger
#

I suggest printing out the moves that are added to the array to check whether they're dupes or not

barren prism
#

okay

#

I'll do that

#

this might take a second, because I'm overwhelming the engine with the string size

#

and I can't yield it because other parts of the code need to be run simulataniously to it

#

so I'm going to figure out a solution to make the strings shorter

#

oh I could just do this haha

#

it's... admittedly a bit hard to read, but I'm pretty sure that these are the moves that should exist in the discreet_move_dict_new variable if the multiplicity is 2

#

essentially, the point of the multiplicity function is that it's supposed to allow for doing "double moves"

#

like in checkers

#

except, I want to use code to basically do that with all the moves of a piece that has higher multiplicity values

elder dagger
#

(Gonna be right back!)

barren prism
#

oki

#

I wanna clarify that my chess engine is mainly designed for creating modded chess and checker pieces which is part of why it's intended to have this feature in the first place haha

gloomy sparrow
#

I'd start planting assertions into your code

#

Like, if at some point of execution you have an assumption about what should be true

#

write an assertion for it

#

Find the point of execution where things go from what you expect into what you didn't expect

#

and then step through with the debugger, or have more detailed debug prints

barren prism
#

Thanks! I'll start doing that

#

I think I need to get some rest for now though

#

so I'll probably do it in a couple hours

#

I need to sleep

gloomy sparrow
#

👍

barren prism
#

okay, I'm back

#

I've isolated the problem.

The problem exists regardless of whether I refactor it, so I found it in both versions and it happens right after I append this variable

multiplicity_moves.append(move)
#

in the original, non-refactored code, it looks like this

            if level > 0:
                var i = 0
                for move in multiplicity_move_queue:
                    if not move in multiplicity_moves:
                        print("test 3.5 %s" % [not len(original_moves) > 8])
                        multiplicity_moves.append(move)
                        print("test 3.6 %s" % [not len(original_moves) > 8])
                        #print(multiplicity_move_names_queue)
                        multiplicity_move_names.append(multiplicity_move_names_queue[i])

                    i += 1                i += 1

and at test 3.6, it changes to the different value

#

whereas, in the refactored code, it does the same thing, it's just written a bit differently due to abstraction

#

it's also interesting to note how the code only breaks multiple loops after the function starts printing false

#

but anyway, I'm really not sure where to go from here

#

because I seemed to have isolated the line, but I have no idea why this line in particular changes the original_moves variable to be a faulty value

rich rapids
#

Just from looking at those last 2 screenshots, my first guess is that something is appending an element to the original_moves array. I don't otherwise see how you could go from Test 5 True to Test 6 False if the original_moves array has not been altered

barren prism
#

it's just really confusing

#

because I only have 1 append for the original_moves variable

#

and it stops being appended from there at the beginning of the function TwT

gloomy sparrow
#

ohh wait hold on

#

I see it now

#

You're doing this: multiplicity_moves = original_moves

#

multiplicity_moves and original_moves are the same array.

#

If you want multiplicity_moves to have the same elements, but not be the same array, you have to write multiplicity_moves = original_moves.duplicate() instead!

barren prism
#

like it's holding faulty values

#

so the original_moves variable, is supposed to hold 8 items

gloomy sparrow
#

...but it's holding faulty values after you're appending something to another array, right?

barren prism
#

yeah

gloomy sparrow
#

You're secretly appending to the original_moves array.

gloomy sparrow
#

Arrays are passed by reference, not value.

var a = 1
var b = a
b += 1
print(a) // 1
print(b) // 2
var a = ["bird"]
var b = a
b.append("dog")
print(a) // ["bird", "dog"]
print(b) // ["bird", "dog"]
barren prism
#

whaaaa

#

wait that's bizarre

gloomy sparrow
#

I remember discovering this fact for myself when debugging

#

it's... not intuitive

barren prism
#

waitwaitwaitwaitwaitwait

#

I gotta test this haha

#

but not rn, I got stuff to do

#

but I'll be back

#

and then I'm testing this

#

cuz that's wild

stray obsidian
#

You usually need a clone/copy if you want to make them separate (or you can map values across, filter, etc)

barren prism
#

This is highkey blowing my mind hahaha

gloomy sparrow
stray obsidian
#

I sure did! Fixed it

#

Sadly rocket no go

barren prism
#

And it works!

#

Wow haha

#

Thanks y’all 😊

gloomy sparrow
#

👍

#

remember to put the "solved" tag on the post ^^

barren prism
#

added it 👍

gloomy sparrow
#

yeee