Ejemplo n.º 1
0
        private bool MoveCar(int[,] carpark, CarPos pos, MoveType moveType)
        {
            _step.Push(moveType);

            _gone[pos.Y, pos.X] = Walked;

            if (IsEndFloor(carpark, pos))
            {
                int steps = carpark.GetLength(1) - pos.X - 1;
                for (int i = 0; i < steps; i++)
                {
                    _step.Push(MoveType.Right);
                }
                return(true);
            }

            if (TryDown(carpark, pos))
            {
                return(true);
            }

            if (TryRight(carpark, new CarPos(pos.X + 1, pos.Y)))
            {
                return(true);
            }

            if (TryLeft(carpark, new CarPos(pos.X - 1, pos.Y)))
            {
                return(true);
            }

            _step.Pop();
            return(false);
        }
Ejemplo n.º 2
0
 bool CanDown(int[,] carpark, CarPos pos)
 {
     if (!IsInCarparkRange(carpark, pos))
     {
         return(false);
     }
     return(GetItem(carpark, pos) == Staircases);
 }
Ejemplo n.º 3
0
 bool IsEndFloor(int[,] carpark, CarPos pos)
 {
     for (int x = 0; x < carpark.GetLength(1); x++)
     {
         if (carpark[pos.Y, x] != 0)
         {
             return(false);
         }
     }
     return(true);
 }
Ejemplo n.º 4
0
 bool CanMove(int[,] carpark, CarPos pos)
 {
     if (!IsInCarparkRange(carpark, pos))
     {
         return(false);
     }
     if (carpark[pos.Y, pos.X] == Staircases)
     {
         return(true);
     }
     if (_gone[pos.Y, pos.X] == Walked)
     {
         return(false);
     }
     return(GetItem(carpark, pos) == 0);
 }
Ejemplo n.º 5
0
        private CarPos FindCarStartPos(int[,] carpark)
        {
            var startPos = new CarPos(0, 0);

            for (int y = 0; y < carpark.GetLength(0); y++)
            {
                for (int x = 0; x < carpark.GetLength(1); x++)
                {
                    if (carpark[y, x] == ParkingPlace)
                    {
                        startPos.X = x;
                        startPos.Y = y;
                        return(startPos);
                    }
                }
            }
            return(startPos);
        }
Ejemplo n.º 6
0
        public int getStartPointY(CarPos carPos)
        {
            switch (carPos)
            {
            case CarPos.TOP:
                return(startFields[0, 1]);

            case CarPos.RIGHT:
                return(startFields[1, 1]);

            case CarPos.BOTTOM:
                return(startFields[2, 1]);

            case CarPos.LEFT:
                return(startFields[3, 1]);

            default:
                return(99);
            }
        }
Ejemplo n.º 7
0
 bool IsInCarparkRange(int[,] carpark, CarPos pos)
 {
     if (pos.X < 0)
     {
         return(false);
     }
     if (pos.X >= carpark.GetLength(1))
     {
         return(false);
     }
     if (pos.Y < 0)
     {
         return(false);
     }
     if (pos.Y >= carpark.GetLength(0))
     {
         return(false);
     }
     return(true);
 }
Ejemplo n.º 8
0
        void correctRotation(int pos_X, int pos_Y, CarPos carDestination)
        {
            switch (carDestination)
            {
            case CarPos.TOP:
                if (pos_X == 12 && pos_Y == 12)
                {
                    //niemozliwe
                }
                if (pos_X == 13 && pos_Y == 12)
                {
                    carRot = CarRotation.UP;
                }
                if (pos_X == 12 && pos_Y == 13)
                {
                    carRot = CarRotation.RIGHT;
                }
                if (pos_X == 13 && pos_Y == 13)
                {
                    carRot = CarRotation.UP;
                }
                break;

            case CarPos.RIGHT:
                if (pos_X == 12 && pos_Y == 12)
                {
                    carRot = CarRotation.DOWN;
                }
                if (pos_X == 13 && pos_Y == 12)
                {
                    //niemozliwe
                }
                if (pos_X == 12 && pos_Y == 13)
                {
                    carRot = CarRotation.RIGHT;
                }
                if (pos_X == 13 && pos_Y == 13)
                {
                    carRot = CarRotation.RIGHT;
                }
                break;

            case CarPos.LEFT:
                if (pos_X == 12 && pos_Y == 12)
                {
                    carRot = CarRotation.LEFT;
                }
                if (pos_X == 13 && pos_Y == 12)
                {
                    carRot = CarRotation.LEFT;
                }
                if (pos_X == 12 && pos_Y == 13)
                {
                    //niemozliwe
                }
                if (pos_X == 13 && pos_Y == 13)
                {
                    carRot = CarRotation.UP;
                }
                break;

            case CarPos.BOTTOM:
                if (pos_X == 12 && pos_Y == 12)
                {
                    carRot = CarRotation.DOWN;
                }
                if (pos_X == 13 && pos_Y == 12)
                {
                    carRot = CarRotation.LEFT;
                }
                if (pos_X == 12 && pos_Y == 13)
                {
                    carRot = CarRotation.DOWN;
                }
                if (pos_X == 13 && pos_Y == 13)
                {
                    //niemożliwe
                }
                break;
            }
        }
Ejemplo n.º 9
0
        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;
            }
        }
Ejemplo n.º 10
0
        public static void Resolver(string path)
        {
            StreamWriter escritor = new StreamWriter(string.Concat(path, "/", "current.out"));
            StreamReader lector   = new StreamReader(string.Concat(path, "/", "current.in"));
            int          T        = int.Parse(lector.ReadLine());

            for (int t = 0; t < T; ++t)
            {
                string[] data = lector.ReadLine().Split(' ');
                double   D    = double.Parse(data[0].Replace(".", ","));
                int      N    = int.Parse(data[1]);
                int      A    = int.Parse(data[2]);

                CarPos[] positions = new CarPos[N];
                for (int p = 0; p < N; p++)
                {
                    data         = lector.ReadLine().Split(' ');
                    positions[p] = new CarPos(double.Parse(data[0].Replace(".", ",")), double.Parse(data[1].Replace(".", ",")));
                }

                data = lector.ReadLine().Split(' ');

                escritor.WriteLine(string.Concat("Case #", t + 1, ":"));
                for (int i = 0; i < A; i++)
                {
                    double a = double.Parse(data[i].Replace(".", ","));

                    double timeElapsed       = 0;
                    double myCarLastPosition = 0;
                    double myCarLastSpeed    = 0;
                    for (int p = 1; p < N; p++)
                    {
                        double myCarTime    = TimeFromX0ToXf(myCarLastPosition, positions[p].X, myCarLastSpeed, a);
                        double otherCarTime = positions[p].T - positions[p - 1].T;

                        if (myCarTime <= otherCarTime)
                        {
                            if (positions[p].X >= D)
                            {
                                double otherCarSpeed = (positions[p].X - positions[p - 1].X) / (positions[p].T - positions[p - 1].T);
                                timeElapsed      += Math.Max(TimeFromX0ToXf(myCarLastPosition, D, myCarLastSpeed, a), (D - positions[p - 1].X) / otherCarSpeed);
                                myCarLastPosition = D;
                                break;
                            }
                            myCarLastSpeed    = FinalSpeed(myCarLastPosition, positions[p].X, myCarLastSpeed, a);
                            myCarLastPosition = positions[p].X;
                        }
                        else
                        {
                            double positionIWillBe = myCarLastSpeed * otherCarTime + (a / 2) * otherCarTime * otherCarTime;
                            if (positionIWillBe >= D)
                            {
                                timeElapsed      += TimeFromX0ToXf(myCarLastPosition, D, myCarLastSpeed, a);
                                myCarLastPosition = D;
                                break;
                            }
                            myCarLastPosition = positionIWillBe;
                            myCarLastSpeed    = myCarLastSpeed + a * otherCarTime;
                        }
                        timeElapsed += otherCarTime;
                    }
                    if (myCarLastPosition < D)
                    {
                        timeElapsed += TimeFromX0ToXf(myCarLastPosition, D, myCarLastSpeed, a);
                    }

                    escritor.WriteLine(timeElapsed.ToString("N6").Replace(".", "").Replace(",", "."));
                }
            }
            lector.Close();
            escritor.Close();
        }
Ejemplo n.º 11
0
        private void GenerateCarsProc()
        {
            int MAX_CAR_NUMBER = simRoad.getMAX_CAR_NUMBER();
            //int CURRENT_CAR_NUMBER = 0;
            int          frequency = 200;
            int          genX      = 0;
            int          genY      = 0;
            CarPos       newCarPos = CarPos.TOP;
            CarDirection newCarDir = CarDirection.FORWARD;

            while (getPrun())
            {
                //zmienic na dopoki kolejka ma symbole
                if (simRoad.getQueueLength() > 0)//CURRENT_CAR_NUMBER < MAX_CAR_NUMBER)
                {
                    //losuj carPos
                    int temp = 0;
                    lock (randLock)
                    {
                        temp = rand.Next() % 4;
                    }
                    switch (temp)
                    {
                    case 0:
                        newCarPos = CarPos.TOP;
                        break;

                    case 1:
                        newCarPos = CarPos.RIGHT;
                        break;

                    case 2:
                        newCarPos = CarPos.BOTTOM;
                        break;

                    case 3:
                        newCarPos = CarPos.LEFT;
                        break;
                    }
                    genX = simRoad.getStartPointX(newCarPos);
                    genY = simRoad.getStartPointY(newCarPos);
                    simRoad.GetRoadMutex(genX, genY).WaitOne();
                    // :::::::::::
                    if (simRoad.checkSpace(genX, genY))
                    {
                        lock (randLock)
                        {
                            temp = rand.Next() % 3;
                        }
                        switch (temp)
                        {
                        case 0:
                            newCarDir = CarDirection.FORWARD;
                            break;

                        case 1:
                            newCarDir = CarDirection.TURN_LEFT;
                            break;

                        case 2:
                            newCarDir = CarDirection.TURN_RIGHT;
                            break;
                        }

                        Car    newCar       = new Car(newCarPos, newCarDir, simRoad.popCharacter(), this, simRoad, godOfRoad);
                        Thread newCarThread = new Thread(new ThreadStart(newCar.ThreadProc));
                        //newCarThread.Name= String.Format("{0}", i);
                        newCarThread.Start();
                        newCar.setThread(newCarThread);
                        cars.Add(newCar);
                        //CURRENT_CAR_NUMBER++;
                    }
                    simRoad.GetRoadMutex(genX, genY).ReleaseMutex();
                }
                Thread.Sleep(frequency);
            }
        }
Ejemplo n.º 12
0
 int GetItem(int[,] carpark, CarPos pos)
 {
     return(carpark[pos.Y, pos.X]);
 }
Ejemplo n.º 13
0
        private bool TryDown(int[,] carpark, CarPos pos)
        {
            var nextPos = new CarPos(pos.X, pos.Y + 1);

            return(CanDown(carpark, pos) && MoveCar(carpark, nextPos, MoveType.Down));
        }
Ejemplo n.º 14
0
 private bool TryRight(int[,] carpark, CarPos nextPos)
 {
     return(CanMove(carpark, nextPos) && MoveCar(carpark, nextPos, MoveType.Right));
 }