Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. import java.util.LinkedList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class Exam02SeashellTreasure {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. int n = Integer.parseInt(scanner.nextLine());
  9. String[][] beach = new String[n][];
  10. for (int i = 0; i < beach.length; i++) {
  11. beach[i] = scanner.nextLine().split("\\s+");
  12. }
  13. List<String> collectedShells = new LinkedList<>();
  14. int stolenShells = 0;
  15. String input = scanner.nextLine();
  16. while (!"Sunset".equals(input)){
  17. String[] tokens = input.split("\\s+");
  18. String command = tokens[0];
  19. int row = Integer.parseInt(tokens[1]);
  20. int col = Integer.parseInt(tokens[2]);
  21. switch (command){
  22. case "Collect":
  23. if (isValidIndex(row, col, beach)){
  24. String shell = beach[row][col];
  25. if (!shell.equals("-")){
  26. collectedShells.add(shell);
  27. beach[row][col] = "-";
  28. }
  29. }
  30. break;
  31. case "Steal":
  32. String direction = tokens[3];
  33. if (isValidIndex(row, col, beach)){
  34. String shell = beach[row][col];
  35. if (!shell.equals("-")){
  36. stolenShells++;
  37. beach[row][col] = "-";
  38. }
  39. stolenShells += stealing(beach,row,col, direction);
  40. }
  41. break;
  42. }
  43.  
  44. input = scanner.nextLine();
  45. }
  46.  
  47. printMatrix(beach);
  48. System.out.print(String.format("Collected seashells: %d", collectedShells.size()));
  49. if (!collectedShells.isEmpty()){
  50. System.out.print(" -> ");
  51. System.out.println(String.join(", ", collectedShells));
  52. }else {
  53. System.out.println();
  54. }
  55. System.out.println(String.format("Stolen seashells: %d", stolenShells));
  56. }
  57.  
  58. private static int stealing(String[][] beach, int row, int col, String direction) {
  59. int counter = 0;
  60. switch (direction){
  61. case "up":
  62. for (int i = row; i >= row - 3; i--) {
  63. counter = getCounter(beach, i, counter, col);
  64. }
  65. break;
  66. case "down":
  67. for (int i = row; i <= row + 3; i++) {
  68. counter = getCounter(beach, i, counter, col);
  69. }
  70. break;
  71. case "left":
  72. for (int i = col; i >= col -3 ; i--) {
  73. counter = getCounter(beach, row, counter, i);
  74. }
  75. break;
  76. case "right":
  77. for (int i = col; i <= col +3 ; i++) {
  78. counter = getCounter(beach, row, counter, i);
  79. }
  80. break;
  81. }
  82. return counter;
  83. }
  84.  
  85. private static int getCounter(String[][] beach, int row, int counter, int i) {
  86. if (isValidIndex(row, i, beach)) {
  87. String shell = beach[row][i];
  88. if (!shell.equals("-")) {
  89. counter++;
  90. beach[row][i] = "-";
  91. }
  92. }
  93. return counter;
  94. }
  95.  
  96. private static boolean isValidIndex(int row, int col, String[][] beach) {
  97. if (row >= 0 && row < beach.length){
  98. if (col >= 0 && col < beach[row].length){
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104.  
  105. private static void printMatrix(String[][] beach) {
  106. for (String[] row : beach) {
  107. for (String col : row) {
  108. System.out.print(col + " ");
  109. }
  110. System.out.println();
  111. }
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement