My code keeps skipping over a conditional if statement when the condition is met. I have put print statements in the if statement to make sure and the program keeps skipping over it. I also put print statements before the condition of the if statement to make sure that the condition is being met, and yet it is still not executing the if statement. This is bizarre and I am wondering if someone can see what the problem is because I haven't been able to figure this out after nearly 3 hours.
On the if statement that says if(randomNum ==1) it keeps not executing that statement even though in the instances in which randomNum is equal to 1 (which I have checked with if statements). I know this because the print statement (System.out.print("line 117")) is not executing because "Line 117" is not showing up in the console.
private int[] createSequence() {
System.out.println("line 52");
int [] blockSequence = new int [15]; //sequence of spaceship arrivals according to their indices (e.g., "1" next means CS ship appears next in the sequence; "4" means a non-predictive friendly ship appears next)
int [] remainingSpaceships = {3,2,2,2,2,2,2};
int randomNum;
int indexOfFriendlySpaceshipThatPrecedesEnemyOnceThisBlock; //The index for the non-predictive friendly spaceship that precedes an enemy spaceship once in each block
int appearanceOfNonPredictiveFriendlySpaceshipInWhichItWillPrecedeEnemy; //Whether this spaceship will precede the enemy spaceship on its first or second appearance of this block
boolean previousSpaceshipWasCS = false;
boolean previousSpaceshipWasFriendlySpaceshipThatPrecedesEnemyOnceThisBlockAndRequiresEnemyToFollow = false;
indexOfFriendlySpaceshipThatPrecedesEnemyOnceThisBlock = ((int)(Math.random()*5)) + 2;
appearanceOfNonPredictiveFriendlySpaceshipInWhichItWillPrecedeEnemy = (int)(Math.random()*2) + 1;
System.out.println("indexOfFriendlySpaceshipThatPrecedesEnemyOnceThisBlock: " + indexOfFriendlySpaceshipThatPrecedesEnemyOnceThisBlock);
for(int i = 0; i < blockSequence.length; i++){
if(previousSpaceshipWasCS == true){
blockSequence[i] = 0;
remainingSpaceships[0]--;
previousSpaceshipWasCS = false;
System.out.println("line 72");
System.out.println("line 73 blockSequence[" + i + "]: " + blockSequence[i]);
}
else if(previousSpaceshipWasFriendlySpaceshipThatPrecedesEnemyOnceThisBlockAndRequiresEnemyToFollow == true){
blockSequence[i] = 0;
remainingSpaceships[0]--;
previousSpaceshipWasFriendlySpaceshipThatPrecedesEnemyOnceThisBlockAndRequiresEnemyToFollow = false;
System.out.println("line 81");
System.out.println("line 81 blockSequence[" + i + "]: " + blockSequence[i]);
}
else if(i == 13 && previousSpaceshipWasCS == false && previousSpaceshipWasFriendlySpaceshipThatPrecedesEnemyOnceThisBlockAndRequiresEnemyToFollow == false && remainingSpaceships[0] == 1){
if(remainingSpaceships[1] == 1){ //if there is one CS appearance left
blockSequence[i] = 1;
remainingSpaceships[1]--;
previousSpaceshipWasCS = true;
System.out.println("line 91");
}
else if(remainingSpaceships[indexOfFriendlySpaceshipThatPrecedesEnemyOnceThisBlock] == 1){ //if the non-predictive friendly that appears before the enemy once for this block has not yet been followed by the enemy
blockSequence[i] = indexOfFriendlySpaceshipThatPrecedesEnemyOnceThisBlock;
remainingSpaceships[indexOfFriendlySpaceshipThatPrecedesEnemyOnceThisBlock]--;
previousSpaceshipWasFriendlySpaceshipThatPrecedesEnemyOnceThisBlockAndRequiresEnemyToFollow = true;
System.out.println("line 98");
}
}
else{
System.out.println("line 104");
do{
randomNum = (int)(Math.random()*7);
}while(remainingSpaceships[randomNum]==0 || randomNum == 0);
System.out.println("line 110 randomNum: " + randomNum);
if (randomNum == indexOfFriendlySpaceshipThatPrecedesEnemyOnceThisBlock){
if((remainingSpaceships[indexOfFriendlySpaceshipThatPrecedesEnemyOnceThisBlock] == 2 && appearanceOfNonPredictiveFriendlySpaceshipInWhichItWillPrecedeEnemy == 1) || (remainingSpaceships[indexOfFriendlySpaceshipThatPrecedesEnemyOnceThisBlock] == 1 && appearanceOfNonPredictiveFriendlySpaceshipInWhichItWillPrecedeEnemy == 2)){
previousSpaceshipWasFriendlySpaceshipThatPrecedesEnemyOnceThisBlockAndRequiresEnemyToFollow = true;
System.out.println("line 112");
}
if (randomNum == 1){
previousSpaceshipWasCS = true;
System.out.println("line 117");
}
}
blockSequence[i] = randomNum;
remainingSpaceships[randomNum]--;
System.out.println("line 126 blockSequence[" + i + "]: " + blockSequence[i]);
}
System.out.println("remaining spaceships: " + remainingSpaceships[0] + remainingSpaceships[1]+remainingSpaceships[2]+remainingSpaceships[3]+remainingSpaceships[4]+remainingSpaceships[5]+remainingSpaceships[6]);
}
return blockSequence;
}
Aucun commentaire:
Enregistrer un commentaire