Jump to content

Invisible game friend in C#

In my university, they've sent us two task but I need help on this one, We need to do an invisible friend game and I am stuck. This is all I have:

 string[] nombres;
            int personas = 0;
            string[] parejas;
              nombres = new string[personas];

            Console.WriteLine("Eliga: 1)Introducir nombres\n       2)Amigo Invisible\n       3)Salir");
            int opcion = Convert.ToInt32(Console.ReadLine());
            switch (opcion)
            {
                case 1:
                    Console.WriteLine("Cuantas personas sois");
                    personas = Convert.ToInt32(Console.ReadLine());
                    nombres = new string[personas];
                    Console.WriteLine("introduzca" + personas + "nombres");
                    for (int a = 0; a < personas; ++a)
                    {
                        nombres[a]= Console.ReadLine();
                    }
                    for(int a= 0; a < personas; ++a)
                    {
                        Console.WriteLine("Las personas que jugaran contigo serán:" + nombres);
                    }
                    break;
                    
                case 2:
                    Console.WriteLine("Ahora toca hacer las parejas.");
                    List<int> indices = new List<int>();
                    Random gen = new Random();
                    int i, indice;
                    for (i = 0; i < personas; i++)
                    {
                        if (i == personas - 1 && indices.Contains(i) == false) // si es el último y sólo queda como amigo posible si mismo, lo cambiamos por el de arriba            
                        {
                            indices.Insert(personas - 2, i); // insertando el último, que quedará como anteúltimo y éste se corre a último         
                        }
                        else
                        {
                            while (true) // itera "infinitamente" hasta generar un índice aleatorio que no esté en la lista y que no sea si mismo (i)
                            {
                                indice = gen.Next(0, personas - 1); // nro al azar entre 0 y personas - 1, que son los índices válidos
                                if (indices.Contains(indice) || i == indice) // si el índice generado es el mismo i, o ya está, continua, o sea lo genera otra vez
                                    continue;
                                else  // si no está ni es si mismo (i), interrumpe, sale del while
                                    break;
                            }
                            indices.Add(indice); // agrega el índice de su amigo invisible
                        }
                    }
                    Console.WriteLine("Lista de nombres y sus correspondientes amigos invisibles");
                    for (i = 0; i < personas; i++)
                        Console.WriteLine(nombres + " ---> " + nombres[indices]);
                    break;

 
                case 3:
                    Console.WriteLine("Salir");
                    break;

              }

The problem I have is that the first option you can put the names and creat the listing but it doesn't save the names for option 2 when it has to do the pairs.

Thanks for the help and HAPPY NEW YEAR!

Program.cs

Link to comment
Share on other sites

Link to post
Share on other sites

I think you want to wrap the option selection and the switch statement in a while loop, and use one of the options as a way to stop the loop.

 

If you want to keep information persistent between iterations of the loop you want to declare those outside (before) the loop.

#killedmywife #howtomakebombs #vgamasterrace

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×