Spec-
In the game the children sit in a circle and one person outside the ring (the leader) sings a song with a fixed number of words. Moving clockwise around the circle, the leader points to a new child in the ring for each word in the song. The child being pointed to on the last word of the song is out and must leave the circle. The leader then repeats the process with the smaller circle. The game continues until the last child is eliminated. This child is then the leader to start the next game.
I've almost got this logic figured out but there's a bug I can't find somewhere in the indexing of my array where I'm adding the current element to another array(to display the kids eliminated) and then removing them from the current array.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Practice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of kids: ");
int kidCount = sc.nextInt();
List<Integer> kids = new ArrayList<Integer>();
List<Integer> rejects = new ArrayList<Integer>();
System.out.println("Enter number of words in the song: ");
int words = sc.nextInt();
int wordCount = 0;
for (int i = 1; i <= kidCount; i++) {
kids.add(i);
}
for (int i = 0; i < kids.size(); i++) {
wordCount++;
if (wordCount % words == 0) {
rejects.add(kids.get(i));
kids.remove(kids.get(i));
}
if (i == kids.size() - 1) {
if (wordCount % words != 0) {
i = 0;
} else {
rejects.add(kids.get(i));
kids.remove(kids.get(i));
}
}
}
System.out.println(rejects.toString());
}
}
Expected output if there's 6 kids and 3 words: 3 6 4 2 5 1
Current output : [3, 2, 4, 6]