CCB Premier Import
This commit is contained in:
75
bug_game/src/Bug.java
Normal file
75
bug_game/src/Bug.java
Normal file
@@ -0,0 +1,75 @@
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
|
||||
public class Bug {
|
||||
private int pos_x;
|
||||
private int pos_y;
|
||||
private String orientation;
|
||||
|
||||
public Bug(int i, int j) {
|
||||
pos_x = i; pos_y = j; orientation = "right";
|
||||
}
|
||||
|
||||
public void deplacer() {
|
||||
if (orientation == "right") {
|
||||
pos_y++;
|
||||
}
|
||||
else if (orientation == "left") {
|
||||
pos_y--;
|
||||
}
|
||||
else if (orientation == "top") {
|
||||
pos_x--;
|
||||
}
|
||||
else if (orientation == "bot") {
|
||||
pos_x++;
|
||||
}
|
||||
}
|
||||
|
||||
public String afficher() {
|
||||
return orientation;
|
||||
}
|
||||
|
||||
public boolean rotation(int taillePlateau) {
|
||||
Random r = new Random();
|
||||
int alea = r.nextInt(4);
|
||||
boolean correctRotation = true;
|
||||
|
||||
switch(alea){
|
||||
case 0:
|
||||
if(pos_y + 1 > taillePlateau){
|
||||
correctRotation = false;
|
||||
break;
|
||||
}
|
||||
orientation = "right";
|
||||
break;
|
||||
case 1:
|
||||
if(pos_y - 1 < 0){
|
||||
correctRotation = false;
|
||||
break;
|
||||
}
|
||||
orientation = "left";
|
||||
break;
|
||||
case 2:
|
||||
if(pos_x - 1 < 0){
|
||||
correctRotation = false;
|
||||
break;
|
||||
}
|
||||
orientation = "top";
|
||||
break;
|
||||
case 3: default:
|
||||
if(pos_x + 1 > taillePlateau){
|
||||
correctRotation = false;
|
||||
break;
|
||||
}
|
||||
orientation = "bot";
|
||||
break;
|
||||
}
|
||||
|
||||
return correctRotation;
|
||||
}
|
||||
public int getX() {return pos_x;}
|
||||
public int getY() {return pos_y;}
|
||||
|
||||
|
||||
}
|
||||
18
bug_game/src/Friandise.java
Normal file
18
bug_game/src/Friandise.java
Normal file
@@ -0,0 +1,18 @@
|
||||
public class Friandise extends Gadget {
|
||||
private String nom;
|
||||
|
||||
public Friandise(int i, int j) {
|
||||
super(i, j);
|
||||
nom = "Friandise";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void utilisation(Bug g) {
|
||||
|
||||
}
|
||||
|
||||
public String afficher() {
|
||||
return nom;
|
||||
}
|
||||
|
||||
}
|
||||
18
bug_game/src/Gadget.java
Normal file
18
bug_game/src/Gadget.java
Normal file
@@ -0,0 +1,18 @@
|
||||
public abstract class Gadget {
|
||||
|
||||
/* Attribut */
|
||||
private int pos_x;
|
||||
private int pos_y;
|
||||
|
||||
/* Constructeur */
|
||||
protected Gadget(int i, int j) {
|
||||
pos_x = i;
|
||||
pos_y = j;
|
||||
}
|
||||
|
||||
public abstract void utilisation(Bug b);
|
||||
|
||||
public abstract String afficher();
|
||||
public int getX() {return pos_x;}
|
||||
public int getY() {return pos_y;}
|
||||
}
|
||||
137
bug_game/src/Game.java
Normal file
137
bug_game/src/Game.java
Normal file
@@ -0,0 +1,137 @@
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.Image;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
|
||||
public class Game extends JPanel{
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
public static int taille = 7;
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Labyrinthe lab = new Labyrinthe(taille - 1);
|
||||
JFrame fenetre = new JFrame();
|
||||
fenetre.setTitle("BUG GAME");
|
||||
fenetre.setSize(500,500);
|
||||
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
fenetre.setLocationRelativeTo(null);
|
||||
fenetre.setResizable(false);
|
||||
fenetre.setLayout(new BorderLayout());
|
||||
JPanel content = new JPanel();
|
||||
fenetre.setContentPane(content);
|
||||
content.setLayout(new GridLayout(taille, taille));//on définit la taille de la grille de 10 sur 10
|
||||
|
||||
JPanel[][] plat = afficherLabyrinthe(lab, content);
|
||||
fenetre.setVisible(true);
|
||||
|
||||
while(lab.getGadgets()[0].getX() != lab.getBugs()[0].getX() ||
|
||||
lab.getGadgets()[0].getY() != lab.getBugs()[0].getY()) {
|
||||
// Pour une execution pas à pas
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
deplacement(lab, plat, content);
|
||||
|
||||
}
|
||||
lab.getGadgets()[0].utilisation(lab.getBugs()[0]);
|
||||
plat[lab.getGadgets()[0].getX()][lab.getGadgets()[0].getY()].removeAll();
|
||||
plat[lab.getGadgets()[0].getX()][lab.getGadgets()[0].getY()].add(getImg(lab.getBugs()[0].afficher()));
|
||||
plat[lab.getGadgets()[0].getX()][lab.getGadgets()[0].getY()].revalidate();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
//creation des cases et des collones du jpanel
|
||||
public static JPanel[][] afficherLabyrinthe(Labyrinthe lab, JPanel content){
|
||||
JPanel[][] plateau = new JPanel[taille-1][taille-1];
|
||||
|
||||
int taille = lab.getTaille();
|
||||
// Creation du plateau
|
||||
Bug[] bugs = lab.getBugs();
|
||||
Gadget[] gadgets = lab.getGadgets();
|
||||
content.setBackground(Color.white);
|
||||
|
||||
for(int i=0; i<taille; i++){
|
||||
for(int j=0; j<taille; j++){
|
||||
plateau[i][j]= new JPanel();
|
||||
|
||||
//on definis la dimension du jpanel de 50,50
|
||||
plateau[i][j].setSize(new Dimension(500/taille, 500/taille));
|
||||
|
||||
plateau[i][j].setBackground(Color.white);
|
||||
plateau[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
|
||||
|
||||
content.add(plateau[i][j]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
for (int nbBug = 0; nbBug < lab.getNbBug(); nbBug++){
|
||||
plateau[bugs[nbBug].getX()][bugs[nbBug].getY()].add(getImg(bugs[nbBug].afficher()));
|
||||
}
|
||||
|
||||
for (int nbGadget = 0; nbGadget < lab.getNbGadget(); nbGadget++){
|
||||
plateau[gadgets[nbGadget].getX()][gadgets[nbGadget].getY()].add(getImg(gadgets[nbGadget].afficher()));
|
||||
}
|
||||
|
||||
return plateau;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void deplacement(Labyrinthe lab, JPanel[][] plateau, JPanel content){
|
||||
Bug[] bugs = lab.getBugs();
|
||||
|
||||
// On joue un tour pour tout les bugs
|
||||
for(int nbBug = 0; nbBug < lab.getNbBug(); nbBug++){
|
||||
int x = bugs[nbBug].getX(); int y = bugs[nbBug].getY();
|
||||
|
||||
plateau[x][y].removeAll();
|
||||
plateau[x][y].setSize(new Dimension(500/taille, 500/taille));
|
||||
plateau[x][y].setBackground(Color.white);
|
||||
|
||||
// Tant que la rotation n'est pas correct
|
||||
while(!bugs[nbBug].rotation(taille-2));
|
||||
|
||||
// Deplacement du bug
|
||||
bugs[nbBug].deplacer();
|
||||
x = bugs[nbBug].getX();y =bugs[nbBug].getY();
|
||||
|
||||
plateau[x][y].add(getImg(bugs[nbBug].afficher()));
|
||||
plateau[x][y].revalidate();
|
||||
}
|
||||
}
|
||||
|
||||
public static JLabel getImg(String nom) {
|
||||
if (nom == "right")
|
||||
return new JLabel(new ImageIcon(new ImageIcon("bug_right.jpeg").getImage().getScaledInstance(500/taille - 10, 500/taille - 10, Image.SCALE_DEFAULT)));
|
||||
else if (nom == "left")
|
||||
return new JLabel(new ImageIcon(new ImageIcon("bug_left.jpeg").getImage().getScaledInstance(500/taille - 10, 500/taille - 10, Image.SCALE_DEFAULT)));
|
||||
else if (nom == "top")
|
||||
return new JLabel(new ImageIcon(new ImageIcon("bug_top.jpeg").getImage().getScaledInstance(500/taille - 10, 500/taille - 10, Image.SCALE_DEFAULT)));
|
||||
else if (nom == "bot")
|
||||
return new JLabel(new ImageIcon(new ImageIcon("bug_bot.jpeg").getImage().getScaledInstance(500/taille - 10, 500/taille - 10, Image.SCALE_DEFAULT)));
|
||||
else
|
||||
return new JLabel(new ImageIcon(new ImageIcon("fraise.jpeg").getImage().getScaledInstance(500/taille - 10, 500/taille - 10, Image.SCALE_DEFAULT)));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
67
bug_game/src/Labyrinthe.java
Normal file
67
bug_game/src/Labyrinthe.java
Normal file
@@ -0,0 +1,67 @@
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
public class Labyrinthe {
|
||||
/* Attribut */
|
||||
private int taille;
|
||||
private Bug[] bugs;
|
||||
private int nbBug = 0;
|
||||
private Gadget[] gadgets;
|
||||
private int nbGadget = 0;
|
||||
|
||||
|
||||
// Constructeur
|
||||
public Labyrinthe(int t) {
|
||||
bugs = new Bug[t*t];
|
||||
gadgets = new Gadget[2];
|
||||
|
||||
taille = t;
|
||||
|
||||
|
||||
Random r = new Random();
|
||||
// Ajout du Bug
|
||||
int iAlea = r.nextInt(t);
|
||||
int jAlea = r.nextInt(t);
|
||||
bugs[0] = new Bug(iAlea,jAlea);
|
||||
nbBug++;
|
||||
|
||||
// Ajout du Bug
|
||||
iAlea = r.nextInt(t);
|
||||
jAlea = r.nextInt(t);
|
||||
gadgets[0] = new Friandise(iAlea,jAlea);
|
||||
nbGadget++;
|
||||
|
||||
}
|
||||
|
||||
public boolean addBug() {
|
||||
if (bugs.length == taille*taille) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Random r = new Random();
|
||||
int iAlea = r.nextInt(taille);
|
||||
int jAlea = r.nextInt(taille);
|
||||
bugs[nbBug - 1] = new Bug(iAlea,jAlea);
|
||||
nbBug++;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public int getTaille(){
|
||||
return taille;
|
||||
}
|
||||
public Bug[] getBugs(){
|
||||
return bugs;
|
||||
}
|
||||
public int getNbBug(){
|
||||
return nbBug;
|
||||
}
|
||||
public Gadget[] getGadgets(){
|
||||
return gadgets;
|
||||
}
|
||||
public int getNbGadget(){
|
||||
return nbGadget;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user