首页 > 解决方案 > Visual Studio C# 隐形朋友游戏代码

问题描述

我一直在尝试如何制作情侣,我要做的是开发一个程序(控制台应用程序),我必须

  1. 添加玩家(最多 30 人)
  2. 在那之间混合这些球员(每个名字必须在每列中出现一次

例如:

玛丽亚的搭档是胡安,所以玛丽亚要把礼物送给胡安,每个人都必须在两边出现一次。我试过2个代码:

代码 1:

    Console.WriteLine("Bienvenido al juego del amigo invisible!");
    string[] nombres= new string[30];
    bool salir = false;
    int personas=30 ;
    int cant = 1;

    while (salir != true)
    {
        int opc;
        Console.WriteLine("1-Introducir parejas 2-Amigo invisible   3-Introducir parejas    4- Añadir más personas  5-Salir ");
        while (!int.TryParse(Console.ReadLine(), out opc))
        {
          Console.Write("Valor incorrecto, inserta de nuevo: ");
        }

        switch (opc)
        {
            case 1:
                Nombres(ref nombres, ref personas);
                break;
            case 2:
                Parejas(nombres,ref personas);
                break;
            case 3:
                Parejitas(cant);
                break;
            case 4:
                MasParejas(ref personas);
                break;      
            case 5:
                salir = true;
                break;
        }
    }

    static void Nombres(ref string[] nombres, ref int personas)
    {
        Console.WriteLine("Cuantas personas sois");
        while (!int.TryParse(Console.ReadLine(), out personas))
        {
           Console.Write("Valor incorrecto, inserta de nuevo: ");
        }
        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;)
        {
           Console.WriteLine("Las personas que jugaran contigo serán:" + nombres[a]);
        }

        Console.Clear();
    }
         
    static void Parejas(string[] nombres, ref int personas)
    {
        Random rnd = new Random();
        bool aprobado = false;
        int[] usados = new int[personas];
        Console.WriteLine("Hagamos las parjeas");
        foreach (string persons in nombres)
        {
            do
            {
                for (int i = 0; i < personas; i++)
                {
                    int h = rnd.Next(0, personas - 1);
                    do
                    {
                        string name = nombres[h];
                        if (name != null)
                        {
                            if (persons?.CompareTo(name) != 0 && usados.Contains(h))
                            {
                                aprobado = true;
                                Console.WriteLine($"{nombres[i]} va con {nombres[h]}");
                            }
                        }
                        usados[i] = h;
                    } while ( usados.Contains(h));
                }

            } while (!aprobado);

        }    
    }

    static void Parejitas(int cant)
    {
        Console.WriteLine("Hagamos las parejas por separado.");
        string[] parejitas = new string[cant];
        do
        {
            if (cant % 2 != 0)
            {
                while (!int.TryParse(Console.ReadLine(), out cant))
                {
                    Console.Write("Valor incorrecto, inserta de nuevo: ");
                }
            }
        } while (cant % 2 != 0);
        Console.WriteLine("Introduzca los nombres de las parejas");
        for (int i = 0; i < cant; i++)
        {
            parejitas[i] = Console.ReadLine();
        }
        for (int i = 0; i < cant; i++)
        {
            Console.WriteLine($"las parejas son {parejitas[i]} con {parejitas[i + 1]}");
        }
        Console.Clear();
    }

    static void MasParejas(ref int personas)
    {
        int más;
        Console.WriteLine("¿Cuanta gente quiere añadir?");
        while (!int.TryParse(Console.ReadLine(), out más))
        {
            Console.Write("Valor incorrecto, inserta de nuevo: ");
        }
        int total = personas + más;
        do
        {
            if (total >= 30 || más % 2 != 0)
            {
                Console.WriteLine("Ha superado el limite de personas o introducido un número inpar, el limite es 30");
                while (!int.TryParse(Console.ReadLine(), out más))
                {
                    Console.Write("Valor incorrecto, inserta de nuevo: ");
                }
            }

        } while (total >= 30 || más % 2 != 0);
        int a = 1;
        string[] personasañadidas = new string[más];
        Console.WriteLine("Introduzca los nombres de los nuevos");
        for (int i = 0; i < más; i++)
        {
            personasañadidas[i] = Console.ReadLine();
        }
        for (int j = 0;j < personasañadidas.Length; j++)
        {
            if (j< personasañadidas.Length-1)
            {
                Console.WriteLine($" Parejas: {personasañadidas[j]}  con  {personasañadidas[j + 1]}");
            }
            if (j==personasañadidas.Length-1)
            {
                Console.WriteLine($" Parejas: {personasañadidas[j]}  con  {personasañadidas[0]}");
            }
           
     

        }
    }

代码 2:

static void Main(string[] args)
{
    bool salir = false;
    int cantidadPersonas=30 ;
    string[] Personas = new string[cantidadPersonas];

    while (salir != true)
    {

        int opc;
        Console.WriteLine("1-Introducir parejas 2-Amigo invisible   3-Salir ");
        while (!int.TryParse(Console.ReadLine(), out opc))
        {
            Console.Write("Valor incorrecto, inserta de nuevo: ");
        }
        switch (opc)
        {
            case 1:
                MeterNombres(ref cantidadPersonas, ref Personas);
              

                break;

            case 2:
              GenerarParejas(cantidadPersonas, Personas);
                break;


            case 3:
                salir = true;
                break;
        }



    }

    static void MeterNombres(ref int cantidadPersonas, ref string[] Personas) {
        Console.WriteLine("¿Cuantos sois?");
        cantidadPersonas = Convert.ToInt32(Console.ReadLine());
        do
        {
            if (cantidadPersonas % 2 != 0)
            {
                cantidadPersonas = Convert.ToInt32(Console.ReadLine());
            }
        } while (cantidadPersonas % 2 != 0);

        Console.WriteLine("Introduzca los nombres de las personas que vayan a participar:");
        for (int i = 0; i < cantidadPersonas; i++)
        {
            Personas[i]= Console.ReadLine();

        }
        Console.Clear();
        Console.WriteLine("Los participantes son:");
        for (int i = 0; i < cantidadPersonas; i++)
        {
            Console.WriteLine(Personas[i]);
        }

    }

    static void GenerarParejas(int cantidadPersonas, string[] Personas)
    {
        Console.WriteLine("Comencemos a generar las parejas...");
        string[] PersonasCopia = new string[cantidadPersonas];
        Random rnd = new Random();
        bool Repeticiones = false;
        int NumeroAleatorio;
        for (int i = 0; i < cantidadPersonas; i++)
        {
            PersonasCopia[i] = Personas[i];
        }
        for (int i = 0; i < cantidadPersonas; i++)
        {
            do
            {
                Repeticiones = false;
                NumeroAleatorio = rnd.Next(0, cantidadPersonas);

                if (Personas[i] == PersonasCopia[i])
                {
                    Repeticiones = true;
                }
            } while (Repeticiones = false); ;
        }
    }

标签: c#

解决方案


推荐阅读