I'm trying to write the Josephus algorithm as a project for school, however, I think that there is a better way than what I already have.
public static int[] StartTheGame(int prisoners, int count)
{
var position = count - 1;
int[] list = new int[prisoners];
for (int i = 0; i < prisoners; i++)
{
list[i] = 1;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(" {0} ", list[i]);
}
Console.WriteLine();
for (int i = 1; i <= prisoners - 1; i++)
{
while (position + 1 >= prisoners)
{
position = (position) - prisoners;
}
if (list[position + 1] == 0)
{
position++;
}
if (list[position + 1] == 1)
{
list[position + 1] = 0;
}
position = position + count + 1;
ShowInConsole(prisoners, list);
}
return list;
}
Any ideas to make it better?
The output for 10 and 2 is:
Full code:
private static int _prisoners, _count;
static void Main()
{
while (true)
{
Console.ForegroundColor = ConsoleColor.White;
GetInitialValues();
int[] lastManAlive = StartTheGame(_prisoners, _count);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("The last person who is freed is standing at position {0}. ", IsFreed(lastManAlive));
}
}
private static void GetInitialValues()
{
Console.WriteLine("Please enter the number of prisoners that are going to be hanged: ");
try
{
_prisoners = (int) Convert.ToInt64(Console.ReadLine());
}
catch (Exception)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Something went wrong, please try again");
GetInitialValues();
}
Console.WriteLine("Please enter a number less than the number of prisoners and greater than 0 to start the game and hang the prisoners: ");
try
{
_count = (int)Convert.ToInt64(Console.ReadLine());
}
catch (Exception)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Something went wrong, please try again");
GetInitialValues();
}
while (_count <= 0)
{
Console.WriteLine("Please enter a number greater than 0 to start the game and hang the prisoners: ");
_count = (int)Convert.ToInt64(Console.ReadLine());
}
}
public static int[] StartTheGame(int prisoners, int count)
{
var position = count - 1;
int[] list = new int[prisoners];
InitiateGame(list, prisoners);
for (int i = 1; i <= prisoners - 1; i++)
{
while (position + 1 >= prisoners)
{
position = (position) - prisoners;
}
if (list[position + 1] == 0)
{
position++;
}
if (list[position + 1] == 1)
{
list[position + 1] = 0;
}
position = position + count + 1;
ShowInConsole(prisoners, list);
}
return list;
}
private static void InitiateGame(int[] list, int prisoners)
{
for (int i = 0; i < prisoners; i++)
{
list[i] = 1;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(" {0} ", list[i]);
}
Console.WriteLine();
}
public static int IsFreed(int[] list)
{
var length = list.Length;
for (int i = 1; i <= length; i++)
{
if (list[i - 1] == 1)
{
return i;
}
}
return -1;
}
public static void ShowInConsole(int prisoners, int[] list)
{
Console.WriteLine();
for (int j = 0; j < prisoners; j++)
{
if (list[j] == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
}
Console.Write(" {0} ", list[j]);
}
Console.WriteLine();
}