洪水检查开始时的颜色是否有效

时间:2017-03-07 04:16:36

标签: java matrix

我被困在我的Flood It游戏中。我试图解决获得起始矩阵并找到连接到点(0,0)的相同颜色的问题。例如,如果生成了我的起始矩阵:

1 4 5
1 1 1
5 3 2

它应该捕获1,以便在下一轮让我说我选择4然后矩阵应该是:

4 4 5
4 4 4
5 3 2

然而,当我现在选择4时,矩阵是:

4 4 5
1 1 1
5 3 2

我知道这是任何正常的Flood It游戏的一部分,但我坚持实施它。 '

import java.awt.event.*;
import javax.swing.*;

public class GameController implements ActionListener {

private GameModel model;
private GameView view;
private MyStack<DotInfo> dots;
private int size;
private DotInfo dot;

/**
 * Constructor used for initializing the controller. It creates the game's view 
 * and the game's model instances
 * 
 * @param size
 *            the size of the board on which the game will be played
 */
public GameController(int size) {
    this.size = size;
    model = new GameModel(size);
    view = new GameView(model, this);
    dots = new MyStack<DotInfo>(size*size);
    /*view.reset.addActionListener(this);
    view.quit.addActionListener(this);
    view.grey.addActionListener(this);
    view.yellow.addActionListener(this);
    view.blue.addActionListener(this);
    view.green.addActionListener(this);
    view.violet.addActionListener(this);
    view.red.addActionListener(this);*/
}

/**
 * resets the game
 */
public void reset(){
    model.reset();
    System.out.println(model);
}

/**
 * Callback used when the user clicks a button (reset or quit)
 *
 * @param e
 *            the ActionEvent
 */

public void actionPerformed(ActionEvent e) {

    if (e.getSource() instanceof JButton) {
        JButton x = (JButton) e.getSource();
        if (x.getText()=="Quit") {        //Quit button
            System.exit(0);
        }
        else if (x.getText()=="Reset") {  //Reset button
            reset();
            view.update();
        }
    }

}

/**
 * <b>selectColor</b> is the method called when the user selects a new color.
 * If that color is not the currently selected one, then it applies the logic
 * of the game to capture possible locations. It then checks if the game
 * is finished, and if so, congratulates the player, showing the number of
 * moves, and gives two options: start a new game, or exit
 * @param color
 *            the newly selected color
 */
public void selectColor(int color){
        model.setCurrentSelectedColor(color);
        sendCapturedToStack();
        equalityCheck(color);
        System.out.println(model);
}

/** 
 * On every turn <b>sendCapturedToStack</b> will push every captured dot to the stack.
 * It will also allow the colours to change every time a user picks a color.
 */
private void sendCapturedToStack() {
    model.capture
    for (int j=0;j<size;j++) {
        for (int i=0;i<size;i++) {
            if (model.isCaptured(i, j)) {
                model.capture(i, j);
                dots.push(model.get(i,j));
            }
        }
    }
}

/**
 * <b>equalityCheck</b> checks to see if there is a dot to the left, right, above, or below
 * another dot. If there is then the dot will be captured and pushed to the top of the stack.
 * @param newColor
 *                 the newly selected color
 */
private void equalityCheck(int newColor) {
    while (!dots.isEmpty()) {
        dot = dots.pop();
        int x = dot.getX();
        int y = dot.getY();
        if (y<size-1 && model.getColor(x,y+1)==newColor && !model.isCaptured(x,y+1)) {
            model.capture(x, y+1);
            dots.push(model.get(x,y+1));
        } if (x<size-1 && model.getColor(x+1,y)==newColor && !model.isCaptured(x+1,y)) {
            model.capture(x+1, y);
            dots.push(model.get(x+1,y));
        } if (y>0 && model.getColor(x,y-1)==newColor && !model.isCaptured(x,y-1)) {
            model.capture(x, y-1);
            dots.push(model.get(x,y-1));
        } if (x>0 && model.getColor(x-1,y)==newColor && !model.isCaptured(x-1,y)) {
            model.capture(x-1, y);
            dots.push(model.get(x-1,y));
        }
    }
}
}

0 个答案:

没有答案