Ejemplo n.º 1
0
        private void calculateIntersection(Street[] streets)
        {
            switch (this.Street.Position)
            {
                case Direction.North:
                    if (this.Turn == Direction.East)
                    {
                        _path.Add(new int[3] { 4, 1, 0 });
                        _path.Add(new int[3] { 4, 2, 1 });
                        _path.Add(new int[3] { 4, 2, 2 });
                    }
                    if (this.Turn == Direction.South)
                    {
                        _path.Add(new int[3] { 4, 0, 0 });
                        _path.Add(new int[3] { 4, 0, 1 });
                        _path.Add(new int[3] { 4, 0, 2 });
                    }
                    if (this.Turn == Direction.West)
                    {
                        _path.Add(new int[3] { 4, 0, 0 });
                    }
                    return;

                case Direction.East:
                    if (this.Turn == Direction.West)
                    {
                        _path.Add(new int[3] { 4, 2, 0 });
                        _path.Add(new int[3] { 4, 1, 0 });
                        _path.Add(new int[3] { 4, 0, 0 });
                    }
                    if (this.Turn == Direction.South)
                    {
                        _path.Add(new int[3] { 4, 2, 1 });
                        _path.Add(new int[3] { 4, 1, 2 });
                        _path.Add(new int[3] { 4, 0, 2 });
                    }
                    if (this.Turn == Direction.North)
                    {
                        _path.Add(new int[3] { 4, 2, 0 });
                    }
                    return;

                case Direction.South:

                    if (this.Turn == Direction.North)
                    {
                        _path.Add(new int[3] { 4, 2, 2 });
                        _path.Add(new int[3] { 4, 2, 1 });
                        _path.Add(new int[3] { 4, 2, 0 });
                    }
                    if (this.Turn == Direction.West)
                    {
                        _path.Add(new int[3] { 4, 1, 2 });
                        _path.Add(new int[3] { 4, 0, 1 });
                        _path.Add(new int[3] { 4, 0, 0 });
                    }
                    if (this.Turn == Direction.East)
                    {
                        _path.Add(new int[3] { 4, 2, 2 });
                    }
                    return;

                case Direction.West:
                    if (this.Turn == Direction.East)
                    {
                        _path.Add(new int[3] { 4, 0, 2 });
                        _path.Add(new int[3] { 4, 1, 2 });
                        _path.Add(new int[3] { 4, 2, 2 });
                    }
                    if (this.Turn == Direction.North)
                    {
                        _path.Add(new int[3] { 4, 0, 1 });
                        _path.Add(new int[3] { 4, 1, 0 });
                        _path.Add(new int[3] { 4, 2, 0 });
                    }
                    if (this.Turn == Direction.South)
                    {
                        _path.Add(new int[3] { 4, 0, 2 });
                    }
                    return;
            }
        }
Ejemplo n.º 2
0
 private void calculateExit(Street[] streets)
 {
     _path.Add(new int[3] { (int)this.Turn, 2, 2 });
     _path.Add(new int[3] { (int)this.Turn, 2, 1 });
     _path.Add(new int[3] { (int)this.Turn, 2, 0 });
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Moves the car to the next position.
        /// </summary>
        /// <returns>returns true if the car moves</returns>
        public bool move()
        {
            Street[] streets = new Street[5];
            Crossing cr = this.Crossing;
            streets[0] = this.Crossing.StreetN;
            streets[1] = this.Crossing.StreetE;
            streets[2] = this.Crossing.StreetS;
            streets[3] = this.Crossing.StreetW;
            streets[4] = this.Crossing.Intersection;

            nextSlot++;//increments next slot
            if (nextSlot > _path.Count-1)//if car is entering a crossin or leaving the grid
            {
                Crossing tempCrossing = this.Crossing;

                if (HasEnteredGrid)//gets new crossing
                {
                    this._crossing = getNextCrossing();
                }
                else//if car is entering the grid for the first time
                {

                    calculateTurn();
                    calculateEntrance(streets);
                    calculateIntersection(streets);
                    calculateExit(streets);

                }

                if (this._crossing == null)//if car is leaving the grid
                {
                    this.HasExitedGrid = true;
                    this.Street.Lanes[this.StreetIndex[0]][this.StreetIndex[1]] = null; //clears the cars last position
                    this.StreetIndex[0] = -1;
                    this.StreetIndex[1] = -1;
                    _path = null;
                    nextSlot = 0;
                    Street = null;
                    return true;
                }
                else//if car is entering a new crossing
                {
                    if (this.Street.Position == this.Turn && this.HasEnteredGrid)//adjust car street
                    {
                        streets[0] = this.Crossing.StreetN;
                        streets[1] = this.Crossing.StreetE;
                        streets[2] = this.Crossing.StreetS;
                        streets[3] = this.Crossing.StreetW;
                        streets[4] = this.Crossing.Intersection;

                        Direction tempTurn = this.Turn;
                        Direction tempDir = this.Direction;
                        int[] lastPosition = this._path[_path.Count - 1];
                        Street tempStreet = this.Street;//holds old value of street

                        this.Street = streets[((int)this.Turn + 2) % 4];

                        _path.Clear();
                        nextSlot = 0;

                        calculateTurn();
                        calculateEntrance(streets);
                        calculateIntersection(streets);
                        calculateExit(streets);

                        int str = _path[nextSlot][0], lane = _path[nextSlot][1], pos = _path[nextSlot][2];

                        if (streets[str].Lanes[lane][pos] != null)//if next position is NOT available, returns old values
                        {
                            this.Street = tempStreet;
                            this._crossing = tempCrossing;
                            List<int[]> oldPath = new List<int[]>();
                            oldPath.Add(lastPosition);
                            this._path = oldPath;
                            this.Turn = tempTurn;
                            this.Direction = tempDir;

                            return false;
                        }
                        else
                        {
                            tempStreet.Lanes[StreetIndex[0]][StreetIndex[1]] = null;//leaves previous position

                        }
                    }
                }
            }//end if(car is entering a new crosisng/leaving grid)

            int str2 = _path[nextSlot][0], lane2 = _path[nextSlot][1], pos2 = _path[nextSlot][2];

            if (streets[str2].Lanes[lane2][pos2] == null  && (nextSlot != 3 || checkLights()))//if next position is available and light is green
            {

                if (HasEnteredGrid == false)
                    this.HasEnteredGrid = true;
                else
                    Street.Lanes[StreetIndex[0]][StreetIndex[1]] = null;//leaves previous position

                streets[str2].Lanes[lane2][pos2] = this;//enters new position

                StreetIndex[0] = lane2;
                StreetIndex[1] = pos2;

                //updates the street
                if (nextSlot == 3)
                {
                    this.Street = this.Crossing.Intersection;
                }
                if ((nextSlot == 4 && _path.Count == 7) || (nextSlot == 6 && _path.Count == 9))//if car left intersection zone
                {
                    this.Street = streets[(int)this.Turn];
                    this.Direction = this.Turn;
                }

            }
            else//if car didnt move
            {
                nextSlot--;
                return false;
            }

            return false;
        }
Ejemplo n.º 4
0
        private void calculateEntrance(Street[] streets)
        {
            //Dir: N=0, E=1, S=2, W=3

            _path.Add(new int[3] { (int)this.Street.Position, Convert.ToInt32(((int)this.Street.Position + 1) % 4 == (int)this.Turn), 0 });//pos Dir,0,0
            _path.Add(new int[3] { (int)this.Street.Position, Convert.ToInt32(((int)this.Street.Position + 1) % 4 == (int)this.Turn), 1 });
            _path.Add(new int[3] { (int)this.Street.Position, Convert.ToInt32(((int)this.Street.Position + 1) % 4 == (int)this.Turn), 2 });
        }
Ejemplo n.º 5
0
        public virtual bool reset()
        {
            //clears all streets (remove all cars)
               _intersection = new Street(this.GetType(), Direction.Center);
            _streetN = new Street(this.GetType(), Direction.North);
            _streetE = new Street(this.GetType(), Direction.East);
            _streetS = new Street(this.GetType(), Direction.South);
            _streetW = new Street(this.GetType(), Direction.West);

               //clears all lights
            _lightEtoNW._color = Color.Gray;
            _lightWtoSE._color = Color.Gray;
            _lightEtoS._color = Color.Gray;
            _lightWtoN._color = Color.Gray;

               _state = 0;
               _tickCount = 0;
               return true;
        }