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

📄 roadcourse.java

📁 Java程序设计(美) David D. Riley著 机械工业出版社 书籍配套 代码
💻 JAVA
字号:
import javax.swing.JFrame;import java.awt.Color;public class RoadCourse  {    private char[][] course = {{' ', ' ', ' ', ' ', ' ', 'X', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},                              {' ', ' ', ' ', ' ', ' ', 'X', ' ', ' ', ' ', ' ', ' ', ' ', ' '},                              {'X', 'X', 'X', ' ', ' ', 'X', ' ', ' ', 'X', ' ', ' ', ' ', ' '},                              {' ', ' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X', ' ', ' ', ' ', ' '},                              {' ', ' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X', 'X', 'X', ' ', ' '},                              {' ', ' ', 'X', 'X', 'X', ' ', ' ', ' ', 'X', ' ', ' ', ' ', ' '},                              {' ', ' ', 'X', 'X', ' ', ' ', ' ', ' ', 'X', ' ', ' ', ' ', ' '},                              {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X', ' ', ' ', ' ', 'X'},                              {' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X', 'X', 'X', ' ', ' ', ' '},                              {' ', ' ', 'X', 'X', 'X', ' ', ' ', 'X', ' ', 'X', ' ', ' ', ' '}};                                  private boolean[][] hasRobot;                                private JFrame window;    public RoadCourse() {        Barrier barrier;        window = new JFrame("Robot Driving Road Course");        window.setBounds(20, 20, 13*72, 10*72);        window.getContentPane().setLayout(null);        window.setVisible(true);        window.getContentPane().setBackground(Color.white);        hasRobot = new boolean[10][13];        for (int row=0;  row != 10;  row++) {            for (int col=0;  col != 13;  col++) {                hasRobot[row][col] = false;                if (course[row][col] != ' ') {                    barrier = new Barrier();                    barrier.setLocation(col*72, row*72);                    window.getContentPane().add(barrier, 0);                }            }        }        window.getContentPane().repaint();    }        /** pre:    isAvailable(0, 0)  and  robo.getParent()==null     *  post:   robo is placed on window and robo.getX()==0 and robo.getY()==0     *          and  hasRobot[0][0]  and  hasRobot[0][1]     *          and  hasRobot[1][0]  and  hasRobot[1][1]      */    public void placeRobotOnRoad(Robot robo)  {        robo.setLocation(0, 0);        window.getContentPane().add(robo);        robo.repaint();        hasRobot[0][0] = true;        hasRobot[0][1] = true;        hasRobot[1][0] = true;        hasRobot[1][1] = true;    }        /** post:   result ==  1 <= r <= 8  and  0 <= c <= 11     *                     and  not hasRobot[r-1][c]   and   not hasRobot[r-1][c+1]     *                     and  course[r-1][c]==' '  and course[r-1][c+1]==' '      */    public boolean canMoveUp(int r, int c)  {        return 1<=r && r<=8 && 0<=c && c<=11                && !hasRobot[r-1][c] && !hasRobot[r-1][c+1]                && course[r-1][c]==' ' && course[r-1][c+1]==' ';    }        /** post:   result ==  0 <= r <= 8  and  0 <= c <= 10     *                     and  not hasRobot[r][c+2]   and   not hasRobot[r+1][c+2]      *                     and  course[r][c+2]==' '  and course[r+1][c+2]==' '      */    public boolean canMoveRight(int r, int c)  {        return 0<=r && r<=8 && 0<=c && c<=10                && !hasRobot[r][c+2] && !hasRobot[r+1][c+2]                && course[r][c+2]==' ' && course[r+1][c+2]==' ';    }        /** post:   result ==  0 <= r <= 7  and  0 <= c <= 11     *                     and  not hasRobot[r+2][c]   and   not hasRobot[r+2][c+1]      *                     and  course[r+2][c]==' '  and course[r+2][c+1]==' '      */    public boolean canMoveDown(int r, int c)  {        return 0<=r && r<=7 && 0<=c && c<=11                && !hasRobot[r+2][c] && !hasRobot[r+2][c+1]                && course[r+2][c]==' ' && course[r+2][c+1]==' ';    }        /** post:   result ==  0 <= r <= 8  and  1 <= c <= 11     *                     and  not hasRobot[r][c-1]   and   not hasRobot[r+1][c-1]      *                     and  course[r][c-1]==' '  and course[r+1][c-1]==' '      */    public boolean canMoveLeft(int r, int c)  {        return 0<=r && r<=8 && 1<=c && c<=11                && !hasRobot[r][c-1] && !hasRobot[r+1][c-1]                && course[r][c-1]==' ' && course[r+1][c-1]==' ';    }        /** pre:    isAvailable(r, c)  and  robo.getParent()==null     *  post:   not hasRobot[oldR][oldC]  and  not hasRobot[oldR][oldC+1]     *          and  not hasRobot[oldR+1][oldC]  and  not hasRobot[oldR+1][oldC+1]     *          and hasRobot[r][c]  and  hasRobot[r][c+1]     *          and hasRobot[r+1][c]  and  hasRobot[r+1][c+1]      */    public void moveRobotFromTo(int oldR, int oldC, int r, int c)  {        hasRobot[oldR][oldC] = false;        hasRobot[oldR][oldC+1] = false;        hasRobot[oldR+1][oldC] = false;        hasRobot[oldR+1][oldC+1] = false;        hasRobot[r][c] = true;        hasRobot[r][c+1] = true;        hasRobot[r+1][c] = true;        hasRobot[r+1][c+1] = true;    }        }      

⌨️ 快捷键说明

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