Neocat18

Bowling Score

Jun 11th, 2021
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. package BowlingScore;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4.  
  5. public class BowlingGame {
  6.  
  7.  
  8. public static void main(String args[]){
  9. int k;
  10. int i = 1;
  11. int pinsRoll1 = 0;
  12. int pinsRoll2 = 0;
  13. int tempSpareRoll1 = 0;
  14. int tempSpareRoll2 = 0;
  15. boolean spareHappened = false;
  16. boolean strikeHappened1 = false;
  17. boolean strikeHappened2 = false;
  18. int finalScore = 0;
  19. int[] Score = new int[10];
  20. int[] Rolls = new int[21];
  21. int r = 0;
  22. int s = 0;
  23.  
  24. Scanner sc = new Scanner(System.in);
  25. for (k = 1; k < 3; k++) {
  26. if (k == 2) {
  27. System.out.println("Pins:" + Arrays.toString(Rolls));
  28. System.out.println("Score:" +Arrays.toString(Score));
  29. System.out.println("Frame " + i + " Roll 2");
  30. System.out.println("Input roll: ");
  31. pinsRoll2 = sc.nextInt();
  32. if (pinsRoll2 < 0 || pinsRoll2 > 10 || pinsRoll1 + pinsRoll2 > 10) {
  33. System.out.println("Invaild number.");
  34. k = 1;
  35. } else {
  36. Rolls[r] = pinsRoll2;
  37. if (pinsRoll1 + pinsRoll2 == 10) {
  38. System.out.println("SPARE!");
  39. tempSpareRoll1 = pinsRoll1;
  40. tempSpareRoll2 = pinsRoll2;
  41. spareHappened = true;
  42. } else if (strikeHappened1 = true) {
  43. finalScore += 10 + pinsRoll2 + pinsRoll1;
  44. strikeHappened1 = false;
  45. k = 0;
  46. Score[s] = finalScore;
  47. i += 1;
  48. r += 1;
  49. s += 1;
  50. } else {
  51. finalScore += pinsRoll2 + pinsRoll1;
  52. k = 0;
  53. Score[s] = finalScore;
  54. i += 1;
  55. r += 1;
  56. s += 1;
  57. }
  58. }
  59.  
  60. } else {
  61. System.out.println("Pins:" + Arrays.toString(Rolls));
  62. System.out.println("Score:" +Arrays.toString(Score));
  63. System.out.println("Frame " + i + " Roll 1");
  64. System.out.println("Input roll: ");
  65. pinsRoll1 = sc.nextInt();
  66. if (pinsRoll1 < 0 || pinsRoll1 > 10) {
  67. System.out.println("Invaild number.");
  68. k = 0;
  69. } else {
  70. Rolls[r] = pinsRoll1;
  71. } if (pinsRoll1 == 10) {
  72. System.out.println("STRIKE!");
  73. strikeHappened1 = true;
  74. k = 0;
  75. i += 1;
  76. Rolls[r] = pinsRoll1;
  77. r += 2;
  78. } else if (strikeHappened1 = true && pinsRoll1 == 10){
  79. System.out.println("STRIKE!");
  80. strikeHappened2 = true;
  81. k = 0;
  82. i += 1;
  83. Rolls[r] = pinsRoll1;
  84. } else if (strikeHappened1 = true && pinsRoll1 == 10) {
  85. if (strikeHappened2 = true) {
  86. finalScore += 30;
  87. Score[s] = finalScore;
  88. s += 1;
  89. strikeHappened2 = false;
  90. Rolls[r] = pinsRoll1;
  91. r += 2;
  92. }
  93. } else if (spareHappened = true) {
  94. finalScore += tempSpareRoll1 + tempSpareRoll2 + pinsRoll1;
  95. spareHappened = false;
  96. Rolls[r] = pinsRoll1;
  97. r += 1;
  98. Score[s] = finalScore;
  99. s += 1;
  100. } else {
  101. r += 1;
  102. }
  103.  
  104.  
  105.  
  106. }
  107. if (i == 11 && pinsRoll1 == 10) {
  108. k = 3;
  109. System.out.println("Two more rolls!");
  110. pinsRoll1 = sc.nextInt();
  111. System.out.println("Last ball!");
  112. pinsRoll2 = sc.nextInt();
  113. finalScore += pinsRoll2 + pinsRoll1 - 20;
  114.  
  115. } else if (i == 11 && pinsRoll1 + pinsRoll2 == 10) {
  116. k = 3;
  117. System.out.println("One more roll!");
  118. pinsRoll1 = sc.nextInt();
  119. finalScore += pinsRoll1;
  120. }
  121. }
  122. System.out.println("Final Score: " + finalScore);
  123. }
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment