Advertisement
Guest User

Untitled

a guest
Oct 16th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Scanner;
  6.  
  7. public class test {
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10. Character[][] board = new Character[8][8];
  11. for (int i = 0; i <=7 ; i++) {
  12. String[] row = scanner.nextLine().split("\\|");
  13. for (int j = 0; j <=7 ; j++) {
  14. board[i][j]=row[j].toCharArray()[0];
  15. }
  16. }
  17. int countInvalid = 0;
  18. int boardOutOf = 0;
  19. List<Character> pieces = new ArrayList<>();
  20. int startCoordinates = Integer.parseInt(scanner.nextLine());
  21. while (true){
  22. String input = scanner.nextLine();
  23. if(input.equals("END")){
  24. System.out.printf("Pieces take: %s%n", pieces.toString().replaceAll("\\[|\\]", ""));
  25. System.out.println("Invalid moves: "+countInvalid);
  26. System.out.println("Board out moves: "+ boardOutOf);
  27. break;
  28. }
  29. int[] coordinates = Arrays.stream(input.split("->"))
  30. .map(String::trim)
  31. .mapToInt(Integer::parseInt)
  32. .toArray();
  33. if(!isValidMove(coordinates[0],coordinates[1])){
  34. countInvalid++;
  35. }else if(coordinates[1]%10>7||coordinates[1]%10>7){
  36. boardOutOf++;
  37. }else{
  38. Character toAdd = board[(coordinates[1]/10)][(coordinates[1]%10)];
  39. if(toAdd!=' '){
  40. pieces.add(toAdd);
  41. board[(coordinates[1]/10)][(coordinates[1]%10)] = ' ';
  42. }
  43. }
  44.  
  45. }
  46. }
  47.  
  48. private static boolean isValidMove(int start,int end) {
  49. int rowS = start/10;
  50. int colS= start%10;
  51. int rowF = end/10;
  52. int colF= end%10;
  53. if(colS-2==colF&&(rowS-1==rowF||rowS+1==rowF))
  54. return true;
  55. if(colS+2==colF&&(rowS-1==rowF||rowS+1==rowF))
  56. return true;
  57. if(rowS+2==rowF&&(colS-1==colF||colS+1==colF))
  58. return true;
  59. if (rowS - 2 == rowF) if (colS - 1 == colF || colS + 1 == colF) return true;
  60. return false;
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement