Package trafficjams.model.classes

Examples of trafficjams.model.classes.CrossRoad


        //создаем перекрестки и дороги ввиде сетки, уже смещенной
        for (int i = 0 ; i<crossCount; ++i){
            if (i==0){
                for (int j = 0 ; j < crossCount; ++j){
                    CrossRoad currCross = new CrossRoad(retVal);
                    l_crossRoads.add(currCross);
                    currCross.setCoord(new Point(gorLength*i + gorLength*rand(), vertLength*j + vertLength * rand()));
                    a_crossRoads[i][j] = currCross;
                    if (j==0){
                        continue;
                    }
                    r = new Road(retVal);
                    roads.add(r);
                    r.setFCross(a_crossRoads[i][j-1]);
                    r.setLCross(currCross);

                    ((CrossRoad)a_crossRoads[i][j-1]).setRoadForDirection(r, right);
                    currCross.setRoadForDirection(r, left);

                }
                continue;
            }

            for (int j = 0 ; j < crossCount; ++j){
                CrossRoad currCross = new CrossRoad(retVal);
                l_crossRoads.add(currCross);
                currCross.setCoord(new Point(gorLength*i + gorLength*rand(), vertLength*j + vertLength * rand()));
                a_crossRoads[i][j] = currCross;
                r = new Road(retVal);
                roads.add(r);
                r.setFCross(a_crossRoads[i-1][j]);
                r.setLCross(currCross);
                ((CrossRoad)a_crossRoads[i-1][j]).setRoadForDirection(r, down);
                currCross.setRoadForDirection(r, up);
                if (j==0){
                    continue;
                }
                r = new Road(retVal);
                roads.add(r);
                r.setFCross(a_crossRoads[i][j-1]);
                r.setLCross(currCross);

                ((CrossRoad)a_crossRoads[i][j-1]).setRoadForDirection(r, right);
                currCross.setRoadForDirection(r, left);

            }
        }

        //убиваем координаты выходящие за границы(выставляем в границы)
        this.removeNegatives(l_crossRoads);

        //выбираем дороги для уничтожения
        ArrayList<IRoad> toDelete = new ArrayList<IRoad>();
        for (IRoad __r : roads){
            if (Math.random()<Numbers.percent){
                toDelete.add(__r);
            }
        }

        //удаляем дороги
        for (IRoad __r : toDelete){
            if (!roads.remove(__r))
                throw new Exception("дорога не удалилась!");
            Road _r = (Road) __r;
            ((CrossRoad)_r.getFCross()).deleteRoad(_r);
            ((CrossRoad)_r.getLCross()).deleteRoad(_r);
        }

        //удаляем перекрестки в которые входят 0 или 2 дороги (и соединяем дорогий то что разъединилось)
        for (int i = 0 ; i<crossCount ; ++i){
            for (int j = 0 ; j<crossCount; ++j){
                CrossRoad cr = (CrossRoad) a_crossRoads[i][j];
                if (cr.getType()  == 0){
                    if (!l_crossRoads.remove(a_crossRoads[i][j]))
                        throw new Exception("перекресток не удалился!");
                    a_crossRoads[i][j] = null;
                }
                if (cr.getType()  == 2){
                    if (!l_crossRoads.remove(a_crossRoads[i][j]))
                        throw new Exception("перекресток не удалился!");
                    a_crossRoads[i][j] = null;
                    IRoad[] rd = cr.get2Roads();
                    CrossRoad c0 = (CrossRoad)((Road)rd[0]).getOtherEnd(cr);
                    CrossRoad c1 = (CrossRoad)((Road)rd[1]).getOtherEnd(cr);

                    if (!(roads.remove(rd[0]) && roads.remove(rd[1])))
                        throw new Exception("road remove error");

                    IRoad newRoad = this.createConnector(c0,c1);
                    roads.add(newRoad);
                    ((CrossRoad) c0).setRoadForDirection(newRoad, c0.deleteRoad(rd[0]));
                    ((CrossRoad) c1).setRoadForDirection(newRoad, c1.deleteRoad(rd[1]));
                }
            }
        }

        //проверяем на связность
        if (!this.isCorrect(l_crossRoads, roads, tryCount)){
            return this.makeRoadMap(crossCount,autoCount,--tryCount);
        }

        //проверяем перекрестки и инициализируем очереди
        for (ICrossRoad iCrossRoad: l_crossRoads){
            CrossRoad crossRoad = (CrossRoad) iCrossRoad;
            if (crossRoad.getType()==2){
                return this.makeRoadMap(crossCount, autoCount, --tryCount);
            }
            crossRoad.initQueues();
        }

        ///выставляем длины дорог
        float maxLength = 0;
        float minLength = Math.max(Numbers.mapHeight, Numbers.mapWidth);
View Full Code Here


        }
        return true;
    }

    private void checkBfs(ArrayList<ICrossRoad> crosses, HashSet<CrossRoad> register) throws Exception {
        CrossRoad cr = (CrossRoad) crosses.get(0);
        if (cr != null){
            register.add(cr);
            _checkBfs(cr, register);
        }
    }
View Full Code Here

    }

    private void _checkBfs(CrossRoad cr, HashSet<CrossRoad> register) throws Exception {
        for (IRoad r : cr.getRoads()){
            if (r != null){
                CrossRoad _cr = (CrossRoad)((Road) r).getOtherEnd((ICrossRoad)cr);
                if (!register.contains(_cr)){
                    register.add(_cr);
                    this._checkBfs(_cr, register);
                }
            }
View Full Code Here

      return Math.max(a,c) <= Math.min(b,d);
    }

    private void removeNegatives(ArrayList<ICrossRoad> l_crossRoads) {
        for (ICrossRoad _cr : l_crossRoads){
            CrossRoad cr = (CrossRoad) _cr;
            Point p = cr.getCoord();
            float d = 0 ;
            if (p.getX()<0){
               // p.setX(Math.abs(p.getX()));
                p.setX(0);
            }
            if (p.getY()<0){
                //p.setY(Math.abs(p.getY()));
                p.setY(0);
            }
            if (p.getX()>Numbers.mapWidth){
                //p.setX(2*Numbers.mapWidth - p.getX());
                p.setX(0);
            }
            if (p.getY()>Numbers.mapWidth){
               //p.setY((2*Numbers.mapHeight-p.getY()));
               p.setY(0);
            }
            cr.setCoord(p);
        }
    }
View Full Code Here

TOP

Related Classes of trafficjams.model.classes.CrossRoad

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.