Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class NimAdvisorAdvanced{
  3. public static int times;
  4. public static void main (String[]args){
  5. Scanner nim = new Scanner(System.in);
  6. int matches = 1;
  7. System.out.println("Welcome to the advanced version of Nim Advisor.");
  8. sleep(1600);
  9. System.out.println("Let me take some information about the specific game you are playing.");
  10. sleep(2000);
  11. System.out.println("First, tell me what is the maximum number of matches\na player can take every turn.");
  12. int possTake;
  13. while(true){
  14. possTake = Integer.parseInt(nim.nextLine());
  15. sleep(300);
  16. if(possTake<=1){
  17. System.out.println("Invalid input.");
  18. }
  19. else{
  20. break;
  21. }
  22. }
  23. System.out.println("Now, tell me, the one who takes the last match...");
  24. sleep(1000);
  25. System.out.println("Wins / Loses");
  26. String winCond;
  27. // =========================================================================================================================
  28. // Who takes the last match wins.
  29. // =========================================================================================================================
  30. while (true){
  31. winCond = nim.nextLine();
  32. sleep(300);
  33. if (winCond.toLowerCase().equals("wins")){
  34. while(true){
  35. System.out.println("Tell me how many matches are left when your turn comes.");
  36. matches = nim.nextInt();
  37. sleep(300);
  38. if(matches<0){
  39. System.out.println("Invalid input, again,");
  40. sleep(1000);
  41. }
  42. else if(matches==0){
  43. System.out.println("In that case you lost. Goodbye.");
  44. return;
  45. }
  46. else if (matches==1){
  47. System.out.println("You can only take 1 match, therefore you won. Goodbye.");
  48. return;
  49. }
  50. else{
  51. int take = matches%(possTake+1);
  52. switch (take){
  53. case 0: System.out.println("you are currently in a losing situation, unless your opponent fails,\nyou will lose. Take between 1-"+possTake+" matches.");
  54. sleep(3200);
  55. break;
  56. case 1: System.out.println("Take " + take + " match");
  57. sleep(1000);
  58. break;
  59. default: System.out.println("Take " + take + " matches");
  60. sleep(1000);
  61. break;
  62. }
  63. }
  64. }
  65.  
  66. }
  67. // =========================================================================================================================
  68. // Who takes the last match loses.
  69. // =========================================================================================================================
  70. else if (winCond.toLowerCase().equals("loses")){
  71. while(true){
  72. System.out.println("Tell me how many matches are left when your turn comes.");
  73. matches = nim.nextInt();
  74. sleep(300);
  75. if(matches<0){
  76. System.out.println("Invalid input, again,");
  77. sleep(1000);
  78. }
  79. else if(matches==0){
  80. System.out.println("In that case you won. Goodbye.");
  81. return;
  82. }
  83. else if (matches==1){
  84. System.out.println("You can only take 1 match, therefore you lost. Goodbye.");
  85. return;
  86. }
  87. else{
  88. int p = matches/(possTake+1);
  89. if (matches%(possTake+1)==0){
  90. p = p-1;
  91. }
  92. int should = (possTake+1)*p + 1;
  93. int take = matches-should;
  94. switch (take){
  95. case 0: System.out.println("you are currently in a losing situation, unless your opponent fails,\nyou will lose. Take between 1-"+possTake+" matches.");
  96. sleep(3200);
  97. break;
  98. case 1: System.out.println("Take " + take + " match");
  99. sleep(1000);
  100. break;
  101. default: System.out.println("Take " + take + " matches");
  102. sleep(1000);
  103. break;
  104. }
  105. }
  106. }
  107. }
  108. else{
  109. System.out.println("Invalid input.");
  110. }
  111. }
  112. }
  113. public static void sleep (int times){
  114. try {
  115. Thread.sleep(times);
  116. }
  117. catch(InterruptedException e){
  118. System.out.println("got interrupted!");
  119. }
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement