# 童年的记忆——iOS 拼图游戏

2018-02-27 11:11:12来源:https://www.jianshu.com/p/8f1436f688d3作者:小而白人点击

demo.gif

``-(void)calculateIndexOfMoveable {    //记录空白块的索引，紧靠空白块的方块才可以移动，实际上就是与空白块交换位置。初始化时的空白块统一在右下角。    //计算当前可移动的方块    // 白色块所在行row = indexOfWhite / totalCols    // 白色块所在列col = indexOfWhite % totalCols    left = indexOfWhite - 1    right = indexOfWhite + 1;    up = indexOfWhite - totalCols;    down = indexOfWhite + totalCols;    //    但是要排除一些四周情况下的索引    if ([self indexOfCol: left] > [self indexOfCol: indexOfWhite]) {        //left 排除        left = -1;    }    if ([self indexOfCol: right] < [self indexOfCol: indexOfWhite]) {        //right 排除        right = -1;    }    if (up < 0) {        //up 排除        up = -1;    }    if (down > totalCols*totalRows-1) {        //down 排除        down = -1;    }}``

``-(NSInteger)indexOfRow:(NSInteger)index {    return index / totalCols;}-(NSInteger)indexOfCol:(NSInteger)index {     return index % totalCols;}``

``-(NSInteger)calculateIndexOfMoveable_left {    left = indexOfWhite - 1;    return [self indexOfCol: left] > [self indexOfCol: indexOfWhite] ? -1 : left;}-(NSInteger)calculateIndexOfMoveable_right {    right = indexOfWhite + 1;    return [self indexOfCol: right] < [self indexOfCol: indexOfWhite] ? -1 : right;}-(NSInteger)calculateIndexOfMoveable_up {        return (indexOfWhite - totalCols) < 0 ? -1 : indexOfWhite - totalCols;}-(NSInteger)calculateIndexOfMoveable_down {        return (indexOfWhite + totalCols) > (totalCols*totalRows-1) ? -1 : indexOfWhite + totalCols;}``

``-(NSMutableArray *)dataSource {    if (!_dataSource) {        _dataSource = [NSMutableArray array];                CGFloat x,y,w,h;         w = (self.oringinalImg.image.size.width/totalCols)/[UIScreen mainScreen].scale;         h = (self.oringinalImg.image.size.height/totalRows)/[UIScreen mainScreen].scale;                for (int i=0; i<totalRows; i++) {            for (int j=0; j<totalCols; j++) {                x = j*w;                y = i*h;                               CGRect rect = CGRectMake(x,y,w,h);                if ((i==totalRows-1) && (j== totalCols-1)) {                    [_dataSource addObject: [[UIImage alloc] init] ];                } else {                                        [_dataSource addObject: [self ct_imageFromImage:self.oringinalImg.image inRect: rect]];                }            }       }     }     return _dataSource;}``

``-(NSMutableArray *)startIndexs {    if (!_startIndexs) {        _startIndexs = [NSMutableArray array];        for (int i = 0; i < totalCols*totalRows; i++) {            _startIndexs[i] = @(i);        };    }    return _startIndexs;}``

``/** *  从图片中按指定的位置大小截取图片的一部分 * *  @param image UIImage image 原始的图片 *  @param rect  CGRect rect 要截取的区域 * *  @return UIImage */- (UIImage *)ct_imageFromImage:(UIImage *)image inRect:(CGRect)rect {        //把像素rect 转化为点rect（如无转化则按原图像素取部分图片）    CGFloat scale = [UIScreen mainScreen].scale;    CGFloat x= rect.origin.x*scale,y=rect.origin.y*scale,w=rect.size.width*scale,h=rect.size.height*scale;    CGRect dianRect = CGRectMake(x, y, w, h);        //截取部分图片并生成新图片    CGImageRef sourceImageRef = [image CGImage];    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, dianRect);    UIImage *newImage = [UIImage imageWithCGImage:newImageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];    return newImage;}``

``- (void)randomArray {    //两个数组同步打乱顺序,早知道这么麻烦我就用模型将索引值绑定image了。/(ㄒoㄒ)/~~    NSMutableArray *newDatasourceArr = [NSMutableArray array];    NSMutableArray *newStartIndexArr = [NSMutableArray array];    int m = (int)self.dataSource.count;    for (int i=0; i<m; i++) {        int t = arc4random() % (self.dataSource.count);        newDatasourceArr[i] = self.dataSource[t];        newStartIndexArr[i] = self.startIndexs[t];        self.dataSource[t] = [self.dataSource lastObject];        self.startIndexs[t] = [self.startIndexs lastObject];        [self.dataSource removeLastObject];        [self.startIndexs removeLastObject];    }    self.dataSource = newDatasourceArr;    self.startIndexs = newStartIndexArr;}``

12.17修改更新：关于打乱图序，我这种随机打乱顺序的做法欠妥，试玩几次后发现有些情况我总是还原不了，回忆上学时玩过的一款游戏没有出现过这样的情况。这时候我开始怀疑并不是所有的序列都可以进行还原。而我却忽略了，这非常不应该。