Advertisement
Guest User

Хамалското

a guest
Sep 2nd, 2017
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.20 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.  
  7. namespace _03.Numbers_War
  8. {
  9.     public class Program
  10.     {
  11.         public static void Main()
  12.         {
  13.             int turns = 0;
  14.  
  15.             char[] letters = new char[]
  16.             {
  17.                 ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  18.                 'u',
  19.                 'v', 'w', 'x', 'y', 'z'
  20.             };
  21.  
  22.             string[] firstPlayerCardsInput = Console.ReadLine().Split().ToArray();
  23.             string[] secondPlayerCardsInput = Console.ReadLine().Split().ToArray();
  24.  
  25.             Queue<string> firstPlayerCards = new Queue<string>(firstPlayerCardsInput);
  26.             Queue<string> secondPlayerCards = new Queue<string>(secondPlayerCardsInput);
  27.  
  28.             while (firstPlayerCards.Count > 0 && secondPlayerCards.Count > 0 && turns < 1_000_000)
  29.             {
  30.                 string card1 = firstPlayerCards.Dequeue();
  31.                 string card2 = secondPlayerCards.Dequeue();
  32.  
  33.                 if (int.Parse(card1.Substring(0, card1.Length - 1)) > int.Parse(card2.Substring(0, card2.Length - 1)))
  34.                 {
  35.                     firstPlayerCards.Enqueue(card1);
  36.                     firstPlayerCards.Enqueue(card2);
  37.                     turns++;
  38.                     continue;
  39.                 }
  40.                 if (int.Parse(card1.Substring(0, card1.Length - 1)) < int.Parse(card2.Substring(0, card2.Length - 1)))
  41.                 {
  42.                     secondPlayerCards.Enqueue(card2);
  43.                     secondPlayerCards.Enqueue(card1);
  44.                     turns++;
  45.                     continue;
  46.                 }
  47.                 if (int.Parse(card1.Substring(0, card1.Length - 1)) == int.Parse(card2.Substring(0, card2.Length - 1)))
  48.                 {
  49.                     if (card1[card1.Length - 1] > card2[card2.Length - 1])
  50.                     {
  51.                         firstPlayerCards.Enqueue(card1);
  52.                         firstPlayerCards.Enqueue(card2);
  53.                         turns++;
  54.                         continue;
  55.                     }
  56.                     if (card1[card1.Length - 1] < card2[card2.Length - 1])
  57.                     {
  58.                         secondPlayerCards.Enqueue(card2);
  59.                         secondPlayerCards.Enqueue(card1);
  60.                         turns++;
  61.                         continue;
  62.                     }
  63.  
  64.                     int firstSum = 0;
  65.                     int secondSum = 0;
  66.  
  67.                     List<string> firstCards = new List<string>();
  68.                     List<string> secondCards = new List<string>();
  69.                     firstCards.Add(card1);
  70.                     secondCards.Add(card2);
  71.  
  72.                     while (firstSum == secondSum && firstPlayerCards.Count > 0 && secondPlayerCards.Count > 0)
  73.                     {
  74.                         for (int i = 0; i < 3; i++)
  75.                         {
  76.                             if (firstPlayerCards.Count != 0)
  77.                             {
  78.                                 string card = firstPlayerCards.Peek();
  79.                                 firstCards.Add(firstPlayerCards.Dequeue());
  80.                                 firstSum += Array.IndexOf(letters, card[card.Length - 1]);
  81.                             }
  82.                             if (secondPlayerCards.Count != 0)
  83.                             {
  84.                                 string card = secondPlayerCards.Peek();
  85.                                 secondCards.Add(secondPlayerCards.Dequeue());
  86.                                 secondSum += Array.IndexOf(letters, card[card.Length - 1]);
  87.                             }
  88.                         }
  89.                     }
  90.                     List<string> allCards = new List<string>();
  91.                     foreach (var c in firstCards)
  92.                     {
  93.                         allCards.Add(c);
  94.                     }
  95.                     foreach (var c in secondCards)
  96.                     {
  97.                         allCards.Add(c);
  98.                     }
  99.                     if (firstSum > secondSum)
  100.                     {
  101.                         foreach (var c in allCards.OrderByDescending(x => int.Parse(x.Substring(0, x.Length - 1)))
  102.                             .ThenByDescending(x => x[x.Length - 1]))
  103.                         {
  104.                             firstPlayerCards.Enqueue(c);
  105.                         }
  106.                         turns++;
  107.                         continue;
  108.                     }
  109.                     if (firstSum < secondSum)
  110.                     {
  111.                         foreach (var c in allCards.OrderByDescending(x => int.Parse(x.Substring(0, x.Length - 1)))
  112.                             .ThenByDescending(x => x[x.Length - 1]))
  113.                         {
  114.                             secondPlayerCards.Enqueue(c);
  115.                         }
  116.                         turns++;
  117.                         continue;
  118.                     }
  119.                     else
  120.                     {
  121.                         turns++;
  122.                         continue;
  123.                     }
  124.                 }
  125.             }
  126.  
  127.             if (firstPlayerCards.Count == 0 && secondPlayerCards.Count == 0)
  128.             {
  129.                 Console.WriteLine($"Draw after {turns} turns");
  130.             }
  131.             else if (secondPlayerCards.Count == 0)
  132.             {
  133.                 Console.WriteLine($"First player wins after {turns} turns");
  134.             }
  135.             else if (firstPlayerCards.Count == 0)
  136.             {
  137.                 Console.WriteLine($"Second player wins after {turns} turns");
  138.             }
  139.             else
  140.             {
  141.                 if (firstPlayerCards.Count > secondPlayerCards.Count)
  142.                 {
  143.                     Console.WriteLine($"First player wins after 1000000 turns");
  144.                 }
  145.                 else if (firstPlayerCards.Count < secondPlayerCards.Count)
  146.                 {
  147.                     Console.WriteLine($"Second player wins after 1000000 turns");
  148.                 }
  149.                 else
  150.                 {
  151.                     Console.WriteLine($"Draw after 1000000 turns");
  152.                 }
  153.             }
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement