Advertisement
Guest User

SeaShell

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