Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4. * Simulates a tower that can hold disks.
  5. * @author S. Camilleri
  6. * @author <Hasan Zafar>
  7. */
  8. public class Challenge {
  9. public static void main(String[] args) {
  10. Scanner input = new Scanner(System.in);
  11.  
  12. // This array holds the disks. A 0 represents no disk.
  13. int[] tower = {4,3,2,1};
  14. int[] tower2 = {0,0,0,0};
  15. int[] tower3 = {0,0,0,0};
  16.  
  17. // This index represents the first available empty spot for a disk.
  18. int index = 0;
  19.  
  20. int towerCounter = 0;
  21. int length = tower.length;
  22. int length2 = tower2.length;
  23. int length3 = tower3.length;
  24. int diskChoice = 1;
  25. int i;
  26. int held = 0;
  27. int placeChoice;
  28.  
  29. boolean playing = true;
  30. while (playing)
  31. {
  32. //Check if Won
  33. if (tower3[0] == 4 && tower3[1] == 3 && tower3[2] == 2 && tower[3] == 1) {
  34. System.out.println("Congratulations! You win!");
  35. playing = false;
  36. break;
  37.  
  38. }
  39.  
  40. /********************
  41. * Display the towers
  42. ********************/
  43. System.out.println();
  44. //Tower1
  45. System.out.print("{ ");
  46.  
  47. for (int x=0; x<length; x++)
  48. {
  49. System.out.print(tower[x]);
  50. }
  51.  
  52. System.out.println();
  53. //Tower2
  54. System.out.print("{ ");
  55. towerCounter = 0;
  56.  
  57. for (int x=0; x<length2; x++)
  58. {
  59. System.out.print(tower2[x]);
  60. }
  61.  
  62. System.out.println();
  63. //Tower3
  64. System.out.print("{ ");
  65. towerCounter = 0;
  66.  
  67. for (int x=0; x<length3; x++)
  68. {
  69. System.out.print(tower3[x]);
  70. }
  71.  
  72.  
  73.  
  74. /********************
  75. * Select the highest disk from the tower
  76. ********************/
  77.  
  78. System.out.println();
  79. System.out.println("Pick a tower (The disk highest on that tower will be chosen)");
  80. diskChoice = input.nextInt();
  81.  
  82. // If user uses the first tower
  83. if (diskChoice == 1) {
  84. i = 3;
  85. while (tower[i] == 0) {
  86. i--;
  87.  
  88. }
  89. held = tower[i];
  90. tower[i] = 0;
  91. } else if (diskChoice == 2) { // If user uses the second tower
  92. i = 3;
  93. while (tower2[i] == 0) {
  94. i--;
  95.  
  96. }
  97. held = tower2[i];
  98. tower2[i] = 0;
  99. } else if (diskChoice == 3) { // If user uses the third tower
  100. i = 3;
  101. while (tower3[i] == 0) {
  102. i--;
  103.  
  104. }
  105. held = tower3[i];
  106. tower3[i] = 0;
  107. }
  108.  
  109. /********************
  110. * Place the disk
  111. ********************/
  112.  
  113. System.out.println("Where would you like to place" + " " + held + "?");
  114. placeChoice = input.nextInt();
  115.  
  116. if (placeChoice == 1) {
  117. i = 3;
  118. if (tower[3] == 0){
  119. while (tower[i] == 0) {
  120.  
  121. i--;
  122. if (i == 0) {
  123. break;
  124. }
  125.  
  126. }
  127. }
  128.  
  129. if (tower[i] == 0) {
  130. tower[i] = held;
  131. } else if (tower[i] != 0) {
  132. tower[i+1] = held;
  133. }
  134. } else if (placeChoice == 2) {
  135. i = 3;
  136.  
  137. if (tower2[3] == 0){
  138. while (tower2[i] == 0) {
  139.  
  140. i--;
  141. if (i == 0) {
  142. break;
  143. }
  144.  
  145. }
  146. }
  147. if (tower2[i] == 0) {
  148. tower2[i] = held;
  149. } else if (tower2[i] != 0) {
  150. tower2[i+1] = held;
  151. }
  152. } else if (placeChoice == 3) {
  153. i = 3;
  154. if (tower3[3] == 0){
  155. while (tower3[i] == 0) {
  156.  
  157. i--;
  158. if (i == 0) {
  159. break;
  160. }
  161.  
  162. }
  163. }
  164. if (tower3[i] == 0) {
  165. tower3[i] = held;
  166. } else if (tower3[i] != 0) {
  167. tower3[i+1] = held;
  168. }
  169. }
  170.  
  171.  
  172.  
  173. }
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement