Added loop to check if next move is valid and corresponding board position is filled.