For my end of year project I need to develop an image processing and artificial intelligence application.
My image processing is already done, so I’m moving to the AI. However I have a problem here.
I will try to describe the situation here.
For a correct image processing I need an AI that can validate the board through the rules of checkers. So I need to check the current state of the play field with the last known state.
So for this I created an object with the currentBoard 2 dimensional array, and a method that validates the raw input of the board with the currentBoard (= last known state).
However, when another object — my image processing object — is finished with his method, it will change the array currentBoard in my AI object.
This is the same for a new array I created inside the main form. I think this happens cause of the heap/stack.
I hope I made my problem clear and understandable. I know I’m not the best in describing situations so please tell me when you don’t understand a part completely.
Arrays are reference types, so, as you’ve found, changing the array contents in one place will change it for any other code which holds a reference to the same object.
To avoid changing the array, you should take a deep copy of your 2 dimensional array and work with the copy instead. Note that we have to take a deep copy of both the array and its internal arrays:
You can also produce the same result using some simple linq: