⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 origmazegraphics.java

📁 控制移到机器人的例子程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    public boolean removeGold(int x1, int y1, int x2, int y2, Vector dagolds)
    {
        for (int i=0; i < dagolds.size(); i++) {
            int[] goldpos = (int[])(dagolds.elementAt(i));
            if ((goldpos.length == 4) &&
                (((goldpos[0] == x1) && (goldpos[1] == y1) &&
                 (goldpos[2] == x2) && (goldpos[3] == y2)) ||
                 ((goldpos[0] == x2) && (goldpos[1] == y2) &&
                 (goldpos[2] == x1) && (goldpos[3] == y1))))
                 {
                dagolds.removeElementAt(i);
                eraseGoldPos(xCell2Pixel(x1), xCell2Pixel(y1), xCell2Pixel(x2), xCell2Pixel(y2));
                if (x1 == x2 && y1 == y2-1)
                    eraseWall(x1, y1, 0);
                else if (x1 == x2 && y1 == y2+1)
                    eraseWall(x1, y1, 2);
                else if (y1 == y2 && x1 == x2 -1)
                    eraseWall(x1, y1, 3);
                else if (y1 == y2 && x1 == x2 + 1)
                    eraseWall(x1, y1, 1);
                return(true);
            } //endif
        } //endfor
        return(false);
    } // removeGold()2 //

    public int[] findOtherGoldCell(int x, int y)
    {
        return(this.findOtherGoldCell(x,y,this.golds));
    } //
    public int[] findOtherGoldCell(int x, int y, Vector dagolds)
    {
        for (int i=0; i < dagolds.size(); i++) {
            int[] goldpos = (int[])(dagolds.elementAt(i));
            if (goldpos.length == 4) {
                if ((goldpos[0] == x) && (goldpos[1] == y)) {
                    int[] temp = new int[2];
                    temp[0] = goldpos[2]; temp[1] = goldpos[3];
                    return(temp);
                }
                if ((goldpos[2] == x) && (goldpos[3] == y)) {
                    int[] temp = new int[2];
                    temp[0] = goldpos[0]; temp[1] = goldpos[1];
                    return(temp);
                }
            } // end if (goldpos.length == 4
        } // endfor
        return((int[])null);
    } // findOtherGoldCell() //

    // this searches for any accessible piece of gold (from here) and
    // removes it.
    public boolean removeAnyGold(int x, int y, Vector golds)
    {
        if (this.removeGold(x,y,golds)) {return(true);}
        else
        if (this.removeGold(x,y,x-1,y,golds)) {return(true);}
        else
        if (this.removeGold(x,y,x+1,y,golds)) {return(true);}
        else
        if (this.removeGold(x,y,x,y-1,golds)) {return(true);}
        else
        if (this.removeGold(x,y,x,y+1,golds)) {return(true);}
        return(false);
    } // removeAnyGold() //
    // this version does *not* remove gold at the bays, though.
    // this is for robot "control"
    public boolean removeAnyGoldRobot(int x, int y, Vector golds)
    {
        if ((x > 0) && (x < this.width-1) &&
            (this.removeGold(x,y,golds))) {return(true);}
        else
        if (this.removeGold(x,y,x-1,y,golds)) {return(true);}
        else
        if (this.removeGold(x,y,x+1,y,golds)) {return(true);}
        else
        if (this.removeGold(x,y,x,y-1,golds)) {return(true);}
        else
        if (this.removeGold(x,y,x,y+1,golds)) {return(true);}
        return(false);
    } // removeAnyGoldRobot() //


    // takes an x,y cell and a wallspec and draws the wallspec for the cell
    public boolean cellWallspec(int x, int y, int[] wallspec)
    {
        for (int Index = 0; Index < 4; Index ++) {
            if (wallspec[Index] == 1)
                drawWall(x, y, Index);
        }
        return(true);
    } // cellWallSpec() //

    public boolean drawWall(int x, int y, int dir)
    {
        switch(dir) {
        case 0:
            drawHorizontalLine(this.xCell2Pixel(x),
                               this.xCell2Pixel(x+1),
                               this.yCell2Pixel(y),
                               this.wall_thickness);
            break;
        // if (wallspec[0]) //
        case 2:
            drawHorizontalLine(this.xCell2Pixel(x),
                               this.xCell2Pixel(x+1),
                               this.yCell2Pixel(y-1),
                               this.wall_thickness);
            break;
        // if (wallspec[2]) //
        case 1:
            drawVerticalLine(this.xCell2Pixel(x),
                             this.yCell2Pixel(y),
                             this.yCell2Pixel(y-1),
                             this.wall_thickness);
            break;
        // if (wallspec[1]) //
        case 3:
            drawVerticalLine(this.xCell2Pixel(x+1),
                             this.yCell2Pixel(y),
                             this.yCell2Pixel(y-1),
                             this.wall_thickness);
            break;
        // if (wallspec[3]) //
        }
        return true;
    }

    public boolean eraseWall(int x, int y, int dir)
    {
        switch(dir) {
        case 0:
            eraseHorizontalLine(this.xCell2Pixel(x),
                                this.xCell2Pixel(x+1),
                                this.yCell2Pixel(y),
                                this.wall_thickness);
            drawHorizontalLine(this.xCell2Pixel(x),
                                this.xCell2Pixel(x+1),
                                this.yCell2Pixel(y),
                                1);
            break;
        // if (wallspec[0]) //
        case 2:
            eraseHorizontalLine(this.xCell2Pixel(x),
                                this.xCell2Pixel(x+1),
                                this.yCell2Pixel(y-1),
                                this.wall_thickness);
            drawHorizontalLine(this.xCell2Pixel(x),
                                this.xCell2Pixel(x+1),
                                this.yCell2Pixel(y-1),
                                1);
            break;
        // if (wallspec[2]) //
        case 1:
            eraseVerticalLine(this.xCell2Pixel(x),
                              this.yCell2Pixel(y),
                              this.yCell2Pixel(y-1),
                              this.wall_thickness);
            drawVerticalLine(this.xCell2Pixel(x),
                              this.yCell2Pixel(y),
                              this.yCell2Pixel(y-1),
                              1);
            break;
        // if (wallspec[1]) //
        case 3:
            eraseVerticalLine(this.xCell2Pixel(x+1),
                              this.yCell2Pixel(y),
                              this.yCell2Pixel(y-1),
                              this.wall_thickness);
            drawVerticalLine(this.xCell2Pixel(x+1),
                              this.yCell2Pixel(y),
                              this.yCell2Pixel(y-1),
                              1);
            break;
        } // if (wallspec[3]) //
        return true;
    }

    public boolean cellGrid(int x, int y)
    {
            drawHorizontalLine(this.xCell2Pixel(x),
                               this.xCell2Pixel(x+1),
                               this.yCell2Pixel(y),
                               1);
            drawHorizontalLine(this.xCell2Pixel(x),
                               this.xCell2Pixel(x+1),
                               this.yCell2Pixel(y-1),
                               1);
            drawVerticalLine(this.xCell2Pixel(x),
                             this.yCell2Pixel(y),
                             this.yCell2Pixel(y-1),
                             1);
            drawVerticalLine(this.xCell2Pixel(x+1),
                             this.yCell2Pixel(y),
                             this.yCell2Pixel(y-1),
                             1);
        return(true);
    } // cellGrid() //


    // takes the cell,cell x,y position & direction of the robot & draws
    public boolean drawRobot(int[] robotdesc, int robotnumber)
    {
        int cell_x= robotdesc[0];
        int cell_y=robotdesc[1] ;
        int dir=robotdesc[2];
        int numgolds = robotdesc[3];
        int x,y; // drawRobotCircle needs upper left corner of cell block
        x = this.xCell2Pixel(cell_x);
        y = this.yCell2Pixel(cell_y);
        return(this.drawRobotCircle(x,y,dir,numgolds,robotnumber));
    } // drawRobot() //
    
    public boolean drawRobotLine(int[] robotdesc, int robotnumber)
    {
        int cell_x= robotdesc[0];
        int cell_y=robotdesc[1] ;
        int dir=robotdesc[2];
        int numgolds = robotdesc[3];
        int x,y; // drawRobotLine needs upper left corner of cell block
        x = this.xCell2Pixel(cell_x);
        y = this.yCell2Pixel(cell_y);
        return(this.drawRobotLine(x,y,dir,numgolds,robotnumber));
    } // drawRobotLines() //
    

    public boolean redoMazeCell(int cell_x, int cell_y)
    {
        int x,y;
        x = this.xCell2Pixel(cell_x);
        y = this.yCell2Pixel(cell_y);
        // now wipe the whole cell, including walls //
        x = x + this.anchor_x;
        y = y + this.anchor_y;
        Graphics g = this.parent_frame.getGraphics();
        g.setColor(Color.white);
        g.fillRect(x-
                    (int)(this.magnification *
                     this.wall_thickness * 0.5) + 2,
                   y-
                    (int)(this.magnification *
                     this.wall_thickness * 0.5) + 2,
                   this.cellsize+
                   (1 * (int)(this.magnification *
                         this.wall_thickness)) - 4,
                   this.cellsize+
                   (1 * (int)(this.magnification *
                         this.wall_thickness)) - 4);

        // now put in hash marks
        this.cellGrid(cell_x,cell_y);
        this.cellWallspec(cell_x,cell_y,this.walls[cell_x][cell_y]);
        this.drawGolds(cell_x,cell_y);
        this.drawRobots(cell_x,cell_y);
        g.dispose();
        return(true);
    } // redomazecell //

    // takes the cell,cell xy position of a port and draws it
    public boolean drawPort(int cell_x, int cell_y)
    {
        int x,y; // drawGoldPos needs upper x,y coordinates //
        x = this.xCell2Pixel(cell_x);
        y = this.yCell2Pixel(cell_y);
        return(this.drawPortPos(x,y));
    } // drawPort() //

    // takes the cell,cell xy position of a 1-cell gold and draws it
    public boolean drawGold(int cell_x, int cell_y)
    {
        int x,y; // drawGoldPos needs upper x,y coordinates //
        x = this.xCell2Pixel(cell_x);
        y = this.yCell2Pixel(cell_y);
        return(this.drawGoldPos(x,y));
    } // drawGold() //
    // overloaded 2-cell gold case..
    public boolean drawGold(int c1_x, int c1_y, int c2_x, int c2_y)
    {
        int x1, y1, x2, y2;
        x1 = this.xCell2Pixel(c1_x);
        x2 = this.xCell2Pixel(c2_x);
        y1 = this.yCell2Pixel(c1_y);
        y2 = this.yCell2Pixel(c2_y);
        return(this.drawGoldPos(x1,y1,x2,y2));
    }

    // draws the interior grid of the maze -- 1 pixel thick //
    public boolean drawGrid()
    {
        for (int i=1; i < this.height; i++) {
            this.drawHorizontalLine(0, (int)(this.width *
                                       this.cellsize *
                                       this.magnification),
                                    (int)(i *
                                    this.cellsize *
                                    this.magnification),
                                    1);
        } // end for() //
        for (int i=1; i < this.width; i++) {
            this.drawVerticalLine((int)(i * this.cellsize
                                        * this.magnification),
                                        0,(int)(this.height *
                                       this.cellsize *
                                       this.magnification),
                                    1);
        } // end for() //

        return(true);
    } // end drawGrid() //
    // draws the outside wall border of the maze //
    public boolean drawBorder()
    {
        this.drawHorizontalLine(0,
                                (int)(this.cellsize *
                                      this.width *
                                      this.magnification),
                                0,
                                this.wall_thickness);
        this.drawHorizontalLine(0,
                               (int)(this.cellsize *
                                     this.width *
                                     this.magnification),
                               (int) (this.cellsize *
                                      this.height *
                                      this.magnification),
                               this.wall_thickness);
        this.drawVerticalLine(0,
                              0,
                              (int) (this.cellsize *
                                     this.height *
                                     this.magnification),
                              this.wall_thickness);
        this.drawVerticalLine((int) (this.cellsize *
                                     this.width *
                                     this.magnification),
                              0,
                              (int) (this.cellsize *
                                     this.height *
                                     this.magnification),
                              this.wall_thickness);

        return(true);
    } // drawBorder() //

    // translates from cell,cell to x,y //
    public int xCell2Pixel(int cell)
    {
        return( (int)(cell * this.cellsize * this.magnification));
    } // xCell2Pixel //
    public int yCell2Pixel(int cell)
    {
        return( (int) ((this.height * this.cellsize * this.magnification) -
                   (this.cellsize * (cell+1) * this.magnification)));
    } // yCell2Pixel //
    // helper functions for just plain drawing... //

    // draw a circle for a robot and a line to indicate its direction //

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -