You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Pigeon.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package com.rthoni.uqac.pigeon;
  2. import java.util.List;
  3. import java.util.Random;
  4. /**
  5. * Created by ZHAJOR on 30/10/2016.
  6. */
  7. public class Pigeon implements Runnable{
  8. private int x;
  9. private int y;
  10. private String name;
  11. private Thread t;
  12. private final Game game;
  13. private static final int speed = 10;
  14. /**
  15. * Create a new pigeon with a random position
  16. * @param name The pigeon name
  17. * @param g The pigeon world
  18. */
  19. public Pigeon(String name, Game g) {
  20. Random r = new Random();
  21. x = r.nextInt(700);
  22. y = r.nextInt(700);
  23. this.name = name;
  24. this.game = g;
  25. }
  26. /**
  27. * The piegon behavior during his life
  28. */
  29. @Override
  30. public void run() {
  31. while (true) {
  32. synchronized (game) {
  33. // Get the food near the pigeon
  34. List<Food> foods = game.getFoods();
  35. Food best = new Food();
  36. int distance = -1;
  37. for (Food food : foods) {
  38. if (distance == -1) {
  39. distance = computeDistance(food.getX(), food.getY());
  40. best = food;
  41. } else if (distance > computeDistance(food.getX(), food.getY())) {
  42. distance = computeDistance(food.getX(), food.getY());
  43. best = food;
  44. }
  45. }
  46. // Eat or move if there is a food
  47. if (distance != -1 && distance <= Pigeon.speed) {
  48. this.x = best.getX();
  49. this.y = best.getY();
  50. foods.remove(best);
  51. game.setFoods(foods);
  52. System.out.print("Eat by " + this.name + "\n");
  53. }
  54. else if (distance != -1){
  55. this.computeNewPosition(best.getX(), best.getY());
  56. }
  57. // Random move
  58. else {
  59. Random r = new Random();
  60. if (r.nextInt(100) > 90 + r.nextInt(8)) {
  61. int moveX = r.nextInt(100) -50;
  62. int moveY = r.nextInt(100) -50;
  63. this.x += moveX;
  64. this.y += moveY;
  65. if (this.x > game.getWidth())
  66. this.x = game.getWidth() - r.nextInt(30);
  67. else if (this.x < 0)
  68. this.x = r.nextInt(30);
  69. if (this.y > game.getHeight())
  70. this.y = game.getHeight() - r.nextInt(30);
  71. else if (this.y < 0)
  72. this.y = r.nextInt(30);
  73. }
  74. }
  75. }
  76. try {
  77. Thread.sleep(100);
  78. } catch (InterruptedException e) {
  79. e.printStackTrace();
  80. }
  81. }
  82. }
  83. /**
  84. * Compute the distance between the pigeon and the food
  85. * @param x the x position of the food
  86. * @param y the y position of the food
  87. * @return the distance
  88. */
  89. private int computeDistance(int x, int y) {
  90. int diffx = this.x > x ? this.x - x : x - this.x;
  91. int diffy = this.y > y ? this.y - y : y - this.y;
  92. return diffx + diffy;
  93. }
  94. /**
  95. * Move the pigeon
  96. * @param x the x position of the food
  97. * @param y the y position of the food
  98. */
  99. private void computeNewPosition(int x, int y) {
  100. int moves = Pigeon.speed;
  101. while (moves > 0 ) {
  102. if (this.x > x) {
  103. this.x--;
  104. moves--;
  105. }
  106. if (this.x < x) {
  107. this.x++;
  108. moves--;
  109. }
  110. if (this.y < y) {
  111. this.y++;
  112. moves--;
  113. }
  114. if (this.y > y) {
  115. this.y--;
  116. moves--;
  117. }
  118. }
  119. }
  120. /**
  121. * Start the thread
  122. */
  123. public void start () {
  124. System.out.println("New pigeon alive : " + name );
  125. if (t == null) {
  126. t = new Thread (this, name);
  127. t.start ();
  128. }
  129. }
  130. public int getX() {
  131. return x;
  132. }
  133. public int getY() {
  134. return y;
  135. }
  136. }