Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. package g54368.humbug.model;
  2.  
  3. import g54368.humbug.model.*;
  4. /**
  5. *
  6. * @author Akwatic
  7. */
  8. public class Spider extends Animal {
  9.  
  10. // spider algo
  11. public Position move(Board board, Direction direction, Animal ... animals)
  12. {
  13. // get the current position
  14. Position position = getPositionOnBoard();
  15.  
  16. while (true)
  17. {
  18. // get a ref sur our target position
  19. Position targetPosition = position.next(direction);
  20. Square square = null;
  21.  
  22. // try getting the square at that position
  23. try
  24. {
  25. square = board.getSquareAtPosition(targetPosition);
  26. }
  27. // oob, we are out of the board
  28. catch (ArrayIndexOutOfBoundsException ex)
  29. {
  30. return null;
  31. }
  32.  
  33. catch (IllegalArgumentException ex)
  34. {
  35. return null;
  36. }
  37.  
  38. // square is null (= vide), we die
  39. if (square == null)
  40. {
  41. setPositionOnBoard(null);
  42.  
  43. return null;
  44. }
  45. // check if the square is occupied by another animal
  46. else
  47. {
  48. for(Animal animal : animals)
  49. {
  50. // if so, stay where we are
  51. if(animal.getPositionOnBoard().equals(targetPosition))
  52. { System.out.print(" test 1 ");
  53. System.out.print(" test 2 ");
  54. if (square.getType() == SquareType.STAR)
  55. {
  56. System.out.print( "square type star 1 ");
  57. System.out.print(" square type star 2 ");
  58. square.setType(SquareType.GRASS);
  59. setOnStar();
  60. setPositionOnBoard(null);
  61.  
  62. return position;
  63. }
  64. return position;
  65. }
  66. }
  67.  
  68. // update our pos to the target position and we loop again
  69. position = targetPosition;
  70. setPositionOnBoard(position);
  71. }
  72. }
  73. }
  74.  
  75. public Spider(Position position) {
  76. super(position);
  77.  
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement