Advertisement
Guest User

CardsGame

a guest
Feb 19th, 2019
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. public class cardGame {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         List<Integer> firstCards = Arrays.stream(scanner.nextLine().split(" "))
  10.                 .map(Integer::parseInt)
  11.                 .collect(Collectors.toList());
  12.         List<Integer> secondCards = Arrays.stream(scanner.nextLine().split(" "))
  13.                 .map(Integer::parseInt)
  14.                 .collect(Collectors.toList());
  15.         int sum = 0;
  16.  
  17.         while (!(firstCards.isEmpty() || secondCards.isEmpty())) {
  18.             if (firstCards.get(0) == secondCards.get(0)) {
  19.                 firstCards.remove(0);
  20.                 secondCards.remove(0);
  21.             } else if (firstCards.get(0) > secondCards.get(0)) {
  22.                 firstCards.add(firstCards.get(0));
  23.                 firstCards.add(secondCards.get(0));
  24.                 firstCards.remove(0);
  25.                 secondCards.remove(0);
  26.             } else if (secondCards.get(0) > firstCards.get(0)) {
  27.                 secondCards.add(secondCards.get(0));
  28.                 secondCards.add(firstCards.get(0));
  29.                 secondCards.remove(0);
  30.                 firstCards.remove(0);
  31.             }
  32.         }
  33.         if (firstCards.isEmpty()) {
  34.             for (int i = 0; i < secondCards.size(); i++) {
  35.                 sum += secondCards.get(i);
  36.             }
  37.             System.out.printf("Second player wins! Sum: %d", sum);
  38.         } else {
  39.             for (int i = 0; i < firstCards.size(); i++) {
  40.                 sum += firstCards.get(i);
  41.             }
  42.             System.out.printf("First player wins! Sum: %d", sum);
  43.  
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement