Objective-C 实现2048算法类
程序员文章站
2022-03-08 16:25:21
参数model有一个二维数组data,及阶数matrix
// .h文件
@class datamodel;
@interface algorithm...
参数model有一个二维数组data,及阶数matrix
// .h文件 @class datamodel; @interface algorithm : nsobject @property (nonatomic,assign) int addscore; // 加分 - (void)caculatetop:(datamodel *)model; // 上滑规则 - (void)caculatebottom:(datamodel *)model; // 下滑规则 - (void)caculateleft:(datamodel *)model; // 左滑规则 - (void)caculateright:(datamodel *)model; // 右滑规则 - (bool)randnewone:(datamodel *)data; - (int)getaddscore; @end // .m文件 @implementation algorithm #pragma mark - 滑动规则 - (void)caculatetop:(datamodel *)model { [self up_remove_blank:model]; [self up:model]; } - (void)caculatebottom:(datamodel *)model { [self down_remove_blank:model]; [self down:model]; } - (void)caculateleft:(datamodel *)model { [self left_remove_blank:model]; [self left:model]; } - (void)caculateright:(datamodel *)model { [self right_remove_blank:model]; [self right:model]; } - (int)getaddscore { int temp = _addscore; _addscore = 0; return temp; } #pragma mark - 新一个 - (bool)randnewone:(datamodel *)model { array data = [model getdata]; int all = 0; for (int i=0; i<model.matrix; i++) { for (int j=0; j<model.matrix; j++) { if (data[i][j] == 0) { all = all + 1; } } } if (all == 0) { return no; } int index = arc4random() % all; all = 0; for (int i=0; i<model.matrix; i++) { for (int j=0; j<model.matrix; j++) { if (data[i][j] == 0) { all = all + 1; if (all == index+1) { data[i][j] = 2; return yes; } } } } return no; } #pragma mark - 滑动算法 - (void)up_remove_blank:(datamodel *)model { array data = [model getdata]; int i,j,k; for(j=0;j < model.matrix;j++){ for(i=1;i < model.matrix;i++){ k=i; while(k-1>=0&&data[k-1][j]==0){//上面的那个为空 //swap(data[k][j],data[k-1][j]); int temp = data[k][j]; data[k][j] = data[k-1][j]; data[k-1][j] = temp; k--; } } } } - (void)down_remove_blank:(datamodel *)model { array data = [model getdata]; int i,j,k; for(j=0; j < model.matrix; j++){ for(i = model.matrix-2; i >= 0; i--){ k=i; while(k+1<=model.matrix-1&&data[k+1][j]==0){//上面的那个为空 //swap(a[k][j],a[k][j]); int temp = data[k][j]; data[k][j] = data[k+1][j]; data[k+1][j] = temp; k++; } } } } - (void)left_remove_blank:(datamodel *)model { array data = [model getdata]; int i,j,k; for(i=0;i < model.matrix;i++){ for(j=1;j<model.matrix;j++){ k=j; while(k-1>=0&&data[i][k-1]==0){//上面的那个为空 //swap(a[i][k],a[i][k-1]); int temp = data[i][k]; data[i][k] = data[i][k-1]; data[i][k-1] = temp; k--; } } } } - (void)right_remove_blank:(datamodel *)model { array data = [model getdata]; int i,j,k; for(i=0;i<model.matrix;i++){ for(j=model.matrix-2;j>=0;j--){ k=j; while(k+1<=model.matrix-1&&data[i][k+1]==0){//上面的那个为空 //swap(a[i][k],a[i][k+1]); int temp = data[i][k]; data[i][k] = data[i][k+1]; data[i][k+1] = temp; k++; } } } } - (void)left:(datamodel *)model { array data = [model getdata]; int i,j; for(i=0;i<model.matrix;i++){ for(j=0;j<model.matrix-1;j++){ if(data[i][j]==data[i][j+1]){ _addscore = _addscore + data[i][j]; data[i][j]+=data[i][j+1]; data[i][j+1]=0; [self left_remove_blank:model]; } } } } - (void)right:(datamodel *)model { array data = [model getdata]; int i,j; for(i=0;i<model.matrix;i++){ for(j=model.matrix-1;j>=1;j--){ if(data[i][j]==data[i][j-1]){ _addscore = _addscore + data[i][j]; data[i][j]+=data[i][j-1]; data[i][j-1]=0; [self right_remove_blank:model]; } } } } - (void)up:(datamodel *)model { array data = [model getdata]; int i,j; for(j=0;j<model.matrix;j++){//每一列 for(i=0;i<model.matrix-1;i++){ if(data[i][j]==data[i+1][j]){ _addscore = _addscore + data[i][j]; data[i][j]=data[i][j]+data[i+1][j]; data[i+1][j]=0; //移除空格 [self up_remove_blank:model]; } } } } - (void)down:(datamodel *)model { array data = [model getdata]; int i,j; for(j=0;j<model.matrix;j++){//每一列 for(i=model.matrix-1;i>=1;i--){ if(data[i][j]==data[i-1][j]){ _addscore = _addscore + data[i][j]; data[i][j]=data[i][j]+data[i-1][j]; data[i-1][j]=0; //移除空格 [self down_remove_blank:model]; } } } } @end
上一篇: iOS 捕获程序崩溃日志