Advertisement
CR7CR7

mm

Oct 21st, 2022
873
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 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 MemoryGame {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         List<String> elementList = Arrays.stream(scanner.nextLine().split("\\s+"))
  11.                 .collect(Collectors.toList());
  12.         String input = scanner.nextLine();
  13.         int count = 0;
  14.         while (!input.contains("end")) {
  15.             String[] inputArr = input.split(" ");
  16.             int firstIndex = Integer.parseInt(inputArr[0]);
  17.             int secondIndex = Integer.parseInt(inputArr[1]);
  18.  
  19.             boolean isNotValid = firstIndex == secondIndex || firstIndex < 0 || secondIndex < 0
  20.                     || firstIndex > elementList.size() - 1 || secondIndex > elementList.size() - 1;
  21.  
  22.             count++;
  23.  
  24.             if (isNotValid && elementList.size() > 0) {
  25.  
  26.                 String elementToAdd = "-" + count + "a";
  27.  
  28.                 elementList.add(elementList.size() / 2, elementToAdd);
  29.                 elementList.add(elementList.size() / 2, elementToAdd);
  30.  
  31.  
  32.                 System.out.println("Invalid input! Adding additional elements to the board");
  33.  
  34.             } else  if (elementList.size() == 0) {
  35.  
  36.  
  37.                 System.out.printf("You have won in %d turns!", count - 1);
  38.                 return;
  39.             } else {
  40.                 if (elementList.get(firstIndex).equals(elementList.get(secondIndex))) {
  41.                     String elementForRemove = elementList.get(firstIndex);
  42.                     elementList.remove(elementForRemove);
  43.                     elementList.remove(elementForRemove);
  44.                     System.out.printf("Congrats! You have found matching elements - %s!%n", elementForRemove);
  45.                 } else {
  46.                     System.out.println("Try again!");
  47.                 }
  48.             }
  49.  
  50.  
  51.             input = scanner.nextLine();
  52.         }
  53.  
  54.  
  55.         if (input.contains("end") && elementList.size() != 0) {
  56.             System.out.println("Sorry you lose :(");
  57.             //System.out.println(elementList.toString().replaceAll("[\\]\\[,]", ""));
  58.             System.out.println(String.join(" ",elementList));
  59.         }
  60.  
  61.     }
  62. }
Advertisement
Comments
  • onegeorge555
    2 years
    # text 3.20 KB | 0 0
    1. https://pastebin.com/rx7APZR1#igUx2TrV
    2. Hi Nairaland readerz,Julian Assange was a great "sport" type,
    3. not many people realize that Julian Assange did brilliant things.
    4. Did you know his personal Diarys auctioned for 250,000 dollars
    5. and then there was Pams clothes..oh yeah, and a Niffty NFT for
    6. a mere few million.if you like conspiracy theory sites checkout
    7. these one below great for yup coming sports!!
    8.  
    9. https://polishmexican.blogspot.com/2006/02/blogger-mess-up.html
    10. http://polishmexican.blogspot.com/2007/08/actual-polish-mexican-content.html
    11. https://shirazsocialist.wordpress.com/2017/12/21/mccluskey-jewish-news-interview/
    12. https://psychedelicdungeon.wordpress.com/2010/09/04/wikileaks-pt-2-reaction/
    13. https://rightsfreedoms.wordpress.com/2021/12/15/rfk-jrs-heroic-resistance-to-the-cias-continuing-covid-coup-detat/
    14. https://psychedelicdungeon.wordpress.com/2010/09/02/wikifreaks-pt-1-eat-pray-leak/
    15. https://www.independentsentinel.com/potential-whistleblower-named-but-not-proven/
    16. https://therapidesnote.blogspot.com/2011/04/anonymous-commenters-on-computers.html
    17. https://www.dialoginternational.com/dialog_international/2021/08/anti-vaxxers-in-germany-querdenker-anti-semites-neo-nazis.html
    18. https://www.cringely.com/2021/09/10/bobs-9-11-post-from-20-years-ago-to-a-man-with-a-hammer/
    19. https://ganglandireland.blogspot.com/2009/11/fat-freddie-thompson-riding-shotgun-for.html
    20. https://ganglandireland.blogspot.com/2009/11/gangs-of-dublin.html
    21. https://discyonki.blogspot.com/2011/11/man-is-in-love-waterboys.html
    22. https://www.afepi-ireland.com/member/smith-albert
    23. https://kaildganglife.blogspot.com/2011/03/prison-gangs-one-mexican-mafia-v.html
    24. https://tfiglobalnews.com/2021/11/22/northern-irelands-secession-from-the-united-kingdom-is-almost-a-surety-now/
    25. https://www.independentsentinel.com/potential-whistleblower-named-but-not-proven/
    26. http://www.darkpolitricks.com/2020/02/am-i-going-down-google-plug-of.html
    27. http://www.darkpolitricks.com/2022/05/here-comes-world-war-iii.html
    28. http://www.darkpolitricks.com/2021/03/is-biden-worse-than-trump.html
    29. http://www.darkpolitricks.com/2022/03/two-things-can-be-right-at-same-time.html
    30. http://www.darkpolitricks.com/2021/12/the-internet-is-dead-can-you-prove-it.html
    31. http://www.darkpolitricks.com/2021/09/the-uk-is-becoming-more-despotic-every.html
    32. http://www.darkpolitricks.com/2022/01/how-many-conspiracies-have-to-turn-true.html
    33. http://www.darkpolitricks.com/2022/03/the-stupidity-of-russian-sanctions.html
    34. http://www.darkpolitricks.com/2022/04/who-are-we-standing-with-when-we-stand.html
    35. http://www.darkpolitricks.com/2021/05/cia-agent-admits-why-usa-cant-have.html
    36. http://www.darkpolitricks.com/2018/07/israel-is-first-terrorist-state-with.html
    37. http://www.darkpolitricks.com/2020/07/covid19-were-loving-it.html
    38. http://www.darkpolitricks.com/2020/04/help-me-create-real-altnews-search.html
    39.  
    40. Thats quite alot of sites eh!? were sure youll find interesting
    41. stuff there to read their files the lot, a real Burn Baby Burn list.
    42. infact where sure youll need this naughty daughty.
    43. Hang Tight.
    44. Lee chung.
    45. Big HI to Poe and Scarebear!! hoo helped compile Santas Naughty List
    46.  
    47. HEY KAMAL TRUCKER YOU MUST THINK WE ARE REALLY STUPID OR SOMETHING.
    48.  
    49. AGENT TALLAGHT WATCHES YOU .....
    50.  
Add Comment
Please, Sign In to add comment
Advertisement