I'm in need of help with this problem, I have to make a java program for it, but first I have to understand the logic behind it.
Basically, there is a game where players are arranged in a circle marked 0 to x-1 clockwise, where x is equal to the amount of players(for example, in a 4 player game the order clockwise would be; 0,1,2,3.)
Player 0 passes a sword twice clockwise, and the person to receive it kills the next person, and the circle closes. (So 0 would pass it to 1, who passes it to 2, who kills 3.) The sword starts from player 2 after the circle has closed, and the game continues until only one player is left.
For reference, i have completed the first 10 iterations
# of players; Ideal position
2;0
3;2
4;1
5;4
6;1
7;4
8;7
9;1
10;4
The real task at hand is to detail a method for X amount of players which would produce the last surviving player. The program (later on) should take the user's input for number of players and print out the ideal position.
I haven't been able to quite figure this out, and the only hint I have is that the program should use modulus, suggesting some sort of division with remainders to produce a result.
Any help is appreciated, thank you very much!
A problem of logic and pattern finding
Started by Baumerr, Aug 13 2010 07:14 AM
4 replies to this topic
#1
Posted 13 August 2010 - 07:14 AM
|
|
|
#2
Posted 13 August 2010 - 01:25 PM
I would do a Linked List as a loop, where each node knows it's original position and next in line, and the "Sword" is a pointer into the linked list that moves around it.
#3
Posted 13 August 2010 - 02:12 PM
I'm not sure how to go about doing that, but the thing is, this is designed as an intro assignment to an "Intro to computer science" class, so I believe there must be a simpler way to do it. I haven't been able to find a pattern to detail the method however.
I'll look into it though, thanks!
I'll look into it though, thanks!
#4
Posted 13 August 2010 - 02:44 PM
You could store it in an array, but that does get trickier.
#5
Posted 14 August 2010 - 08:45 AM
c#:
On an unrelated note: What kind of teacher tells you about a game that involves people ritualistically killing each other with swords? That teacher is either awesome or crazy (or a combination of both).
List<int> remainingPlayers = new List<int>(x)
for (int i = 0; i < x; i++)
remainingPlayers.Add(i + 1);
int indexOfPersonWithSword = 0;
while (remainingPlayers.Count > 1)
{
indexOfPersonWithSword += 2;
int indexOfPersonWhoDies = indexOfPersonWithSword + 1;
while (indexOfPersonWithSword >= remainingPlayers.Count)
indexOfPersonWithSword -= remainingPlayers.Count;
while (indexOfPersonWhoDies >= remainingPlayers.Count)
indexOfPersonWhoDies -= remainingPlayers.Count;
if (indexOfPersonWhoDies < indexOfPersonWithSword)
indexOfPersonWithSword--;
remainingPlayers.RemoveAt(indexOfPersonWhoDies);
}
return remainingPlayers[0];
On an unrelated note: What kind of teacher tells you about a game that involves people ritualistically killing each other with swords? That teacher is either awesome or crazy (or a combination of both).


Sign In
Create Account

Back to top









