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

📄 master.java

📁 guan yu java duo xian cheng, bing xing ji suan. shu yu bing xing ji suan fan chou
💻 JAVA
字号:
package traffic;

import example.*;
import java.math.*;

public class Master {
    private NotHelloWorldPanel panel;

    public Master(NotHelloWorldPanel parentPanel) {
        this.panel = parentPanel;
    }

    private int[][] map = new int[20][10];
    /*a[0][]--a[15][] road  a[][8]=1 block a[][8]=0 ok  a[][9]=1 have car in entrance
     * a[16][]--a[19][] center*/
    private int light = 1;
    /*light=2     0 1 8 9 直行
     * light=1    0 1 8 9 左转
     *light=3    4 5 12 13 左转
     *light=4    4 5 12 13 直行
     * */
    public int threadNum = 0;
    public int[] threadTag;

    private int[][] tempMap = new int[20][8];

    public void initial_Tag() {
        for (int i = 0; i < threadNum; i++) {
            threadTag[i] = 0;
        }
    }

    /*This function is used to verify the statu of the map data
      sum=threadNum*2 can update*/
    public int Verify() {
        int sum = 0;
        for (int i = 0; i < threadNum; i++) {
            sum = sum + threadTag[i];
        }
        System.out.println("the sum is : " + sum);
        return sum;
    }

    public int getMap(int i, int j) {
        return map[i][j];
    }

    public void set_tempMap(int i, int j, int value) {
        tempMap[i][j] = value;
    }

    public void updateMap() {
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 8; j++) {
                map[i][j] = tempMap[i][j];
                //System.out.println("map[" + i + "][" + j + "]=" + map[i][j]);
            }
        }
        panel.updateMap(map, this.getLight());
        this.initial_Tag();

        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 8; j++) {
                tempMap[i][j] = 0;

            }
        }

    }

    /*initial map and tempMap*/
    public void initialMap() {
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 8; j++) {
                map[i][j] = 0;
                tempMap[i][j] = 0;
            }
        }
    }

    /* public void initialLight() {
         Traffic_light traffic_light = new Traffic_light(this);
         traffic_light.start();
     }*/

    public void process() {
        System.out.println("initial map");
        this.initialMap();
        System.out.println("initial light");
        // this.initialLight();

        Traffic_light traffic_light = new Traffic_light(this);
        traffic_light.start();

        double j = Math.random();
        threadNum = (int) (j * 1000) % 15;
        System.out.println("the number of the car is :" + threadNum);
        threadTag = new int[threadNum];
        System.out.println("initial Tag");
        this.initial_Tag();
        this.createThread();
    }

    public synchronized int getLight() {
        return this.light;
    }

    public synchronized void setLight(int i) {
        this.light = i;
    }

    public void createThread() {
        int[][] map = new int[20][10];
        int direction = 0;
        int x = 0, y = 0;
        int temp;
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 10; j++) {
                map[i][j] = 0;
            }
        }
        double j = Math.random();

        System.out.println("the number of the car is :" + threadNum);
        carThread[] car = new carThread[threadNum];
        for (int i = 0; i < threadNum; i++) {
            //产生车线程行驶方向
            j = Math.random();
            direction = (int) (j * 1000) % 3;
            //产生具体线程
            if (direction == 0 || direction == 2) {
                while (true) {
                    j = Math.random();
                    temp = (int) (j * 1000) % 4;
                    x = 1 + temp * 4;
                    j = Math.random();
                    y = (int) (j * 1000) % 8;
                    if (map[x][y] == 0) {
                        break;
                    }
                }
            } else if (direction == 1) {
                while (true) {
                    j = Math.random();
                    temp = (int) (j * 1000) % 4;
                    x = temp * 4;
                    j = Math.random();
                    y = (int) (j * 1000) % 8;
                    if (map[x][y] == 0) {
                        break;
                    }
                }
            }
            System.out.println("tread[" + i + "]: (" + x + "," + y + "," +
                               direction + "," + i + ")");
            car[i] = new carThread(this, x, y, direction, i);
            car[i].start();
        }
    }
}

⌨️ 快捷键说明

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