iOS Development-Cinema seat selection algorithm limits generation of solitary seats

Source: Internet
Author: User

It is well known that the selection of cinemas is not allowed to casually choose, we can not wayward pick, finally left n a separate seat, the purpose is to leave at least 2 attached seats;

In addition, some of the theater's seating is not regular, some seats are separated by aisle or special seat, resulting in different partitions, here to detect if there is no seat into a solitary seat,

The meaning of the solitary seat is simply that two little couples can't sit next to each other.

However, there are special circumstances, and because of the partition, a row of seats connected to only 2, or only 3, this time can still be allowed to buy one of them, or two of the ~

OK, after the condition is finished, introduce the data structure, under normal circumstances, each seat of the theater data will contain coordinates, seat number, such as X,y,row,column.

Where the area of the hall is divided into a m*n coordinate system with the size of a single seat, i.e. the aisle has its coordinates, and row and column are the real seats;

Long-winded, the following code:

The entry function is Verifyselectedseatswithseatsdic,

1. Parameter seatsdic is a dictionary, note that the dictionary filters out non-seat data! (The dictionary is required for this project, you can directly use an array, the method I also obtained an array; note that the order of the seat data must be orderly so that you can get to the left and right seat of a seat)

2. The data for each seat I encapsulated into a wcseatbutton, and provided the Isseatavailable method to determine whether the seat can be purchased (not sold)

3. Function Getnearbyseatsinsamerowforseat is responsible for obtaining all seats not separated by non-seats such as aisles

4. Function (BOOL) Isseat: (Wcseatbutton *) S1 nearbyseatwithoutroad: (Wcseatbutton *) s2 determine if two seats are separated by an aisle

-(BOOL) Verifyselectedseatswithseatsdic: (Nsdictionary *) seatsdic {Nsarray*seatbtns =[Seatsdic allvalues]; if([Seatbtns Count] >0) {         for(Wcseatbutton *btninchseatbtns) {            //See if an optional seat is on the list            if([btn isseatavailable]) {intIDX =Btn.seatindex; Wcseatbutton*PREBTN = [Seatsdic objectforkey:[nsstring stringWithFormat:@"%i", IDX-1]]; Wcseatbutton*NEXTBTN = [Seatsdic objectforkey:[nsstring stringWithFormat:@"%i", IDX +1]]; //Verify that there are adjacent seats and availableBOOL Ispreok = prebtn! = Nil &&[PreBtn.serviceSeat.row IsEqualToString:btn.serviceSeat.row]&&[Prebtn isseatavailable]; BOOL Isnextok= nextbtn! = Nil &&[NextBtn.serviceSeat.row IsEqualToString:btn.serviceSeat.row]&&[Nextbtn isseatavailable]; //judge the same partition again                if(ispreok) {Ispreok= ABS ([Btn.serviceSeat.yCoord intvalue]-[PreBtn.serviceSeat.yCoord intvalue]) = =1; }                if(isnextok) {Isnextok= ABS ([Btn.serviceSeat.yCoord intvalue]-[NextBtn.serviceSeat.yCoord intvalue]) = =1; }                if(!ispreok &&!)Isnextok) {Nsarray*nearbyseats =[self getnearbyseatsinsamerowforseat:btn withseatsdic:seatsdic]; //exception two seats selected only one                    if([nearbyseats count] = =2) {                        Continue; }                    //Exception 3 seats 2 consecutive selected, leaving a single seat                    if([nearbyseats count] = =3) {                        intIDX =[Nearbyseats indexofobject:btn]; //empty seat If the middle seat is selected on both sides, then as long as there is a choice to be                        if(idx = =0&&! [nearbyseats[2] isseatavailable]) {Continue; }Else if(idx = =2&&! [nearbyseats[0] isseatavailable]) {Continue; }                    }                    //restrict the seat selected by the current user, prevent other channels from creating a solitary seat, which prevents the current user from selecting a seat                     for(Wc630serviceseat *sinchself.orderItem.seats) {if((prebtn && [[[Prebtn Serviceseat] cineseatid] isequaltostring:s.cineseatid]) | |(nextbtn&&[[[[Nextbtn Serviceseat] cineseatid] Isequaltostring:s.cineseatid]) {                             returnNO; }                    }                }            }        }    }    returnYES;}-(Nsarray *) Getnearbyseatsinsamerowforseat: (Wcseatbutton *) seat Withseatsdic: (Nsdictionary *) seatsdic{Nsmutablearray*result =[Nsmutablearray array];    [Result Addobject:seat]; intIDX = Seat.seatindex-1; // LeftWcseatbutton *tmp= [Seatsdic objectforkey:[nsstring stringWithFormat:@"%i", IDX]];  while([self isseat:tmp nearbyseatwithoutroad:seat]) {[Result insertobject:tmp Atindex:0]; IDX--; TMP= [Seatsdic objectforkey:[nsstring stringWithFormat:@"%i", IDX]]; } idx= Seat.seatindex +1; // Righttmp= [Seatsdic objectforkey:[nsstring stringWithFormat:@"%i", IDX]];  while([self isseat:tmp nearbyseatwithoutroad:seat]) {[Result addobject:tmp]; IDX++; TMP= [Seatsdic objectforkey:[nsstring stringWithFormat:@"%i", IDX]]; }    returnresult;}-(BOOL) Isseat: (Wcseatbutton *) S1 nearbyseatwithoutroad: (Wcseatbutton *) s2{returnS1! = Nil &&[S1.serviceSeat.row IsEqualToString:s2.serviceSeat.row]&&ABS ([S1.serviceSeat.yCoord intvalue]-[S2.serviceSeat.yCoord intvalue]) = = ABS ([s1.serviceSeat.column intvalue]-[S2.serviceSeat.column intvalue]);}

iOS Development-Cinema seat selection algorithm limits generation of solitary seats

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.