IOS開發-影院選座演算法 限制產生孤座,ios影院

來源:互聯網
上載者:User

IOS開發-影院選座演算法 限制產生孤座,ios影院

眾所周知目前影院選座是不允許隨便選的,我們不可以任性的挑三揀四,最後留下N個單獨的座位,目的就是要留下至少2個連著的座位;

另外有些影院的座位擺放並不是規則的,有些座位被過道或者特殊座位分割開,產生了不同的分區,這裡就實現檢測有沒有座位變成了孤座,

孤座的含義簡單說就是兩個小情侶不能挨著坐了~

但是也存在特殊情況,及因為分區的緣故,某一排連著的座位只有2個,或者只有3個,這時候還是可以允許人家買其中之一,或者之二的~

好,條件說完之後,開始介紹資料結構,正常情況下,影院的每一個座位元據會包含座標,座位號,如x,y,row,column。

其中,影廳中的面積會以單個座位的大小被分割成M*N的座標系,即過道也有它的座標;而row和column才是真正的座位;

囉嗦了半天,下面上代碼:

入口函數是verifySelectedSeatsWithSeatsDic,

  1. 參數seatsDic是一個字典,注意該字典過濾掉了非座位的資料!(字典是本項目所需,你可以直接用一個array,方法中我也是擷取了array;注意座位元據的順序一定是有序的,這樣才能擷取到一個座位的左右座位)

  2. 每個座位的資料我封裝成了一個WCSeatButton,並提供了isSeatAvailable方法判斷該座位是否可以被購買(未被售出)

  3. 函數getNearBySeatsInSameRowForSeat負責擷取沒有被過道等非座位隔開的所有座位

    4. 函數(BOOL)isSeat:(WCSeatButton *)s1 nearBySeatWithoutRoad:(WCSeatButton *)s2判斷兩個座位是否被過道隔開

-(BOOL)verifySelectedSeatsWithSeatsDic:(NSDictionary *)seatsDic {    NSArray *seatBtns = [seatsDic allValues];    if ([seatBtns count] > 0) {        for (WCSeatButton *btn in seatBtns) {            //查看可選座位是否落單            if([btn isSeatAvailable]){                int idx = btn.seatIndex;                WCSeatButton *preBtn = [seatsDic objectForKey:[NSString stringWithFormat:@"%i", idx - 1]];                WCSeatButton *nextBtn = [seatsDic objectForKey:[NSString stringWithFormat:@"%i", idx + 1]];                //檢驗是否存在相鄰座位 and 可用                BOOL 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] ;                //再判斷同一分區                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];                    //例外 兩個座位只選一個                    if ([nearBySeats count] == 2) {                        continue;                    }                    //例外 3個座位中2個連續被選,剩下單個座位                    if ([nearBySeats count] == 3) {                        int idx = [nearBySeats indexOfObject:btn];                        //空座位如果在兩邊說明中間的座位已經被選,則只要還有一個被選就可以                        if (idx == 0 && ![nearBySeats[2] isSeatAvailable]) {                            continue;                        }else if(idx == 2 && ![nearBySeats[0] isSeatAvailable]) {                            continue;                        }                    }                    //只限制目前使用者選擇的座位,防止其他渠道產生孤座導致目前使用者不能選座                    for (WC630ServiceSeat *s in self.orderItem.seats) {                         if((preBtn && [[[preBtn serviceSeat] cineSeatId] isEqualToString:s.cineSeatId]) ||                                 (nextBtn && [[[nextBtn serviceSeat] cineSeatId] isEqualToString:s.cineSeatId])    ) {                             return NO;                         }                    }                }            }        }    }    return YES;}-(NSArray *)getNearBySeatsInSameRowForSeat:(WCSeatButton *)seat withSeatsDic:(NSDictionary *)seatsDic{    NSMutableArray *result = [NSMutableArray array];    [result addObject:seat];    int idx = seat.seatIndex - 1;    //left    WCSeatButton *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;    //right    tmp= [seatsDic objectForKey:[NSString stringWithFormat:@"%i", idx]];    while([self isSeat:tmp nearBySeatWithoutRoad:seat]){        [result addObject:tmp];        idx++;        tmp = [seatsDic objectForKey:[NSString stringWithFormat:@"%i", idx]];    }    return result;}-(BOOL)isSeat:(WCSeatButton *)s1 nearBySeatWithoutRoad:(WCSeatButton *)s2{    return     s1 != 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]) ;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.