Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Pigeon.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. public Pigeon(String name, Game g) {
  15. Random r = new Random();
  16. x = r.nextInt(700);
  17. y = r.nextInt(700);
  18. this.name = name;
  19. this.game = g;
  20. }
  21. @Override
  22. public void run() {
  23. while (true) {
  24. synchronized (game) {
  25. List<Food> foods = game.getFoods();
  26. Food best = new Food();
  27. int bestFoodX = 0, bestFoodY = 0, distance = -1, bestIndex = -1, index = 0;
  28. for (Food food : foods) {
  29. if (distance == -1) {
  30. distance = computeDistance(food.getX(), food.getY());
  31. bestFoodX = food.getX();
  32. bestFoodY = food.getY();
  33. bestIndex = index;
  34. best = food;
  35. } else if (distance > computeDistance(food.getX(), food.getY())) {
  36. distance = computeDistance(food.getX(), food.getY());
  37. bestFoodX = food.getX();
  38. bestFoodY = food.getY();
  39. bestIndex = index;
  40. best = food;
  41. }
  42. index++;
  43. }
  44. if (distance != -1 && distance <= Pigeon.speed) {
  45. this.x = best.getX();
  46. this.y = best.getY();
  47. foods.remove(best);
  48. game.setFoods(foods);
  49. System.out.print("Eat by " + this.name + "\n");
  50. }
  51. else if (distance != -1){
  52. this.computeNewPosition(best.getX(), best.getY());
  53. }
  54. }
  55. // Random r = new Random();
  56. // int moveX = r.nextInt(10);
  57. // int moveY = r.nextInt(10);
  58. // this.x += moveX;
  59. // this.y += moveY;
  60. try {
  61. Thread.sleep(100);
  62. } catch (InterruptedException e) {
  63. e.printStackTrace();
  64. }
  65. }
  66. }
  67. private int computeDistance(int x, int y) {
  68. int diffx = this.x > x ? this.x - x : x - this.x;
  69. int diffy = this.y > y ? this.y - y : y - this.y;
  70. return diffx + diffy;
  71. }
  72. private void computeNewPosition(int x, int y) {
  73. int moves = Pigeon.speed;
  74. while (moves > 0 ) {
  75. if (this.x > x) {
  76. this.x--;
  77. moves--;
  78. }
  79. if (this.x < x) {
  80. this.x++;
  81. moves--;
  82. }
  83. if (this.y < y) {
  84. this.y++;
  85. moves--;
  86. }
  87. if (this.y > y) {
  88. this.y--;
  89. moves--;
  90. }
  91. }
  92. }
  93. public void start () {
  94. System.out.println("Starting " + name );
  95. if (t == null) {
  96. t = new Thread (this, name);
  97. t.start ();
  98. }
  99. }
  100. public int getX() {
  101. return x;
  102. }
  103. public int getY() {
  104. return y;
  105. }
  106. }