~eduardo_quiros/snake-game

39bd5121a07ba85477c3451840ae76074c3242cc — Eduardo Quiros 3 months ago cd7f8f2 master
add memento management
M src/com/zetcode/Board.java => src/com/zetcode/Board.java +44 -6
@@ 1,10 1,11 @@
package com.zetcode;

import com.zetcode.bl.apples.concrete.RedApple;
import com.zetcode.bl.apples.prototype.Apple;
import com.zetcode.bl.dots.component.Dot;
import com.zetcode.bl.memento.Memento;
import com.zetcode.tl.AppleManager;
import com.zetcode.tl.DotManager;
import com.zetcode.tl.MementoManager;

import java.awt.Color;
import java.awt.Dimension;


@@ 18,7 19,6 @@ import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.text.html.ImageView;

public class Board extends JPanel implements ActionListener {



@@ 30,16 30,17 @@ public class Board extends JPanel implements ActionListener {
    private final int RAND_POS = 29;
    private final int DELAY = 140;

    private final int[] x = new int[ALL_DOTS];
    private final int[] y = new int[ALL_DOTS];
    private int[] x = new int[ALL_DOTS];
    private int[] y = new int[ALL_DOTS];
    private final JLabel scoreLabel = new JLabel();
    private final JLabel livesLabel = new JLabel();

    private int dots;
    private int apple_x;
    private int apple_y;
    private final int lives = 3;
    private int lives = 3;
    private int score = 0;
    private int speed = 0;

    private boolean leftDirection = false;
    private boolean rightDirection = true;


@@ 54,6 55,7 @@ public class Board extends JPanel implements ActionListener {
    
    private final AppleManager appleManager = new AppleManager();
    private final DotManager dotManager = new DotManager();
    private final MementoManager mementoManager = new MementoManager();

    public Board() {
        


@@ 70,6 72,7 @@ public class Board extends JPanel implements ActionListener {

        setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
        loadImages();
        setMemento();
        initGame();
    }



@@ 83,6 86,27 @@ public class Board extends JPanel implements ActionListener {

        ImageIcon iih = new ImageIcon("src/resources/dots/head.png");
        head = iih.getImage();
        
        if ((dots-3)%5==0)
            setMemento();
    }

    private void setMemento() {
        mementoManager.actualizarMemento(score,x,y,dots,speed,rightDirection,leftDirection,downDirection,
                                         upDirection);
    }
    
    private void getMemento(){
        Memento memento = mementoManager.getMemento();
        speed = memento.getSpeed();
        score = memento.getScore();
        x = memento.getApple_x();
        y = memento.getApple_y();
        dots = memento.getDots();
        rightDirection = memento.isRight();
        leftDirection = memento.isLeft();
        downDirection = memento.isDown();
        upDirection = memento.isUp();
    }

    private void initGame() {


@@ 147,6 171,9 @@ public class Board extends JPanel implements ActionListener {
        if ((x[0] == apple_x) && (y[0] == apple_y)) {
            score += apple.getPoints();
            loadImages();
            increaseSpeed();
            timer = new Timer(DELAY-speed,this);
            timer.start();
            dots++;
            scoreLabel.setText("Puntaje: " + score);
            locateApple();


@@ 203,7 230,13 @@ public class Board extends JPanel implements ActionListener {
        }
        
        if (!inGame) {
            timer.stop();
            if (lives > 0){
                lives--;
                inGame = true;
                getMemento();
            }else {
                timer.stop();
            }
        }
    }



@@ 228,6 261,11 @@ public class Board extends JPanel implements ActionListener {

        repaint();
    }
    
    public void increaseSpeed(){
        if (speed<40)
            speed+=4;
    }

    private class TAdapter extends KeyAdapter {


A src/com/zetcode/bl/memento/Caretaker.java => src/com/zetcode/bl/memento/Caretaker.java +13 -0
@@ 0,0 1,13 @@
package com.zetcode.bl.memento;

public class Caretaker {
	private Memento memento;
	
	public void setMemento(Memento memento){
		this.memento = memento;
	}
	
	public Memento getMemento(){
		return this.memento;
	}
}

A src/com/zetcode/bl/memento/Memento.java => src/com/zetcode/bl/memento/Memento.java +92 -0
@@ 0,0 1,92 @@
package com.zetcode.bl.memento;

public class Memento {
	private int[] x, y;
	private int score, dots, speed;
	private boolean right, left, down, up;

	public Memento(int score, int[] x, int[] y, int dots, int speed, boolean right, boolean left, boolean down
			, boolean up) {
		this.score = score;
		this.x = x;
		this.y = y;
		this.dots = dots;
		this.speed = speed;
		this.right = right;
		this.left = left;
		this.down = down;
		this.up = up;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public int[] getApple_x() {
		return x;
	}

	public void setApple_x(int[] x) {
		this.x = x;
	}

	public int[] getApple_y() {
		return y;
	}

	public void setApple_y(int[] y) {
		this.y = y;
	}

	public int getDots() {
		return dots;
	}

	public void setDots(int dots) {
		this.dots = dots;
	}

	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public boolean isRight() {
		return right;
	}

	public void setRight(boolean right) {
		this.right = right;
	}

	public boolean isLeft() {
		return left;
	}

	public void setLeft(boolean left) {
		this.left = left;
	}

	public boolean isDown() {
		return down;
	}

	public void setDown(boolean down) {
		this.down = down;
	}

	public boolean isUp() {
		return up;
	}

	public void setUp(boolean up) {
		this.up = up;
	}
}

A src/com/zetcode/tl/MementoManager.java => src/com/zetcode/tl/MementoManager.java +17 -0
@@ 0,0 1,17 @@
package com.zetcode.tl;

import com.zetcode.bl.memento.Caretaker;
import com.zetcode.bl.memento.Memento;

public class MementoManager {
	private Caretaker vigilante = new Caretaker();

	public void actualizarMemento(int score, int[] x, int[] y, int dots, int speed, boolean right, boolean left,
	                              boolean down, boolean up){
		vigilante.setMemento(new Memento(score,x,y,dots,speed,right,left,down,up));
	}
	
	public Memento getMemento(){
		return vigilante.getMemento();
	}
}