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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. game.removeFood(best);
  51. System.out.print("Eat by " + this.name + "\n");
  52. }
  53. else if (distance != -1){
  54. this.computeNewPosition(best.getX(), best.getY());
  55. }
  56. // Random move
  57. else {
  58. Random r = new Random();
  59. if (r.nextInt(100) > 90 + r.nextInt(8)) {
  60. int moveX = r.nextInt(100) -50;
  61. int moveY = r.nextInt(100) -50;
  62. this.x += moveX;
  63. this.y += moveY;
  64. if (this.x > game.getWidth())
  65. this.x = game.getWidth() - r.nextInt(30);
  66. else if (this.x < 0)
  67. this.x = r.nextInt(30);
  68. if (this.y > game.getHeight())
  69. this.y = game.getHeight() - r.nextInt(30);
  70. else if (this.y < 0)
  71. this.y = r.nextInt(30);
  72. }
  73. }
  74. }
  75. try {
  76. Thread.sleep(100);
  77. } catch (InterruptedException e) {
  78. e.printStackTrace();
  79. }
  80. }
  81. }
  82. /**
  83. * Compute the distance between the pigeon and the food
  84. * @param x the x position of the food
  85. * @param y the y position of the food
  86. * @return the distance
  87. */
  88. private int computeDistance(int x, int y) {
  89. int diffx = this.x > x ? this.x - x : x - this.x;
  90. int diffy = this.y > y ? this.y - y : y - this.y;
  91. return diffx + diffy;
  92. }
  93. /**
  94. * Move the pigeon
  95. * @param x the x position of the food
  96. * @param y the y position of the food
  97. */
  98. private void computeNewPosition(int x, int y) {
  99. int moves = Pigeon.speed;
  100. while (moves > 0 ) {
  101. if (this.x > x) {
  102. this.x--;
  103. moves--;
  104. }
  105. if (this.x < x) {
  106. this.x++;
  107. moves--;
  108. }
  109. if (this.y < y) {
  110. this.y++;
  111. moves--;
  112. }
  113. if (this.y > y) {
  114. this.y--;
  115. moves--;
  116. }
  117. }
  118. }
  119. /**
  120. * Start the thread
  121. */
  122. public void start () {
  123. System.out.println("New pigeon alive : " + name );
  124. if (t == null) {
  125. t = new Thread (this, name);
  126. t.start ();
  127. }
  128. }
  129. public int getX() {
  130. return x;
  131. }
  132. public int getY() {
  133. return y;
  134. }
  135. }