I am trying to create a shallow copy of the structure of a Board (checkerboard). Before saving the transition to the board, I need to check whether the engine checks this move.
To do this, in the Move method (pointer method), I search for a pointer, refresh and check this possible panel to check. When I change the value of one value of type Board (for example, possible.headers = "Possible Varient" ), the original panel b does not change.
But here, when I call the updateBoard () method, it updates both boards. I still get the error (I can’t go into verification), but the main thread believes that the b.board (board position) has been changed.
func (b *Board) Move(orig, dest int) error { // validation ... // Update possible := *b // A 'shallow copy'? possible.updateBoard(orig, dest, val, isEmpassant, isCastle) king := possible.findKingPositionOfThePlayerWhoMoved() isCheck := possible.isInCheck(king) // bool takes the king to check for if isCheck { return errors.New("Cannot move into Check") } b.updateBoard(orig, dest, val, empassant, isCastle) return nil
Strange, but not all values updated with updateBoard() change. This means that the value of b.toMove does not change, but the value of b.board (position of the parts). This means that if I go through possible := b , the game will only be a white step (toMove alternates in the updateBoard () method). Use possible := *b rotate the rotation until you go to check. Then the transition is applied to b.board , but the error is discarded and check-players remains enabled (the value of possible.updateBoard() did not update b.toMove.
Edit
As Abhink remarked, in Host Usage and Internal Usage ,
Slicing does not copy slice data. It creates a new slice value that points to the original array.
b.board , a []byte always indicates its original value (even if the structure that contains it is dereferenced . abhink answer uses Go func copy(dst, src []Type) int , https://golang.org/pkg / builtin / # copy , shortcut for copying pointer values.