Teo_Yanchev

ListsExercise - 6.CardGame - C# Fundamentals

Sep 19th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _6.CardsGame
  6. {
  7. class Program
  8. {
  9. static void Main()
  10. {
  11. List<int> playerOne = Console.ReadLine()
  12. .Split()
  13. .Select(int.Parse)
  14. .ToList();
  15.  
  16. List<int> playerTwo = Console.ReadLine()
  17. .Split()
  18. .Select(int.Parse)
  19. .ToList();
  20. string winner = string.Empty;
  21.  
  22. while (playerOne.Count > 0 && playerTwo.Count > 0)
  23. {
  24. for (int i = 0; i < Math.Max(playerOne.Count, playerTwo.Count); i++)
  25. {
  26. int temp = 0;
  27. if (playerOne[i] > playerTwo[i])
  28. {
  29. temp = playerOne[i];
  30. playerOne.Add(temp);
  31. playerOne.Add(playerTwo[i]);
  32. playerTwo.RemoveAt(i);
  33. playerOne.RemoveAt(i);
  34. break;
  35. }
  36.  
  37. else if (playerTwo[i] > playerOne[i])
  38. {
  39. temp = playerTwo[i];
  40. playerTwo.Add(temp);
  41. playerTwo.Add(playerOne[i]);
  42. playerOne.RemoveAt(i);
  43. playerTwo.RemoveAt(i);
  44. break;
  45. }
  46.  
  47. else
  48. {
  49. playerOne.RemoveAt(i);
  50. playerTwo.RemoveAt(i);
  51. break;
  52. }
  53.  
  54. }
  55. }
  56.  
  57. if (playerOne.Count == 0)
  58. {
  59. winner = $"Second player wins! Sum: {playerTwo.Sum()}";
  60. }
  61. else
  62. {
  63. winner = $"First player wins! Sum: {playerOne.Sum()}";
  64. }
  65.  
  66. Console.WriteLine(winner);
  67. }
  68. }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment