Jump to content

A problem of logic and pattern finding

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Baumerr

Baumerr

    Newbie

  • Members
  • Pip
  • 3 posts
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!

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Baumerr

Baumerr

    Newbie

  • Members
  • Pip
  • 3 posts
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!

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You could store it in an array, but that does get trickier.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
anotheruser

anotheruser

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
c#:


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).