just swap the 'deleted' one with the 'last' one and update 'last' so that it has the index of the current last guy. |
But that changes the order of the players and I think the game requires that the circle just shrinks each time someone dies.
In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle |
Where do you start when proceeding around the circle? For example, suppose you have players 1 2 3 4 5 6. You start at 1 and skip 2 players . So skip #2 and #3, then shoot #4, leaving 1,2,3,5,6. Now do you start at #5 and skip 6 & 1? Or start at 3 and skip 5 & 6?
PrintPlayers(Players, K);
K is the number of players to skip. NPlayers is the total number. Don't you want to print NPlayers instead of K? This mistake is in two places.
One point that you seem to have missed is that you can go around the circle more than once: if you start with N=10 and K=10, then after shooting 6 people, you have N=4 and K=10. You go around the circle two and a half times to select the next victim. If P is the position that you start from then the person who gets shot is at position (P+K+1) % N. Change ShootRandomPlayer to use this math. Hint: you don't currently pass the value of N.
Use
Players[i]
instead of
*(Players+i)
. These are equivalent but most people will find the former easier to read.
In ShootRandomPlayer, you have a loop but you break out of every path in the loop. So you don't need a loop at all.
Your description doesn't mention anything about a gunholder. What is their function? Is NRandom the position where you start counting? And upon exit, is NRandom the position if the one who gets shot? Please add comments describing what ShootRandomPlayer is
supposed to do. Then we can help you figure out if it's doing what it should.
In main, your loop is wrong since NPlayers will probably shink each time through it.
I modified the program to work, assuming that after one person is killed, you start at the dead soul's former position. I didn't use GunHolder since your description doesn't mention this role. My ShootRandomPlayer signature looks like this:
1 2 3 4 5 6
|
// "Players" points to an array of "NPlayers" players. Starting at position P,
// Skip K players and shoot the K+1'th. Print your action and
// remove the shot player from the array. Upon return,
// Players, NPlayers and P are updated.
void
ShootRandomPlayer(int * &Players, int &NPlayers, int &P, int K)
|
Here's my output with N=10, K=3 and starting at P=2:
Starting at index 2 and skipping 3 players at each turn
INDEX : 0 1 2 3 4 5 6 7 8 9
ARRAY : 1 2 3 4 5 6 7 8 9 10
shot 7
ARRAY : 1 2 3 4 5 6 8 9 10
shot 2
ARRAY : 1 3 4 5 6 8 9 10
shot 8
ARRAY : 1 3 4 5 6 9 10
shot 4
ARRAY : 1 3 5 6 9 10
shot 1
ARRAY : 3 5 6 9 10
shot 10
ARRAY : 3 5 6 9
shot 3
ARRAY : 5 6 9
shot 6
ARRAY : 5 9
shot 9
ARRAY : 5
Player 5 is the last one standing |