Viktomirova

CardGame

Dec 14th, 2021
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace CardGame
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             //You start from the beginning of both hands.
  12.  
  13.             List<int> firstDeck = Console.ReadLine()
  14.                                          .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  15.                                          .Select(int.Parse)
  16.                                          .ToList();
  17.             List<int> secondDeck = Console.ReadLine()
  18.                                          .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  19.                                          .Select(int.Parse)
  20.                                          .ToList();
  21.  
  22.             //Compare the cards from the first deck to the cards from the second deck.
  23.             //The player, who has the bigger card, takes both cards and
  24.             //puts them at the back of his hand ‐ the second
  25.             //player’s card is last, and the first person’s card (the winning one)
  26.             //is before it (second to last) and the player with
  27.             //the smaller card must remove the card from his deck.
  28.  
  29.             while (true)
  30.             {
  31.                 if (firstDeck.Count == 0 || secondDeck.Count == 0)
  32.                 {
  33.                     break;
  34.                 }
  35.                
  36.                 if (firstDeck.First() > secondDeck.First())
  37.                 {
  38.                     firstDeck.Add(firstDeck[0]);
  39.                     firstDeck.Remove(firstDeck[0]);
  40.                     firstDeck.Add(secondDeck[0]);
  41.                     secondDeck.Remove(secondDeck[0]);
  42.                 }
  43.                 else if (firstDeck[0] < secondDeck[0])
  44.                 {
  45.                     secondDeck.Add(secondDeck[0]);
  46.                     secondDeck.Remove(secondDeck[0]);
  47.                     secondDeck.Add(firstDeck[0]);
  48.                     firstDeck.Remove(firstDeck[0]);
  49.                 }
  50.  
  51.                 //If both players’ cards have the same values ‐ no one wins, and 
  52.                 //the two cards must be removed from the decks.
  53.  
  54.                 else if (firstDeck[0] == secondDeck[0])
  55.                 {
  56.                     firstDeck.Remove(firstDeck[0]);
  57.                     secondDeck.Remove(secondDeck[0]);
  58.                 }
  59.  
  60.             }
  61.  
  62.             if (firstDeck.Count == 0)
  63.             {
  64.                 Console.WriteLine($"Second player wins! Sum: {secondDeck.Sum()}");
  65.             }
  66.             else
  67.             {
  68.                 Console.WriteLine($"First player wins! Sum: {firstDeck.Sum()}");
  69.             }
  70.             //The game is over, when one of the decks is left without any cards. 
  71.             //You have to print the winner on the console and the sum of the left cards:
  72.             //"{First/Second} player wins! Sum:{sum}".
  73.  
  74.  
  75.  
  76.         }
  77.     }
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment