示例#1
0
 bool endOfRoad(int pos_X, int pos_Y, Road road)
 {
     if (pos_X >= 24 || pos_Y >= 24 || pos_X <= 0 || pos_Y <= 0)
     {
         road.GetRoadMutex(pos_X, pos_Y).WaitOne();
         road.setSpaceFree(pos_X, pos_Y);
         road.GetRoadMutex(pos_X, pos_Y).ReleaseMutex();
         return(true);
     }
     return(false);
 }
示例#2
0
 bool singleMove()
 {
     step();
     road.GetRoadMutex(next_pos_X, next_pos_Y).WaitOne();
     if (road.checkSpace(next_pos_X, next_pos_Y))
     {
         //you can go into next_pos
         //zajmij miejsce ,zmien miejsce, zwolnij stare miejsce
         road.setSpaceOccupied(next_pos_X, next_pos_Y);
         old_pos_X  = pos_X;
         old_pos_Y  = pos_Y;
         pos_X      = next_pos_X;
         pos_Y      = next_pos_Y;
         shouldFree = true;
     }
     else
     {
         shouldFree = false;
     }
     road.GetRoadMutex(next_pos_X, next_pos_Y).ReleaseMutex();
     //avoiding nested mutex lock!!!
     if (shouldFree)
     {//
         road.GetRoadMutex(old_pos_X, old_pos_Y).WaitOne();
         road.setSpaceFree(old_pos_X, old_pos_Y);
         road.GetRoadMutex(old_pos_X, old_pos_Y).ReleaseMutex();
         if (pos_X == 13 && pos_Y == 11)
         {
             unlockYourLane(road);
         }
         if (pos_X == 14 && pos_Y == 13)
         {
             unlockYourLane(road);
         }
         if (pos_X == 11 && pos_Y == 12)
         {
             unlockYourLane(road);
         }
         if (pos_X == 12 && pos_Y == 14)
         {
             unlockYourLane(road);
         }
         return(true);
     }
     return(false);
 }
示例#3
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);
            }
        }