Commit 66acf99f authored by erreiip's avatar erreiip
Browse files

Refacto of the view and model

parent abe92cf7
Loading
Loading
Loading
Loading
−195 B (5.37 KiB)

File changed.

No diff preview for this file type.

+1 −0
Changes for jeuAssemblage/maps.tetriste: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -8,3 +8,4 @@
15;15;8;2245528489552031744
15;15;8;2245528489552031744
15;15;8;2245528489552031744
10;14;6;2373647313285170176
+0 −8
Changes for jeuAssemblage/src/main/Controller.java: 0 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -104,16 +104,8 @@ public class Controller {
	}

	public void addPieceOnBoard(int x, int y) {

		this.model.addSelectedPiece(x, y);
		this.view.repaint();
		this.eventManager.fireEvent();
	}

	public void removeSelectedPiece() {

		this.model.removeSelectedPiece();
		this.view.repaint();
		this.eventManager.fireEvent();
	}
}
+52 −36
Changes for jeuAssemblage/src/model/PlayBoard.java: 52 added lines, 36 removed lines.
Original line number Diff line number Diff line
@@ -55,7 +55,15 @@ public class PlayBoard {
    public Long             getSeed          () { return this.seed; }

    public Image getImageById (int id) { return this.hmPieceImage.get(id); }
    public Piece getPieceById (int id) { return this.alPieceOnBoard.get(id-1);}

    public Piece getPieceById(int id) {
        for (Piece p : this.alPieceOnBoard) {
            if (p.getInstanceId() == id) {
                return p;
            }
        }
        return null;
    }
    
    // Setters
    public void setPieceSelected(Piece p) { 
@@ -81,23 +89,9 @@ public class PlayBoard {

        if (this.selectedPiece == null) return;

        this.addPieceOnBoard(this.selectedPiece, x, y);

		int index = this.alPieceOnBoard.indexOf(this.getPieceById(this.selectedPiece.getInstanceId()));
        this.alPieceOnBoard.remove(this.getPieceById(this.selectedPiece.getInstanceId()));
		this.alPieceOnBoard.add(index, this.selectedPiece);
        this.selectedPiece = null;

		printMatrice(playBoard);
    }

	public void removeSelectedPiece() {

		if (this.selectedPiece == null) return;

		this.removePieceOnBoard(this.selectedPiece);

		this.alPieceOnBoard.add(this.getPieceById(this.selectedPiece.getInstanceId()));
        this.removePieceOnBoard(this.getPieceById(this.selectedPiece.getInstanceId()));
        this.addPieceOnBoard(this.selectedPiece, x - 1, y - 1);
        this.removeSelectedPiece();
    }

    /**
@@ -108,7 +102,7 @@ public class PlayBoard {
    public boolean selectedPieceCanBeAddedToBoard(int x, int y) {
        if (this.selectedPiece == null) return false;

        return this.canBeAddedToBoard(x, y, this.selectedPiece.getWidth(), this.selectedPiece.getHeight());
        return this.canBeAddedToBoard(this.selectedPiece, x - 1, y - 1);
    }

    /**
@@ -127,7 +121,10 @@ public class PlayBoard {
        return ret;
    }


    /**
    * bla bla 
    *
    */
    public void registerArrangement() {
        String str = this.playBoard.length + ";" +
                this.playBoard[0].length + ";" +
@@ -153,6 +150,11 @@ public class PlayBoard {
        }
    }
    
    /**
    * bla bla 
    *
    * @return   an ArrayList of String that represent the file
    */
    public ArrayList<String> getArrangement() {
        ArrayList<String> alRet = new ArrayList<String>();
        File f = new File(this.getPath() + "/maps.tetriste");
@@ -189,6 +191,14 @@ public class PlayBoard {
        return path.toAbsolutePath().toString();
    }

	private void removeSelectedPiece() {
		if (this.selectedPiece == null) return;

        this.alPieceOnBoard.remove(this.getPieceById(this.selectedPiece.getInstanceId()));
        this.alPieceOnBoard.add(this.selectedPiece);
        this.selectedPiece = null;
	}

    private ArrayList<Piece> createPieces(int numberPiece) {
        ArrayList<Piece> tempAl = new ArrayList<Piece>();
        int samePieceLimit = (int) Math.ceil(numberPiece / (PieceFactory.NUMBER_PIECE * 1d));
@@ -233,7 +243,7 @@ public class PlayBoard {
            do {
                x = rand.nextInt(this.playBoard[0].length - p.getWidth ());
                y = rand.nextInt(this.playBoard.length    - p.getHeight());
            } while( !canBeAddedToBoard(x, y, p.getWidth(), p.getHeight()) );
            } while( !canBeAddedToBoard(p, x, y) );

            addPieceOnBoard(p, x, y);
        }
@@ -244,16 +254,11 @@ public class PlayBoard {
        int[][] bounds = p.getBounds();
        int k = 0;
        int l = 0;

        for (int i = y; i < y + bounds.length; i++) {

			for (int j = x; j < x + bounds[i - y].length; j++) {

				if (bounds[k][l] == 1) {

					this.playBoard[i][j] = p.getInstanceId() + 1;
					this.playBoard[i][j] = p.getInstanceId();
				}

				l++;
			}
			k++;
@@ -265,24 +270,35 @@ public class PlayBoard {
    }

	public void removePieceOnBoard(Piece p) {

		int[][] bounds = p.getBounds();
        int k = 0;
        for (int i = p.getY(); i < p.getY() + p.getHeight(); i++) {

            int l = 0;
            for (int j = p.getX(); j < p.getX() + p.getWidth(); j++) {

                if (bounds[k][l] != 0) {
                    this.playBoard[i][j] = 0;
                }
                l++;
            }
            k++;
        }
	}

    private boolean canBeAddedToBoard(int x, int y, int width, int height) {
        if ( x + width  > this.playBoard.length   ) return false;
        if ( y + height > this.playBoard[0].length) return false;
    private boolean canBeAddedToBoard(Piece p, int x, int y) {
        if ( x + p.getWidth()  > this.playBoard.length   ) return false;
        if ( y + p.getHeight() > this.playBoard[0].length) return false;

        for (int i = y; i < y + height; i++) {
            for (int j = x; j < x + width; j++) {
                if (this.playBoard[i][j] != 0 ) return false;
        int[][] bounds = p.getBounds();
        int k = 0;
        for (int i = y; i < y + p.getHeight(); i++) {
            int l = 0;
            for (int j = x; j < x + p.getWidth(); j++) {
                if ((this.playBoard[i][j] != 0 && this.playBoard[i][j] != p.getInstanceId()) && bounds[k][l] != 0) {
                    return false;
                }
                l++;
            }
            k++;
        }
        return true;
    }
+2 −8
Changes for jeuAssemblage/src/view/component/board/Grid.java: 2 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -29,10 +29,10 @@ public class Grid extends JPanel {
        
        this.setLayout(null);

        this.setVisible(true);

		this.addMouseListener(new GridClickListener());
		this.addMouseMotionListener(new GridMouseMotionListener());
		
		this.setVisible(true);
    }

	@Override
@@ -73,7 +73,6 @@ public class Grid extends JPanel {
                if (matrices[i][j] != 0) {

                    Piece p = this.controller.getPieceById(matrices[i][j]);

					if (p.equals(controller.getSelectedPiece())) {
						
						g2d.setColor(Color.RED);
@@ -167,13 +166,9 @@ public class Grid extends JPanel {
				if (controller.getSelectedPiece() == null) return;

				if (controller.canBeAddedToBoard(x, y)) {

					controller.addPieceOnBoard(x, y);
					controller.setPieceSelected(null);
					getParent().repaint();
				}
				else {

					System.out.println("Piece can't be added to the board");
				}

@@ -193,7 +188,6 @@ public class Grid extends JPanel {
		public void mouseMoved(MouseEvent e) {

			if (controller.getSelectedPiece() != null) {

				mousePosition.setLocation(e.getX(), e.getY());
				repaint();
			}
Loading