So I am making a connect 4 game but there is an issue with a function.
public static int[][] setDisc(int[][] board, int move, int turn)
{
int y = 0;
while(y + 1 <= 6)
{
if(board[y][move - 1] != 0)
break;
y++;
}
y--;
board[y][move - 1] = turn;
return board;
}
which should take a board and set the spot on the board according to the move and who's turn it is
public static int bestSpot(int[][] board, int turn){
//tries to win
if(locationOfAllThreeFores(board, turn) != -1)
{
System.out.println("I won");
return locationOfAllThreeFores(board, turn);
}
//tries to block
if(locationOfAllThreeFores(board, oppositeTurn(turn)) != -1){
System.out.println("blocked you");
return locationOfAllThreeFores(board, oppositeTurn(turn));
}
// random
System.out.println("random");
int ranMove = randomNum(7, 1);
board = setDisc(board,ranMove,turn);
//^^ for testing purposes, it affects the final board
return ranMove;
}
this method calls the setDisc method but it shouldn't affect the board but in testing it does.
board = setDisc(board, bestSpot(board, turn), turn);
I first call this function, which should set the disk on the board give in the best spot. what ends up happening is that the move is done twice. the line
board = setDisc(board,ranMove,turn);
in the bestSpot method somehow changes the final board even though it should only affect its version of the board and not the final one.
If there is some java rule I am missing please enlighten me. I have a link to a myreplit, an online java complier. It shows how its breaking.