TheBulgarianWolf

Cards Game Using Queue

Dec 14th, 2020
1,052
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Cards_Game_Using_Queue
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Queue<int> firstPlayerCards = new Queue<int>();
  12.             Queue<int> secondPlayerCards = new Queue<int>();
  13.             Console.WriteLine("The first player should enter his cards power with space between them: ");
  14.             int[] firstCards = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
  15.             Console.WriteLine("The second player should enter his cards power with space between them: ");
  16.             int[] secondCards = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
  17.             foreach(int card in firstCards)
  18.             {
  19.                 firstPlayerCards.Enqueue(card);
  20.             }
  21.  
  22.             foreach (int card in secondCards)
  23.             {
  24.                 secondPlayerCards.Enqueue(card);
  25.             }
  26.  
  27.             while(firstPlayerCards.Count > 0 && secondPlayerCards.Count > 0)
  28.             {
  29.                 int firstElement = firstPlayerCards.ElementAt(0);
  30.                 int secondElement = secondPlayerCards.ElementAt(0);
  31.                 if (firstPlayerCards.ElementAt(0) > secondPlayerCards.ElementAt(0))
  32.                 {
  33.                     firstPlayerCards.Dequeue();
  34.                     secondPlayerCards.Dequeue();
  35.                     firstPlayerCards.Enqueue(firstElement);
  36.                     firstPlayerCards.Enqueue(secondElement);
  37.                 }
  38.                 else if(secondPlayerCards.ElementAt(0) > firstPlayerCards.ElementAt(0))
  39.                 {
  40.                     firstPlayerCards.Dequeue();
  41.                     secondPlayerCards.Dequeue();
  42.                     secondPlayerCards.Enqueue(secondElement);
  43.                     secondPlayerCards.Enqueue(firstElement);
  44.                 }
  45.                 else
  46.                 {
  47.                     firstPlayerCards.Dequeue();
  48.                     secondPlayerCards.Dequeue();
  49.                 }
  50.             }
  51.  
  52.             if(firstPlayerCards.Count > secondPlayerCards.Count)
  53.             {
  54.                 Console.WriteLine("The first player won and has a sum of: " + firstPlayerCards.Sum());
  55.             }
  56.             else if(secondPlayerCards.Count > firstPlayerCards.Count)
  57.             {
  58.                 Console.WriteLine("The second player won and has a sum of: " + secondPlayerCards.Sum());
  59.             }
  60.             else
  61.             {
  62.                 Console.WriteLine("It's a draw!");
  63.             }
  64.         }
  65.     }
  66. }
  67.  
Add Comment
Please, Sign In to add comment