コード例 #1
0
ファイル: Car.cs プロジェクト: Vyctrus/SO2_crossing
        public Car(CarPos carPos, CarDirection carDirection, String carGraphic, Program program1, Road carRoad, SemaphoreSlim godOfRoad)
        {
            program = program1;
            road    = carRoad;
            roadGod = godOfRoad;
            speed   = randSpeed.Next(100, 500);
            carDir  = carDirection;
            switch (carDirection)
            {
            case CarDirection.FORWARD:
                switch (carPos)
                {
                case CarPos.TOP:
                    carDestination = CarPos.BOTTOM;
                    break;

                case CarPos.RIGHT:
                    carDestination = CarPos.LEFT;
                    break;

                case CarPos.BOTTOM:
                    carDestination = CarPos.TOP;
                    break;

                case CarPos.LEFT:
                    carDestination = CarPos.RIGHT;
                    break;
                }
                break;

            case CarDirection.TURN_RIGHT:
                switch (carPos)
                {
                case CarPos.TOP:
                    carDestination = CarPos.LEFT;
                    break;

                case CarPos.RIGHT:
                    carDestination = CarPos.TOP;
                    break;

                case CarPos.BOTTOM:
                    carDestination = CarPos.RIGHT;
                    break;

                case CarPos.LEFT:
                    carDestination = CarPos.BOTTOM;
                    break;
                }
                break;

            case CarDirection.TURN_LEFT:
                switch (carPos)
                {
                case CarPos.TOP:
                    carDestination = CarPos.RIGHT;
                    break;

                case CarPos.RIGHT:
                    carDestination = CarPos.BOTTOM;
                    break;

                case CarPos.BOTTOM:
                    carDestination = CarPos.LEFT;
                    break;

                case CarPos.LEFT:
                    carDestination = CarPos.TOP;
                    break;
                }
                break;
            }

            switch (carDestination)
            {
            case CarPos.TOP:
                carColor = new Terminal.Gui.Attribute(Color.BrightGreen, Color.Black);
                break;

            case CarPos.RIGHT:
                carColor = new Terminal.Gui.Attribute(Color.BrightYellow, Color.Black);
                break;

            case CarPos.BOTTOM:
                carColor = new Terminal.Gui.Attribute(Color.BrightRed, Color.Black);
                break;

            case CarPos.LEFT:
                carColor = new Terminal.Gui.Attribute(Color.BrightBlue, Color.Black);
                break;
            }
            //pozniej zalezne od kierunku
            graphic       = carGraphic;
            positionMutex = new Mutex();
            lockPosition();
            pos_X       = road.getStartPointX(carPos);
            pos_Y       = road.getStartPointY(carPos);
            posOrigin_X = pos_X;
            posOrigin_Y = pos_Y;
            unlockPosition();
            switch (carPos)
            {
            case CarPos.TOP:
                carRot = CarRotation.DOWN;
                break;

            case CarPos.RIGHT:
                carRot = CarRotation.LEFT;
                break;

            case CarPos.BOTTOM:
                carRot = CarRotation.UP;
                break;

            case CarPos.LEFT:
                carRot = CarRotation.RIGHT;
                break;
            }
        }
コード例 #2
0
        public void ProgramGUI()
        {
            simRoad = new Road();
            //StartThreads();
            Thread generator = new Thread(new ThreadStart(GenerateCarsProc));

            generator.Start();
            Application.Init();
            var top = Application.Top;
            // Creates a menubar f9
            var menu = new MenuBar(new MenuBarItem[] {
                new MenuBarItem("_F9 Menu", new MenuItem [] {
                    new MenuItem("_Quit", "", () => { top.Running = false; }),
                    new MenuItem("_StopCars", "", () => { setPrun(false); }),
                }),
            });

            top.Add(menu);
            //Simulation window
            var win = new Window("Crossing Problem")
            {
                X = 0, Y = 1, Width = Dim.Fill(), Height = Dim.Fill()
            };

            //dodanie poszczegolnych elementow
            addBoundaries(win);

            // var testLabel = new Label(myCar.getGraphic())
            // {
            //     X = myCar.getPosX(),
            //     Y = myCar.getPosY()
            // };
            //win.Add(testLabel);
            DateTime start = DateTime.Now;

            //Main loop, update data here
            bool timer(MainLoop caller)
            {
                win.RemoveAll();
                addBoundaries(win);
                //get dangerous data from???
                //updateInfo(0, pb_0, philosopherStatus_0);
                // updateForks(0, forkLabel_0);
                //powinno byc zabezpieczone!

                //remove not existing/dead cars
                foreach (Car singleCar in cars.ToArray())
                {
                    if (singleCar.checkIfCarExists() == false)
                    {
                        simRoad.pushCharacter(singleCar.getGraphic());
                        cars.Remove(singleCar);
                    }
                }

                //print existing ones
                foreach (Car singleCar in cars.ToArray())
                {
                    var singleCarLabel = new Label(singleCar.getGraphic())
                    {
                        X           = singleCar.getPosX(),
                        Y           = singleCar.getPosY(),
                        ColorScheme = new ColorScheme()
                        {
                            Normal = singleCar.getCarColor()
                        },
                    };
                    win.Add(singleCarLabel);
                }

                //nieodpowiednie nazwy zmiennych, odpowiednie dzialanie
                int ccNumber = 0;

                simRoad.getCrossingCarsNumberMutex().WaitOne();
                ccNumber = simRoad.getCrossingCarsNumber();
                simRoad.getCrossingCarsNumberMutex().ReleaseMutex();
                var ccNumberLabel = new Label("Liczba kolizji: " + ccNumber)
                {
                    X = 28,
                    Y = 5
                };

                win.Add(ccNumberLabel);
                DateTime end       = DateTime.Now;
                TimeSpan ts        = (end - start);
                var      timeLabel = new Label("Czas symulacji: " + ts.Duration().TotalSeconds + "[s]")//+ " : "+ ts.Seconds
                {
                    X = 28,
                    Y = 7
                };

                win.Add(timeLabel);
                //update graphics + generate new cars?
                return(true);
            }

            Application.MainLoop.AddTimeout(TimeSpan.FromMilliseconds(100), timer);
            top.Add(win);
            Application.Run();
        }