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

📄 carthread.java

📁 guan yu java duo xian cheng, bing xing ji suan. shu yu bing xing ji suan fan chou
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package traffic;

public class carThread extends Thread {
    /*
     i,j represent the initial location of the car
     direction represent the way this car will go , 0 go straight ,1 turn left ,2 turn right
     threadNum represent the thread id of the car
     */
    private int i, j, direction, id;
    private Master master;
    private int[][] map = new int[20][8];
    private int last_i;
    private int last_j;

    public carThread(Master parent, int i, int j, int direction, int threadNum) {
        this.i = i;
        this.j = j;
        this.direction = direction;
        this.id = threadNum;
        this.master = parent;
    }

    public void getMap() {
        System.out.println("thread [" + id + "] get map");
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 8; j++) {
                map[i][j] = master.getMap(i, j);
            }
        }
        master.threadTag[id] = 1;

    }

    public void setMap(int i, int j, int value) {
        System.out.println("thread [" + id + "] set map");
        master.set_tempMap(i, j, value);
        System.out.println("thread [" + id + "] set threadTag[" + id + "]=2");
        master.threadTag[id] = 2;
        if (master.Verify() == master.threadNum * 2) {
            System.out.println("thread [" + id + "] update map");
            master.updateMap();
            synchronized (this) {
                System.out.println("wake up all thread");
                notifyAll();
            }
        }
    }

    public boolean isFoward() {
        if (i == 0 || i == 1 || i == 4 || i == 5 || i == 8 || i == 9 || i == 12 ||
            i == 13) {
            return true;
        } else {
            return false;
        }
    }

    public boolean outIntersection() {
        if (i == 2 || i == 3 || i == 6 || i == 7 || i == 10 || i == 11 ||
            i == 14 || i == 15) {
            return true;
        } else {
            return false;
        }
    }

    public boolean inIntersection() {
        if (i == 16 || i == 17 || i == 18 || i == 19) {
            return true;
        } else {
            return false;
        }
    }

    public boolean IntersectionHasCar() {
        for (int i = 16; i < 20; i++) {
            for (int j = 0; j < 4; j++) {
                if (this.map[i][j] == 1) {
                    return false;
                }
            }
        }
        return true;
    }

    //车行至路口判断函数
    public boolean canGo() {

        //左转判断

        if (i == 0 && this.direction == 1) {
            if (this.map[19][0] == 0 && this.map[19][1] == 0 &&
                this.master.getLight() == 1) {

                return true;
            } else {

                return false;
            }
        } else if (i == 4 && this.direction == 1) {
            if (this.map[19][3] == 0 && this.map[18][3] == 0 &&
                this.master.getLight() == 3) {
                return true;
            } else {
                return false;
            }
        } else if (i == 8 && this.direction == 1) {
            if (this.map[16][3] == 0 && this.map[16][2] == 0 &&
                this.master.getLight() == 1) {
                return true;
            } else {
                return false;
            }
        } else if (i == 12 && this.direction == 1) {
            if (this.map[16][0] == 0 && this.map[17][0] == 0 &&
                this.master.getLight() == 3) {
                return true;
            } else {
                return false;
            }
        }

        //直行判断
        else if (i == 1 && this.direction == 0) {
            if (this.map[16][1] == 0 && this.map[17][2] == 0 &&
                this.master.getLight() == 2) {

                return true;
            } else {
                return false;
            }
        } else if (i == 5 && this.direction == 0) {
            if (this.map[18][0] == 0 && this.map[17][1] == 0 &&
                this.master.getLight() == 4) {
                return true;
            } else {
                return false;
            }
        } else if (i == 9 && this.direction == 0) {
            if (this.map[19][2] == 0 && this.map[18][1] == 0 &&
                this.master.getLight() == 2) {
                return true;
            } else {
                return false;
            }
        } else if (i == 13 && this.direction == 0) {
            if (this.map[17][3] == 0 && this.map[18][2] == 0 &&
                this.master.getLight() == 4) {
                return true;
            } else {
                return false;
            }
        }

        //右转判断
        else if (i == 1 && this.direction == 2) {
            if (j == 0) {
                if (this.map[19][2] == 0) {
                    return true;
                } else {
                    return false;
                }

            } else if (j == 1) {
                if (this.map[i][0] == 0) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }

        } else if (i == 5 && this.direction == 2) {
            if (j == 0) {
                if (this.map[17][3] == 0) {
                    return true;
                } else {
                    return false;
                }

            } else if (j == 1) {
                if (this.map[i][0] == 0) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }

        }

        else if (i == 9 && this.direction == 2) {
            if (j == 0) {
                if (this.map[16][1] == 0) {
                    return true;
                } else {
                    return false;
                }

            } else if (j == 1) {
                if (this.map[i][0] == 0) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }

        }

        else if (i == 13 && this.direction == 2) {
            if (j == 0) {
                if (this.map[18][0] == 0) {
                    return true;
                } else {
                    return false;
                }
            } else if (j == 1) {
                if (this.map[i][0] == 0) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }

        } else {
            return false;
        }

    }

    public void run() {
        while (true) {
            System.out.println("this is thread [" + id + "]");
            while (true) {
                if (master.threadTag[id] == 0) {
                    break;
                }
            }
            getMap();

            if (isFoward()) { //向路口方向行驶
                if (j != 0) { //未到路口
                    if (map[i][j - 1] == 0) { //前面没有车
                        j--;
                    }

                } else { //到路口
                    if (this.direction == 0 || this.direction == 2) { //直行方向或者右转方向
                        if (this.canGo()) {
                            if (this.i == 1) {
                                i = 19;
                                j = 3;
                                last_i = 1;
                                last_j = 0;
                            } else if (this.i == 5) {
                                i = 16;
                                j = 3;
                                last_i = 5;
                                last_j = 0;
                            } else if (this.i == 9) {
                                i = 16;

⌨️ 快捷键说明

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