123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package com.rthoni.uqac.pigeon;
-
- import java.util.List;
- import java.util.Random;
-
- /**
- * Created by ZHAJOR on 30/10/2016.
- */
- public class Pigeon implements Runnable{
- private int x;
- private int y;
- private String name;
- private Thread t;
- private final Game game;
- private static final int speed = 10;
-
- /**
- * Create a new pigeon with a random position
- * @param name The pigeon name
- * @param g The pigeon world
- */
- public Pigeon(String name, Game g) {
- Random r = new Random();
- x = r.nextInt(700);
- y = r.nextInt(700);
- this.name = name;
- this.game = g;
- }
-
- /**
- * The piegon behavior during his life
- */
- @Override
- public void run() {
- while (true) {
- synchronized (game) {
- // Get the food near the pigeon
- List<Food> foods = game.getFoods();
- Food best = new Food();
- int distance = -1;
- for (Food food : foods) {
- if (distance == -1) {
- distance = computeDistance(food.getX(), food.getY());
- best = food;
- } else if (distance > computeDistance(food.getX(), food.getY())) {
- distance = computeDistance(food.getX(), food.getY());
-
- best = food;
- }
- }
- // Eat or move if there is a food
- if (distance != -1 && distance <= Pigeon.speed) {
- this.x = best.getX();
- this.y = best.getY();
- game.removeFood(best);
- System.out.print("Eat by " + this.name + "\n");
- }
- else if (distance != -1){
- this.computeNewPosition(best.getX(), best.getY());
- }
- // Random move
- else {
- Random r = new Random();
- if (r.nextInt(100) > 90 + r.nextInt(8)) {
- int moveX = r.nextInt(100) -50;
- int moveY = r.nextInt(100) -50;
- this.x += moveX;
- this.y += moveY;
- if (this.x > game.getWidth())
- this.x = game.getWidth() - r.nextInt(30);
- else if (this.x < 0)
- this.x = r.nextInt(30);
- if (this.y > game.getHeight())
- this.y = game.getHeight() - r.nextInt(30);
- else if (this.y < 0)
- this.y = r.nextInt(30);
- }
- }
- }
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
-
- /**
- * Compute the distance between the pigeon and the food
- * @param x the x position of the food
- * @param y the y position of the food
- * @return the distance
- */
- private int computeDistance(int x, int y) {
- int diffx = this.x > x ? this.x - x : x - this.x;
- int diffy = this.y > y ? this.y - y : y - this.y;
- return diffx + diffy;
- }
-
- /**
- * Move the pigeon
- * @param x the x position of the food
- * @param y the y position of the food
- */
- private void computeNewPosition(int x, int y) {
- int moves = Pigeon.speed;
- while (moves > 0 ) {
- if (this.x > x) {
- this.x--;
- moves--;
- }
- if (this.x < x) {
- this.x++;
- moves--;
- }
- if (this.y < y) {
- this.y++;
- moves--;
- }
- if (this.y > y) {
- this.y--;
- moves--;
- }
- }
- }
-
- /**
- * Start the thread
- */
- public void start () {
- System.out.println("New pigeon alive : " + name );
- if (t == null) {
- t = new Thread (this, name);
- t.start ();
- }
- }
-
- public int getX() {
- return x;
- }
-
- public int getY() {
- return y;
- }
- }
|