This project provides a graphical simulation of the Josephus problem using C++ and SFML for visual rendering. In the Josephus problem, people stand in a circle and are eliminated at regular intervals until only one survivor remains. This program visualizes each elimination step and highlights the final survivor with a graphical user interface (GUI).
The Josephus problem is a classic counting problem where:
- N people stand in a circle, waiting to be eliminated.
- Counting starts at a randomly selected person and proceeds clockwise.
- Every k-th person is eliminated until only one person remains, who is declared the survivor.
- Graphical Visualization: Using SFML, each person is represented as a sprite that visually shows the elimination process.
- Sound Effects: Each elimination and victory display includes sound effects for an engaging experience.
- User Input: The program accepts the number of people (N) and the step count (K) as input from the user.
- Customizable Radius: The circle size adjusts based on the number of people to maintain readability.
-
C++11 or newer.
-
SFML Library: You need to install SFML (Simple and Fast Multimedia Library) for graphics, window handling, and audio.
To install SFML, use:
sudo apt-get install libsfml-dev # For Debian/Ubuntu brew install sfml # For macOS
-
Clone the repository:
git clone <https://github.com/Rebelhere/Josephs-Problem> cd JosephusProblemSolver
-
Ensure you have the necessary assets in the same directory as the executable:
1.mp3
: Background music during the simulation.2.mp3
: Sound effect for each elimination.3.mp3
: Sound effect for the winner display.alive.png
: Image to represent alive participants.dead.png
: Image to represent eliminated participants.winner.jpg
: Image to represent the final survivor.1.ttf
: Font file for displaying text.
Compile the code with SFML:
g++ josephus.cpp -o josephus -lsfml-graphics -lsfml-window -lsfml-audio -lsfml-system
Run the program:
./josephus
- Start the program and enter values for:
- N: The number of people in the circle.
- K: The interval for elimination (every k-th person will be removed).
- The graphical window will open, displaying the participants in a circle.
- The elimination process will start, with each elimination shown in the GUI and accompanied by a sound.
- Once only one participant remains, they will be highlighted as the survivor with a congratulatory message and sound effect.
-
Queue Class: Custom queue implementation used to manage the circle of participants.
Enqueue(T val)
: Adds a participant to the queue.Dequeue()
: Removes a participant from the queue.kill()
: Executes the elimination process by removing every k-th person.josephproblem(RenderWindow& window)
: Manages the Josephus process and updates the GUI.
-
printwindow: Displays the current state of the circle, marking each person as either alive or dead.
-
printwinner: Displays the winning participant after all others are eliminated.
- Sprites: The program uses
alive.png
anddead.png
for participants. The survivor is shown withwinner.jpg
. - Sound Effects:
- Elimination events play the sound from
2.mp3
. - Background music plays from
1.mp3
. - Winner announcement plays
3.mp3
.
- Elimination events play the sound from
For N = 7 and K = 3:
- The program displays a circle of 7 participants.
- Each 3rd person is eliminated in clockwise order until only one remains.
- The GUI updates in real-time, showing the elimination sequence and final winner.
- Adjustable Parameters: Add user controls for adjusting speed and volume.
- Different Elimination Animations: Introduce more visual effects for eliminations.
Feel free to open issues or submit pull requests for improvements or bug fixes.
We welcome contributions from the open-source community. If you'd like to collaborate on this project, please adhere to the following guidelines:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Implement your changes and commit them with descriptive messages.
- Push your branch to your fork and submit a pull request.