Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在洪水填充游戏中无法显示颜色_Java_Arrays - Fatal编程技术网

Java 在洪水填充游戏中无法显示颜色

Java 在洪水填充游戏中无法显示颜色,java,arrays,Java,Arrays,我的洪水填充算法工作,但是,我不能打印我的游戏的6种主要颜色。允许我打印二维数组的Java函数位于名为toString()的GameModel.Java类中: import java.awt.*; 公共类GameController/*实现ActionListener*/{ 私人博弈模型; 私人MyStack点; 私有整数大小; /** *用于初始化控制器的构造函数。它创建游戏的视图 *以及游戏的模型实例 * *@param大小 *游戏将在其上进行的棋盘的大小 */ 公共游戏控制器(整数大小

我的洪水填充算法工作,但是,我不能打印我的游戏的6种主要颜色。允许我打印二维数组的Java函数位于名为toString()的GameModel.Java类中:

import java.awt.*;
公共类GameController/*实现ActionListener*/{
私人博弈模型;
私人MyStack点;
私有整数大小;
/**
*用于初始化控制器的构造函数。它创建游戏的视图
*以及游戏的模型实例
* 
*@param大小
*游戏将在其上进行的棋盘的大小
*/
公共游戏控制器(整数大小){
这个。大小=大小;
模型=新游戏模型(大小);
圆点=新的MyStack(尺寸*尺寸);
}
/**
*重置游戏
*/
公共无效重置(){
model.reset();
System.out.println(模型);
}
/**
*用户单击按钮时使用的回调(重置或退出)
*
*@param e
*行动事件
*/
/*已执行的公共无效操作(操作事件e){
}*/
/**
*selectColor是用户选择新颜色时调用的方法。
*如果该颜色不是当前选择的颜色,则应用该逻辑
*以捕获可能的位置。然后检查游戏
*完成,如果是,向玩家表示祝贺,并显示
*移动,并提供两个选项:开始新游戏或退出
*@param颜色
*新选择的颜色
*/
公共void selectColor(int-color){
model.setCurrentSelectedColor(颜色);
capturePointAtZero();
SendCapturedStatck();
均衡检查(颜色);
System.out.println(模型);
}
私有void capturePointAtZero(){
int x=0,y=0;
如果(!model.isCaptured(x,y)){
模型捕获(x,y);
}
}
私有void sendCapturedStatck(){

对于(int j=0;j而言,代码中存在太多问题,无法解决所有问题。在快速查看之后,我注意到以下几个问题:

  • 是的,矩阵中填充0的原因是
    currentColor
    为0,因为您不调用
    setCurrentSelectedColor

  • 更深层次的问题是逻辑。让我们看看
    equalityCheck

  • if(model.getColor(x,y+1)=newColor&&!model.isCaptured(x,y+1))
    {
    模型捕获(x,y+1);
    dots.push(model.get(x,y+1));
    }
    
    此代码意味着(x,y+1)处的点只有在其已经具有
    newColor
    时才会更新。显然,如果它与“旧颜色”(即(0,0)的颜色)匹配,则您希望它被更新,但您甚至不尝试将该颜色保存在
    capturePointAtZero

  • 代码,例如
  • 公共无效捕获(inti,intj){
    
    对于(int y=0;我理解您的评论,但现在每当我调用矩阵末尾的数字时,我都会得到一个arrayIndexOutOfBounds。这是线程“main”java.lang.ArrayIndexOutOfBoundsException中的错误异常:-1 at GameModel.getColor(GameModel.java:85)在GameController.equalityCheck(GameController.java:126)在GameController.FloodIt.main(FloodIt.java:35)选择color(GameController.java:78)@GarretUlrich,请参阅我的回答中的更新如果我有限制,我如何能够捕获每个端点的最后一个元素?我不确定我是否理解你的最后一个问题。你还记得Java中大小为
    N
    的数组是索引为
    [0..(N-1)]
    的数组吗?
    import java.util.Random;
    
    public class GameModel {
    
    
    /**
     * predefined values to capture the color of a DotInfo
     */
    public static final int COLOR_0           = 0;
    public static final int COLOR_1           = 1;
    public static final int COLOR_2           = 2;
    public static final int COLOR_3           = 3;
    public static final int COLOR_4           = 4;
    public static final int COLOR_5           = 5;
    public static final int NUMBER_OF_COLORS  = 6;
    
    private static DotInfo[][] dots;
    private int size;
    private int currentColor;
    private Random generator;
    private int steps;
    
    /**
     * Constructor to initialize the model to a given size of board.
     * 
     * @param size
     *            the size of the board
     */
    public GameModel(int size) {
        this.size = size;
        dots = new DotInfo[size][size];
        generator = new Random();
    }
    
    
    /**
     * Resets the model to (re)start a game. The previous game (if there is one)
     * is cleared up . 
     */
    public void reset(){
        generator = new Random();
        int color = 0;
        for (int j=0;j<size;j++) {
            for (int i=0;i<size;i++) {
                dots[j][i] = new DotInfo(i, j, generator.nextInt(NUMBER_OF_COLORS)+1);
            }
        }
    }
    
    
    /**
     * Getter method for the size of the game
     * 
     * @return the value of the attribute sizeOfGame
     */   
    public int getSize(){
        return size;
    }
    
    /**
     * returns the current color  of a given dot in the game
     * 
     * @param i
     *            the x coordinate of the dot
     * @param j
     *            the y coordinate of the dot
     * @return the status of the dot at location (i,j)
     */   
    public int getColor(int i, int j){
        return dots[j][i].getColor();
    }
    
    /**
     * returns true is the dot is captured, false otherwise
    * 
     * @param i
     *            the x coordinate of the dot
     * @param j
     *            the y coordinate of the dot
     * @return the status of the dot at location (i,j)
     */   
    public boolean isCaptured(int i, int j){
        return dots[j][i].isCaptured();
    }
    
    /**
     * Sets the status of the dot at coordinate (i,j) to captured
     * 
     * @param i
     *            the x coordinate of the dot
     * @param j
     *            the y coordinate of the dot
     */   
    public void capture(int i, int j){
        dots[j][i] = new DotInfo(i, j, currentColor);
        dots[j][i].setCaptured(true);
    }
    
    
    /**
     * Getter method for the current number of steps
     * 
     * @return the current number of steps
     */   
    public int getNumberOfSteps(){
        return steps;
    }
    
    /**
     * Setter method for currentSelectedColor
     * 
     * @param val
     *            the new value for currentSelectedColor
    */   
    public void setCurrentSelectedColor(int val) {
        currentColor = val;
    }
    
    /**
     * Getter method for currentSelectedColor
     * 
     * @return currentSelectedColor
     */   
    public int getCurrentSelectedColor() {
        return currentColor;
    }
    
    
    /**
     * Getter method for the model's dotInfo reference
     * at location (i,j)
     *
      * @param i
     *            the x coordinate of the dot
     * @param j
     *            the y coordinate of the dot
     *
     * @return model[i][j]
     */   
    public DotInfo get(int i, int j) {
        return dots[j][i];
    }
    
    
     /**
     * The metod <b>step</b> updates the number of steps. It must be called 
     * once the model has been updated after the payer selected a new color.
     */
     public void step(){
        steps++;
    }
    
     /**
     * The metod <b>isFinished</b> returns true iff the game is finished, that
     * is, all the dats are captured.
     *
     * @return true if the game is finished, false otherwise
     */
    public boolean isFinished(){
        boolean flag=true;
        for (int y=0;y<size;y++) {
            for (int x=0;x<size;x++) {
                if (dots[y][x].isCaptured()==false) {
                    flag=false;
                }
            }
        }
        return flag;
    }
    
    
     /**
     * Builds a String representation of the model
     *
     * @return String representation of the model
     */
    public String toString(){
        String rep = "";
        for (int y=0;y<size;y++) {
            for (int x=0;x<size;x++) {
                rep += dots[y][x].getColor()+" ";
            }
            rep+="\n";
        }
        return rep;
    }
    
    System.out.println(model)
    
    import java.awt.*;
    
    public class GameController /*implements ActionListener*/ {
    
    private GameModel model;
    private MyStack dots;
    private int size;
    
    /**
     * 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);
        dots = new MyStack(size*size);
    }
    
    /**
     * 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) {
    
    }*/
    
    /**
     * <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);
        capturePointAtZero();
        sendCapturedToStack();
        equalityCheck(color);
        System.out.println(model);
    }
    
    private void capturePointAtZero() {
        int x = 0, y = 0;
        if (!model.isCaptured(x, y)) {
            model.capture(x, y);
        }
    }
    
    private void sendCapturedToStack() {
        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));
                }
            }
        }
    }
    
    private void equalityCheck(int newColor) {
        while (!dots.isEmpty()) {
            DotInfo dot = dots.pop();
            int x = dot.getX();
            int y = dot.getY();
            if (model.getColor(x,y+1)==newColor && !model.isCaptured(x,y+1)) {
                model.capture(x, y+1);
                dots.push(model.get(x,y+1));
            } if (model.getColor(x+1,y)==newColor && !model.isCaptured(x+1,y)) {
                model.capture(x+1, y);
                dots.push(model.get(x+1,y));
            } if (model.getColor(x,y-1)==newColor && !model.isCaptured(x,y-1)) {
                model.capture(x, y-1);
                dots.push(model.get(x,y-1));
            } if (model.getColor(x-1,y)==newColor && !model.isCaptured(x-1,y)) {
                model.capture(x-1, y);
                dots.push(model.get(x-1,y));
            }
        }
    }
    }