Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace CardGame
- {
- class Program
- {
- static void Main(string[] args)
- {
- //You start from the beginning of both hands.
- List<int> firstDeck = Console.ReadLine()
- .Split(" ", StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToList();
- List<int> secondDeck = Console.ReadLine()
- .Split(" ", StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToList();
- //Compare the cards from the first deck to the cards from the second deck.
- //The player, who has the bigger card, takes both cards and
- //puts them at the back of his hand ‐ the second
- //player’s card is last, and the first person’s card (the winning one)
- //is before it (second to last) and the player with
- //the smaller card must remove the card from his deck.
- while (true)
- {
- if (firstDeck.Count == 0 || secondDeck.Count == 0)
- {
- break;
- }
- if (firstDeck.First() > secondDeck.First())
- {
- firstDeck.Add(firstDeck[0]);
- firstDeck.Remove(firstDeck[0]);
- firstDeck.Add(secondDeck[0]);
- secondDeck.Remove(secondDeck[0]);
- }
- else if (firstDeck[0] < secondDeck[0])
- {
- secondDeck.Add(secondDeck[0]);
- secondDeck.Remove(secondDeck[0]);
- secondDeck.Add(firstDeck[0]);
- firstDeck.Remove(firstDeck[0]);
- }
- //If both players’ cards have the same values ‐ no one wins, and
- //the two cards must be removed from the decks.
- else if (firstDeck[0] == secondDeck[0])
- {
- firstDeck.Remove(firstDeck[0]);
- secondDeck.Remove(secondDeck[0]);
- }
- }
- if (firstDeck.Count == 0)
- {
- Console.WriteLine($"Second player wins! Sum: {secondDeck.Sum()}");
- }
- else
- {
- Console.WriteLine($"First player wins! Sum: {firstDeck.Sum()}");
- }
- //The game is over, when one of the decks is left without any cards.
- //You have to print the winner on the console and the sum of the left cards:
- //"{First/Second} player wins! Sum:{sum}".
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment