Just changing the value of a variable changes the value of a node in my linked list? This should not happen

So, I am making a chess engine in Java. Given the current configuration of the board, the AI ​​should find out every possible move that it can make, add it to the linked list of all possible moves and return this list. I am currently testing the following board configuration:

/*bRbNbBbQbKbBbNbR
  bPbPbPbPbPbPbPbP
  NuNuNuNuNuNuNuNu
  NuNuNuwRNuNuNuNu
  NuNuNuNuNuNuNuNu
  NuNuNuNuNuNuNuNu
  wPwPwPwPwPwPwPwP
  NuwNwBwQwKwBwNwR*/

“bR” means the black rook, “bN” means the black knight, etc. "Nu" means "zero" or "no." In this configuration, I moved the lower left rook to the middle of the board.

The next method possibleMoves()in my class Mobilityis what should generate and return a linked list of all possible board configurations. Each index icorresponds to parts on the board starting from the left. In this case, AI is white, so 0 is the leftmost white pawn, 7 is the rightmost white pawn, 8 is the white rook now in the center of the board, 9 is the other white rook, etc. Right now I'm just checking the rooks, so the other conventions are empty. nonPawnBoardGen()returns a list of possible board configurations.

public LL possibleMoves(){
    LL children = new LL();
    /*
     * check each piece
     */
    for(int i = 0; i < 16; i++){
        if(i < 8){//pawns

        }
        else if(i < 10){//rooks
            children.joinWith(nonPawnBoardGen(i, 0, -1)); //positions to the left
            children.joinWith(nonPawnBoardGen(i, 0, 1)); //right
            children.joinWith(nonPawnBoardGen(i, -1, 0)); //up
            children.joinWith(nonPawnBoardGen(i, 1, 0)); //down
        }
        else if(i < 12){
        //  checkKnight(r, c, int dR, int dC)
        }
        else if(i < 14){//bishops

        }
        else{ //king, queen

        }
    }
    return children;
}

joinWith(), in my LL class, joins a subscription with a general list of related children.

public void joinWith(LL newList){
    if(newList.isEmpty())
        return;
    if(this.isEmpty()){
        first = newList.getFirst();
        last = newList.getLast();
    }
    else{
        last.next = newList.getFirst();
        last = newList.getLast();
    }
}

nonPawnBoardGen() - , index . , , nonPawnBoardGen(8, 0, -1), - 8, . , , , .

private LL nonPawnBoardGen(int index, int vecR, int vecC){
    LL boardSubLst = new LL();
    int sR, sC; //source row and col
    if(turn == true){//white
        //last 16 coords are white pieces
        if(coords[index + 16] == null){//if piece does not exist, return
            return null;
        }
        sR = coords[index + 16].getRow(); //each coord is an object containing a row and col value
        sC = coords[index + 16].getCol();
    }
    else{//black
        //first 16 coords are black pieces
        if(coords[index] == null){
            return null;
        }
        sR = coords[index].getRow();
        sC = coords[index].getCol();
    }
    int curR = sR; //current row
    int curC = sC; //current col
    curR+=vecR; //iterate by unit vector
    curC+=vecC;
    while(curR > -1 && curR < 8 && curC > -1 && curC < 8){ //while in range of board
        if(turn == true){//white
            if(board[curR][curC].charAt(0) != 'w'){ //if space is free or opposite color, valid move
                coords[index + 16].setRow(curR); //move rook to new position
                coords[index + 16].setCol(curC);
                if(board[curR][curC].charAt(0) == 'b'){ //if space contains piece of opposite color,
                    int r, c;                           //piece needs to be removed
                    for(int j = 0; j < 16; j++){ //iterate through 16 opponent pieces
                        r = coords[j].getRow();
                        c = coords[j].getCol();
                        if(curR == r && curC == c){ //check which opponent piece coords match
                            coords[j] = null;       //the rook current coords, then remove opp piece
                            boardSubLst.insert(coords); //add board config to sublist
                            break;
                        }
                    }
                    break;
                }
                else{ //if the space is null, simply add board config to sublist
                    boardSubLst.insert(coords);
                }
            }
            else{ //if space is same color, break
                break;
            }
        }
        else{//black
            if(board[curR][curC].charAt(0) != 'b'){
                coords[index].setRow(curR);
                coords[index].setCol(curC);
                if(board[curR][curC].charAt(0) == 'w'){
                    int r, c;
                    for(int j = 0; j < 16; j++){
                        r = coords[j + 16].getRow();
                        c = coords[j + 16].getCol();
                        if(curR == r && curC == c){
                            coords[j + 16] = null;
                            boardSubLst.insert(coords);
                            break;
                        }
                    }
                    break;
                }
                else{
                    boardSubLst.insert(coords);
                }
            }
            else{
                break;
            }
        }
        curR+=vecR;
        curC+=vecC;
    }
    return boardSubLst;
}

, nonPawnBoardGen(), , , ( ):

coords[index + 16].setRow(curR);
coords[index + 16].setCol(curC);

:

boardSubLst.insert(coords);

, coords, boardSubList coords. ?

EDIT: , , nonPawnBoardGen() . , . possibleMoves(). , ...

+4
2

boardSubLst.insert(coords);

coords. , , , Arrays.copyOf(T[] original, int newLength)

boardSubLst.insert(Arrays.copyOf(coords, coords.length));

, , coords Coord[], System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

Coord[] coords2 = new Coord[coords.length];   
System.arraycopy(coords, 0, coords2, 0, coords.length);
boardSubLst.insert(coords2);
+1

, . . . , . , , . , , , . , , , , . . !

0

All Articles