Browse Source

Random moves sometimes and doc

master
Pierre-Antoine 'ZHAJOR' Tible 8 years ago
parent
commit
e7ccb55d19

+ 13
- 12
src/com/rthoni/uqac/pigeon/Game.java View File

@@ -2,29 +2,28 @@ package com.rthoni.uqac.pigeon;
2 2
 
3 3
 import javax.swing.*;
4 4
 import javax.swing.Timer;
5
-import javax.swing.event.MouseInputListener;
6 5
 import java.awt.*;
7
-import java.awt.event.ActionEvent;
8
-import java.awt.event.ActionListener;
9
-import java.awt.event.MouseEvent;
10
-import java.awt.event.MouseListener;
6
+import java.awt.event.*;
11 7
 import java.util.*;
12 8
 import java.util.List;
13 9
 
14 10
 /**
15 11
  * Created by ZHAJOR on 30/10/2016.
16 12
  */
17
-public class Game extends JPanel implements MouseListener, ActionListener{
13
+public class Game extends JPanel implements MouseListener, ActionListener
14
+{
18 15
 
19
-    private int x, y;
20 16
     private List<Pigeon> pigeons;
21 17
     private List<Food> foods;
22 18
 
23
-    public Game(){
19
+    /**
20
+     * Initialize the game and create some pigeons
21
+     */
22
+    public Game(int nb){
24 23
         addMouseListener(this);
25 24
         this.pigeons = new ArrayList<Pigeon>();
26 25
         this.foods = new ArrayList<Food>();
27
-        for(int i = 0; i < 10; ++i){
26
+        for(int i = 0; i < nb; ++i){
28 27
             Pigeon p = new Pigeon(""+i, this);
29 28
             this.pigeons.add(p);
30 29
             p.start();
@@ -33,6 +32,10 @@ public class Game extends JPanel implements MouseListener, ActionListener{
33 32
         this.setDoubleBuffered(false);
34 33
     }
35 34
 
35
+    /**
36
+     * Paint the scene
37
+     * @param g The graphics
38
+     */
36 39
     @Override
37 40
     public void paint(Graphics g) {
38 41
         Graphics2D g2d = (Graphics2D) g;
@@ -41,7 +44,7 @@ public class Game extends JPanel implements MouseListener, ActionListener{
41 44
         for (Pigeon pigeon : this.pigeons) {
42 45
             g2d.fillOval(pigeon.getX(), pigeon.getY(), 30, 30);
43 46
         }
44
-        //g2d.setColor(Color.BLUE);
47
+        g2d.setColor(Color.BLUE);
45 48
         // Draw foods
46 49
         for (Food food : this.foods) {
47 50
             g2d.fillRect(food.getX(), food.getY(), 15, 15);
@@ -56,8 +59,6 @@ public class Game extends JPanel implements MouseListener, ActionListener{
56 59
     @Override
57 60
     public void mousePressed(MouseEvent e) {
58 61
         this.foods.add(new Food(e.getX(), e.getY()));
59
-        System.out.print(e.getX() + " ");
60
-        System.out.print(e.getY() + "\n");
61 62
     }
62 63
 
63 64
     @Override

+ 1
- 4
src/com/rthoni/uqac/pigeon/Main.java View File

@@ -16,11 +16,8 @@ public class Main {
16 16
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
17 17
         frame.setSize(800, 800);
18 18
         frame.setResizable(false);
19
-        //frame.pack();
20 19
         frame.setVisible(true);
21
-
22
-        Game g = new Game();
23
-        frame.add(g);
20
+        frame.add(new Game(10));
24 21
     }
25 22
 
26 23
     public static void main(String... argv)

+ 45
- 17
src/com/rthoni/uqac/pigeon/Pigeon.java View File

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

Loading…
Cancel
Save