Пример #1
0
        public void refreshScreen(Grid copyOfGrid)
        {
            if (copyOfGrid == null)
                return;

                foreach (PictureBox pb in elements)
                {
                    Controls.Remove(pb);
                }

                elements.Clear();

                foreach (Car c in copyOfGrid.ListOfCars) //moves every existing car by 1 position
                {
                    if (c != null)//&& c.HasExitedGrid==false && c.HasEnteredGrid==true)
                    {
                        drawCar(c);//draws lights
                        //Invoke(new drawCarHandler(drawCar), new object[] { c });//draws cars in case another thread called it
                    }
                }

                 drawLights(copyOfGrid.Slots);//draws lights
                //Invoke(new drawLightsHandler(drawLights), new object[] { copyOfGrid.Slots });//draws lights in case another thread called it

                //MessageBox.Show("a");
                Invalidate();
        }
Пример #2
0
 /// <summary>
 /// Adds a crossing on a givven position on the grid.
 /// </summary>
 /// <param name="id">Indicates a position on the grid, for instance "A3".</param>
 /// <param name="crossing">The class type of the crossing, either "crossing_1" or "crossing_2".</param>
 /// <returns>Error message, or null if successfull.</returns>
 public string addCrossing(string id, Type crossing)
 {
     if (_grid == null)
         _grid = new Grid();
     if (State == State.Stopped)
     {
         if (_grid.addCrossing(id, crossing))
         {
             _fileHandler.setUnsavedData();
             return "";
         }
         return "Slot " + id + " is not available.";
     }
     else
     {
         return "Stop simulation before adding a crossing.";
     }
 }
Пример #3
0
 /// <summary>
 /// Saves data to file.
 /// </summary>
 /// <param name="grid">Grid object to serialize.</param>
 /// <returns>If save was succesful.</returns>
 public bool saveToFile(Grid grid)
 {
     FileStream fileStream = new FileStream(_path, FileMode.Append);
     try
     {
         BinaryFormatter binaryFormater = new BinaryFormatter();
         binaryFormater.Serialize(fileStream, grid);
         _hasUnsavedData = false;
     }
     catch(Exception)
     {
         return false;
     }
     finally
     {
         fileStream.Close();
     }
     //TO DO: save the object grid onto file
     return true;
 }
Пример #4
0
        /// <summary>
        /// Saves data to file.
        /// </summary>
        /// <param name="grid">Grid object to serialize.</param>
        /// <returns>If save was succesful.</returns>
        public bool saveToFile(Grid grid)
        {
            FileStream fileStream=null;

            try
            {
                fileStream = new FileStream(_path, FileMode.Create);
                BinaryFormatter binaryFormater = new BinaryFormatter();
                _hasUnsavedData = false;

                binaryFormater.Serialize(fileStream, grid);

                fileStream.Close();
                //Grid objectToLoad = (Grid)binaryFormater.Deserialize(fileStream);
                return true;
            }
            catch(Exception)
            {
                if (fileStream != null)
                    fileStream.Close();
                return false;
            }
        }
        /// <summary>
        /// Starts simulation.
        /// </summary>
        /// <returns>Error message, or null if successfull.</returns>
        public string startSimulation()
        {
            try
            {
                if (_state == Traffic_Simulator.State.Stopped)
                {
                    _grid = new Grid();
                    _grid.Slots[0, 0] = new Crossing_1();
                    _grid.Slots[1, 0] = new Crossing_2();

                    _grid.Slots[0, 0].FlowW = 1;
                    _grid.Slots[0, 0].FlowS = 1;
                    _grid.Slots[0, 0].FlowN = 1;

                    _grid.Slots[0, 0].ProbWtoN = 40;
                    _grid.Slots[0, 0].ProbWtoS = 40;
                    _grid.Slots[0, 0].ProbWtoE = 40;

                    _grid.Slots[0, 0].ProbNtoW = 40;
                    _grid.Slots[0, 0].ProbNtoS = 40;
                    _grid.Slots[0, 0].ProbNtoE = 40;

                    _grid.Slots[0, 0].ProbStoN = 40;
                    _grid.Slots[0, 0].ProbStoW = 40;
                    _grid.Slots[0, 0].ProbStoE = 40;

                    _grid.Slots[0, 0].ID = "A0";

                    Crossing_2 c2 = (Crossing_2)_grid.Slots[1, 0];
                    TrafficLight tl;
                    tl._color = c2.LightPedestrian._color;
                    tl._greenLightTime = 20;
                    c2.LightPedestrian = tl;

                    tl._color = c2.LightNtoS._color;
                    tl._greenLightTime = 20;
                    c2.LightNtoS = tl;

                    tl._color = c2.LightWtoSE._color;
                    tl._greenLightTime = 20;
                    c2.LightWtoSE = tl;

                    _grid.Slots[1, 0].FlowS = 0;
                    _grid.Slots[1, 0].FlowE = 0;
                    _grid.Slots[1, 0].FlowN = 0;

                    _grid.Slots[1, 0].ProbEtoN = 40;
                    _grid.Slots[1, 0].ProbEtoS = 40;
                    _grid.Slots[1, 0].ProbEtoW = 40;

                    _grid.Slots[1, 0].ProbStoN = 40;
                    _grid.Slots[1, 0].ProbStoW = 40;
                    _grid.Slots[1, 0].ProbStoE = 40;

                    _grid.Slots[1, 0].ID = "A1";

                    _timer.Interval = _refreshRate;//sets and starts the timer
                    _timer.Elapsed += timerHasTriggered;
                    _timer.Start();
                }
                if (_state == Traffic_Simulator.State.Paused)
                {
                    _timer.Start();
                }
                _state = State.Running;
                return "";
            }
            catch
            {
                return "Couldn't start simulation";
            }
        }
 /// <summary>
 /// Loads a simulation file to the memory.
 /// </summary>
 /// <returns>Error message, or null if successfull.</returns>
 public string load()
 {
     try
     {
         _fileHandler.Path = _gui.filePath(0); // shows load-file window
         _grid = _fileHandler.loadFromFile();
         return "Simulation has been loaded";
     }
     catch
     {
         return "Simulation not be loaded";
     }
 }
Пример #7
0
        /// <summary>
        /// Re-draws everything on the screen with updated values
        /// </summary>
        /// <param name="copyOfGrid">Grid from which the information will be extracted</param>
        public void refreshScreen(Grid copyOfGrid)
        {
            if (copyOfGrid == null)
                return;

            _isReady = false;
            foreach (PictureBox pb in _elements)
            {

                Controls.Remove(pb);
            }

            _elements.Clear();

            if(_controller.State==State.Stopped)
                drawCrossings(copyOfGrid.Slots);

            if (Controller.State != State.Stopped)
            {
                drawLights(copyOfGrid.Slots);//draws lights

                foreach (Car c in copyOfGrid.ListOfCars) //moves every existing car by 1 position
                {
                    if (c != null)//&& c.HasExitedGrid==false && c.HasEnteredGrid==true)
                    {
                        drawCar(c);//draws lights
                    }
                }
            }

            Invalidate();
            _isReady = true;
        }