Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab;
- import java.awt.*;
- import java.awt.geom.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.util.*;
- /**
- * Classe principale, initialisation du moele, et mise en place du controleur
- * et de la vue.
- */
- public class Laby {
- public static void main(String[] args) {
- // Initialisation du schema MVC.
- Model laby = new Model(10, 12);
- JFrame frame = new JFrame();
- Controller controller = new Controller(laby, frame);
- View view = new View(laby, frame);
- // -- Schema MVC initialie.
- // Configuration de la fenetre graphique.
- frame.setTitle("Laby");
- frame.pack();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- // -- Fenetre graphique configuree.
- // Configuration du labyrinthe.
- //Premiere section de configuration.
- for (int i=0; i<10; i++) {
- laby.putWall(i, 0);
- laby.putWall(i, 11);
- }
- for (int j=0; j<12; j++) {
- laby.putWall(0, j);
- laby.putWall(9, j);
- }
- for (int i=0; i<7; i++) {
- laby.putWall(i, 4);
- }
- for (int i=8; i<10; i++) {
- laby.putWall(i, 4);
- }
- for (int j=4; j<7; j++) {
- laby.putWall(3, j);
- }
- for (int j=8; j<12; j++) {
- laby.putWall(3, j);
- }
- laby.putExit(0, 1);
- // // -- Fin de la premere section.
- // // Deuxieme section de configuration.
- laby.putHero(1,6);
- // // -- Fin de la deuxieme section.
- // // Troisieme section de configuration.
- laby.putMonster(6, 8);
- laby.putMonster(4, 2);
- // // -- Fin de la troisieme section.
- // // Quatrieme section de configuration.
- laby.putOpenDoor(3, 7);
- laby.putClosedDoor(7, 4);
- // // -- Fin de la quatrieme section.
- // -- Labyrinthe configure.
- }
- }
- /**
- * Le labyrinthe proprement dit.
- */
- class Model extends Observable {
- // Un labyrinthe a : une hauteur, une largeur, un tableau de cellules,
- // un heros et une liste de monstres.
- private final int h, w;
- private Cell[][] laby;
- private Hero hero = null;
- private ArrayList<Monster>monsters ;
- public Cell get(int i, int j) { return laby[i][j]; }
- public int getH() { return h; }
- public int getW() { return w; }
- // Construction d'un labyrinthe aux dimensions donnees.
- // A l'origine, il n'y a ni heros ni monstre, et toutes les cases
- // sont libres.
- public Model(int h, int w) {
- this.h = h;
- this.w = w;
- laby = new Cell[h][w];
- for (int i=0; i< h ;i++){
- for (int j=0; j< w ;j++){
- putCell(i,j);
- }
- }
- this.monsters = new ArrayList<Monster>();
- }
- // Methode pour les deplacements du monstre.
- // Deplacement d'une case dans une direction, puis notification de la vue.
- public void monsterMove() throws NotTraversableCell {
- for(Monster m : this.monsters){
- m.move(Direction.random());
- }
- }
- // Methode pour les deplacements du heros.
- // Deplacement d'une case dans une direction, puis notification de la vue.
- public void heroMove(Direction dir) throws NotTraversableCell {
- hero.move(dir);
- setChanged();
- notifyObservers();
- }
- // Methodes pour la configuration du labyrinthe.
- public void putCell(int i, int j) {
- laby[i][j] = new Cell(this, i, j);
- }
- public void putWall(int i, int j) {
- laby[i][j] = new Wall(this, i, j);
- }
- public void putExit(int i, int j) {
- laby[i][j] = new Exit(this, i, j);
- }
- public void putHero(int i, int j) {
- if (this.hero == null) {
- hero = new Hero(laby[i][j]);
- }
- }
- public void putMonster(int i, int j) {
- monsters.add(new Monster(laby[i][j]));
- }
- public void putOpenDoor(int i, int j) {
- laby[i][j] = Door.openDoorFactory(this, i, j);
- }
- public void putClosedDoor(int i, int j) {
- laby[i][j] = Door.closedDoorFactory(this, i, j);
- }
- // -- Fin des methodes de configuration.
- }
- /**
- * La vue principale du labyrinthe, qui affiche l'ensemble de la structure
- * et ses occupants.
- */
- class View extends JComponent implements Observer {
- // La vue memorise une rerence au labyrinthe et a la fenetre graphique.
- // On enregistre aussi la dimension et le cete de chaque case en pixels.
- private static final int SCALE = 40;
- private Model laby;
- private JFrame frame;
- private Dimension dim;
- // Constructeur, a la vue s'enregistre comme un deplacment de la fenetre
- // graphique et comme un observateur des modifications du labyrinthe.
- public View(Model laby, JFrame frame) {
- this.laby = laby;
- this.frame = frame;
- this.dim = new Dimension(SCALE*laby.getW(),SCALE*laby.getH());
- this.setPreferredSize(dim);
- laby.addObserver(this);
- frame.add(this);
- }
- // Methode de mise a jour pour reagir aux modification du modele.
- public void update(Observable o, Object arg) {
- repaint();
- }
- // Methode d'affichage du labyrinthe.
- public void paintComponent(Graphics g) {
- Graphics2D g2 = (Graphics2D)g;
- for (int i=0; i<this.laby.getH(); i++){
- for (int j=0; j<this.laby.getW(); j++){
- this.laby.get(i, j).paintCell(g2,j*SCALE,i*SCALE, SCALE);
- }
- }
- }
- }
- /**
- * Le controleur des entrees du clavier. Il reagit aussi a la souris pour
- * recuperer le focus.
- */
- class Controller extends JComponent implements KeyListener, MouseListener {
- // Le controleur garde une reference au labyrinthe.
- private Model laby;
- // Constructeur : le controleur s'enregistre comme un recepteur des entees
- // clavier et souris, et comme un element graphique (necessaire pour
- // recuperer le focus et les entrees).
- public Controller(Model laby, JFrame frame) {
- this.laby = laby;
- addKeyListener(this);
- addMouseListener(this);
- setFocusable(true);
- frame.add(this);
- }
- // Methode qui recupere l'entree clavier et appelle l'action correspondante
- // du heros. Si l'action du heros est valide, alors les monstres sont aussi
- // deplaces.
- public void keyTyped(KeyEvent e) {
- Direction dir;
- try{
- switch (e.getKeyChar()){
- case 'z' : dir = Direction.NORTH; break;
- case 'q' : dir = Direction.WEST; break;
- case 's' : dir = Direction.SOUTH; break;
- case 'd' : dir = Direction.EAST; break;
- default: throw new NotADirectionException();
- }
- laby.heroMove(dir);
- laby.monsterMove();
- }
- catch (NotADirectionException e2){}
- catch (ArrayIndexOutOfBoundsException e2){
- System.out.println("gagné!");
- }
- catch (NotTraversableCell e2){}
- }
- // -- Fin de l'action du clavier.
- // Recupere le focus quand on clique dans la fenetre graphique.
- public void mouseClicked(MouseEvent e) {
- requestFocusInWindow();
- }
- // Pas de reaction aux autres stimuli.
- public void keyPressed(KeyEvent e) {}
- public void keyReleased(KeyEvent e) {}
- public void mouseEntered(MouseEvent e) {}
- public void mouseExited(MouseEvent e) {}
- public void mousePressed(MouseEvent e) {}
- public void mouseReleased(MouseEvent e) {}
- }
- class NotADirectionException extends Exception{
- }
- /**
- *a partir d'ici : les classes auxiliaires. Themes couverts dans l'ordre :
- * Les directions.
- * Les cases.
- * Les portes.
- * Les creatures.
- */
- /**
- * Directions cardinales, et equivalents en differences de coordonnees.
- */
- enum Direction {
- NORTH (-1, 0),
- SOUTH ( 1, 0),
- EAST ( 0, 1),
- WEST ( 0,-1),
- NORTHWEST (-1, 1),
- NORTHEAST (1, 1),
- SOUTHWEST (-1, -1),
- SOUTHEAST (1, -1);
- private final int dI, dJ;
- private Direction(int di, int dj) { this.dI = di; this.dJ = dj; }
- public int dI() { return dI; }
- public int dJ() { return dJ; }
- public static Direction random() {
- return values()[(int) (Math.random() * values().length)];
- }
- }
- /**
- * Cases principales du labyrinthe.
- */
- class Cell {
- // On maintient une reference vers le labyrinthe et les coordonnees.
- private final Model laby;
- private final int i, j;
- private Creature c = null ;
- // Constructeur.
- public Cell(Model laby, int i, int j) {
- this.laby = laby;
- this.i = i;
- this.j = j;
- this.c = null;
- }
- public void setCreature(Creature c){
- this.c = c;
- }
- public Creature getCreature(){
- return c;
- }
- public Cell getNext (Direction dir){
- return this.laby.get(this.i+dir.dI(), this.j+dir.dJ());
- }
- // Partie dessin.
- public void paintCell(Graphics2D g2d, int leftX, int topY, int scale) {
- Rectangle2D rect = new Rectangle2D.Double(leftX, topY, scale, scale);
- g2d.setPaint(getColor());
- g2d.fill(rect);
- if (c!= null){ c.paintCreature(g2d, leftX, topY, scale);}
- }
- public Color getColor(){
- return Color.WHITE;
- }
- public boolean isPassable(){
- if(this.c != null){
- return false;
- } else if (this.getColor() == Color.RED){
- return false;
- }
- else return true;
- }
- public void removeHero(){
- c = null;
- }
- }
- /**
- * La classe Door
- */
- abstract class Door extends Cell{
- public Door(Model laby, int i, int j) {
- super(laby, i, j);
- }
- public static OpenDoor openDoorFactory(Model laby, int i, int j){
- return new OpenDoor(laby, i ,j);
- }
- public static ClosedDoor closedDoorFactory(Model laby, int i, int j){
- return new ClosedDoor(laby, i, j);
- }
- abstract public void changeState();
- }
- class OpenDoor extends Door{
- public OpenDoor(Model laby, int i, int j){
- super(laby, i, j);
- }
- public Color getColor(){
- return Color.GREEN;
- }
- public void changeState() {
- }
- }
- class ClosedDoor extends Door{
- public ClosedDoor(Model laby, int i, int j){
- super(laby, i, j);
- }
- public Color getColor(){
- return Color.RED;
- }
- public void changeState() {
- }
- }
- abstract class Creature{
- private Cell cell = null;
- protected boolean isHero;
- public Creature(Cell c ){cell = c; cell.setCreature(this); this.isHero = false;}
- public void move(Direction dir) throws NotTraversableCell {
- Cell c = this.cell.getNext(dir);
- if(c.getCreature() != null){
- this.cell.removeHero();
- System.out.println("Perdu!");
- System.exit(0);
- } else if(c.isPassable()){
- this.cell.setCreature(null);
- c.setCreature(this);
- this.cell = c;
- } else if(this.isHero==true){
- throw new NotTraversableCell(c);
- }
- else throw new NotTraversableCell();
- }
- public Cell getCell(){
- return cell;
- }
- public void paintCreature(Graphics2D g2d, int leftX, int topY, int scale){
- Ellipse2D ell= new Ellipse2D.Double(leftX, topY, scale, scale);
- g2d.setPaint(getColor());
- g2d.fill(ell);
- }
- abstract Color getColor();
- }
- class NotTraversableCell extends Exception {
- public NotTraversableCell(){
- }
- public NotTraversableCell(Cell c){
- if (c.getColor() == Color.BLACK){
- System.out.println("c'est un mur! ce n'est pas traversable!");
- }
- if (c.getColor() == Color.RED){
- System.out.println("la porte est fermee");
- }
- }
- }
- /**
- * La classe du heros.
- */
- // Cette classe est a remanier, ce petit morceau est la juste pour eviter
- // une erreur de compilation due a l'absence de la mathode [move].
- class Hero extends Creature{
- public Hero(Cell c) {
- super(c);
- this.isHero = true;
- }
- public Color getColor(){
- return Color.blue;
- }
- }
- class Monster extends Creature{
- public Monster(Cell c) {
- super(c);
- }
- public Color getColor(){
- return Color.GRAY;
- }
- }
- /**
- * La classe Wall
- */
- class Wall extends Cell {
- public Wall(Model laby, int i, int j) {
- super(laby, i, j);
- }
- public Color getColor(){
- return Color.BLACK;
- }
- public boolean isPassable(){
- return false;
- }
- }
- /**
- * La classe Exit
- */
- class Exit extends Cell {
- public Exit(Model laby, int i, int j) {
- super(laby, i, j);
- }
- public Color getColor(){
- return Color.BLUE;
- }
- public boolean isPassable(){
- if (this.getCreature()!=null){
- return false;
- }
- else return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement