Advertisement
Guest User

mpguchii - retirar cartas vermelhas totalmente aleatorias

a guest
Apr 27th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing;
  7. namespace teste
  8. {
  9.  
  10.     class Program
  11.     {
  12.         public class Baralho
  13.         {
  14.             public List<Carta> Cartas { get; set; } //UM BARALHO CONTÉM UMA LISTA DE CARTAS
  15.         }
  16.  
  17.         public class Carta //UMA CARTA CONTEM UM NÚMERO E UMA COR
  18.         {
  19.             public int Numero { get; set; }
  20.             public Naipes Naipe { get; set; }
  21.         }
  22.  
  23.         public class Naipes // UM NAIPE CONTÉM UM NOME E UMA COR
  24.         {
  25.             public string Naipe { get; set; }
  26.             public Color Cor { get; set; }
  27.         }
  28.  
  29.         static void Main(string[] args)
  30.         {
  31.             Baralho _baralho = new Baralho(); //CRIO UM NOVO BARALHO VAZIO
  32.             int count = 0;
  33.             _baralho.Cartas = new List<Carta>(); //CRIO UMA NOVA LISTA DE CARTAS VAZIA
  34.             for (int i = 1; i < 15; i++)
  35.             {
  36.                 if (count>3) //SE JA ADICIONEI OS 4 NAIPES, SAIDO DO FOR
  37.                 {
  38.                     continue;
  39.                 }
  40.  
  41.                 if (i==14) //PARA TROCAR DE NAIPE
  42.                 {
  43.                     count++;
  44.                     i = 1;
  45.                 }
  46.  
  47.                 Carta _carta = new Carta(); //CRIO UMA NOVA CARTA
  48.                 Naipes _naipe = new Naipes(); //CRIO UM NOVO NAIPE
  49.  
  50.                 _carta.Numero = i;
  51.  
  52.                 switch (count) //PARA ADICIONAR O NAIPE NA CARTA
  53.                 {
  54.                     case 0:
  55.                         _naipe.Cor = Color.Black;
  56.                         _naipe.Naipe = "Paus";
  57.                         break;
  58.                     case 1:
  59.                         _naipe.Cor = Color.Red;
  60.                         _naipe.Naipe = "Ouros";
  61.                         break;
  62.                     case 2:
  63.                         _naipe.Cor = Color.Red;
  64.                         _naipe.Naipe = "Copas";
  65.                         break;
  66.                     case 3:
  67.                         _naipe.Cor = Color.Black;
  68.                         _naipe.Naipe = "Espadas";
  69.                         break;
  70.                     default:
  71.                         break;
  72.                 }
  73.  
  74.                 _carta.Naipe = _naipe;
  75.  
  76.                 _baralho.Cartas.Add(_carta); //ADICIONO A CARTA NO BARALHO
  77.             }
  78.  
  79.  
  80.             Random rndC = new Random();
  81.             _baralho.Cartas = _baralho.Cartas.OrderBy(x => rndC.Next()).ToList(); //EMBARALHO O BARALHO
  82.  
  83.             int execucao = 0;
  84.             int carta = 0;
  85.  
  86.             Baralho _baralhoVermelho = new Baralho();
  87.             _baralhoVermelho.Cartas = new List<Carta>();
  88.             var cartasRetiradas = new HashSet<int>();
  89.  
  90.             while (true)
  91.             {
  92.  
  93.                 if (_baralhoVermelho.Cartas.Count == 26) //PEGUEI UM BARALHO VERMELHO COMPLETO
  94.                 {
  95.                     break;
  96.                 }
  97.                
  98.                 Random rnd = new Random();
  99.                 if (execucao == 0)
  100.                 {
  101.                     carta = rnd.Next(0, 53); //PEGO UMA CARTA ALETORIA
  102.                 }
  103.                 else
  104.                 {
  105.                     //PEGO UMA CARTA ALEATORIA, TIRANDO AS QUE JA SAIRAM
  106.                     var range = Enumerable.Range(0, 53).Where(i => !cartasRetiradas.Contains(i));
  107.                     var rand = new Random();
  108.  
  109.                     int index = rand.Next(0, 53 - cartasRetiradas.Count);
  110.                     carta = range.ElementAt(index);
  111.  
  112.                 }
  113.                
  114.                 if (_baralho.Cartas[carta].Naipe.Cor == Color.Red) //UMA CARTA DE UM NAIPE VERMEHO
  115.                 {
  116.                     cartasRetiradas.Add(carta); //REMOVO A CARTA PARA NAO SELECIONAR ELA DENOVO
  117.                     _baralhoVermelho.Cartas.Add(_baralho.Cartas[carta]);
  118.                 }
  119.  
  120.                 execucao++;
  121.             }
  122.  
  123.             Console.WriteLine("Cartas Vermelhas Retiradas!");
  124.             Console.WriteLine($"Tentativas até retirar todas as cartas vermelhas {execucao}");
  125.             Console.WriteLine("Cartas Retiradas:");
  126.  
  127.             foreach (var item in _baralhoVermelho.Cartas)
  128.             {
  129.                 Console.WriteLine($"Carta número {item.Numero} do naipe {item.Naipe.Naipe}");
  130.             }
  131.  
  132.             Console.ReadLine();
  133.  
  134.         }
  135.  
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement