码迷,mamicode.com
首页 > 移动开发 > 详细

麦子学院ios笔记:Objective-C 实现2048算法类

时间:2015-07-20 16:31:57      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:ios   ios 开发   Objective-C   

今天麦子学院ios开发老师给大家介绍Objective-C 实现的IOS版小游戏2048算法类,十分的实用,有需要的小伙伴可以参考下。

参数model有一个二维数组data,及阶数matrix

// .h文件

@classDataModel;

@interfaceAlgorithm : NSObject

@property(nonatomic,assign)intaddScore;// 加分

- (void)caculateTop:(DataModel *)model;// 上滑规则

- (void)caculateBottom:(DataModel *)model;// 下滑规则

- (void)caculateLeft:(DataModel *)model;// 左滑规则

- (void)caculateRight:(DataModel *)model;// 右滑规则

- (BOOL)randNewOne:(DataModel *)data;

- (int)getAddScore;

@end

// .m文件

@implementationAlgorithm

#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 {

inttemp = _addScore;

_addScore =0;

returntemp;

}

#pragma mark - 新一个

- (BOOL)randNewOne:(DataModel *)model {

array data = [model getData];

intall =0;

for(inti=0; i<model.matrix; i++) {

for(intj=0; j<model.matrix; j++) {

if(data[i][j] ==0) {

all = all +1;

}

}

}

if(all ==0) {

returnNO;

}

intindex = arc4random() % all;

all =0;

for(inti=0; i<model.matrix; i++) {

for(intj=0; j<model.matrix; j++) {

if(data[i][j] ==0) {

all = all +1;

if(all == index+1) {

data[i][j] =2;

returnYES;

}

}

}

}

returnNO;

}

#pragma mark - 滑动算法

- (void)up_remove_blank:(DataModel *)model {

array data = [model getData];

inti,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]);

inttemp = 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];

inti,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]);

inttemp = 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];

inti,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]);

inttemp = 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];

inti,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]);

inttemp = data[i][k];

data[i][k] = data[i][k+1];

data[i][k+1] = temp;

k++;

}

}

}

}

- (void)left:(DataModel *)model {

array data = [model getData];

inti,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];

inti,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];

inti,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];

inti,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开发教程以及实战教程请前往:http://www.maiziedu.com/course/ios/

麦子学院ios笔记:Objective-C 实现2048算法类

标签:ios   ios 开发   Objective-C   

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
麦子学院
加入时间:2015-07-08
  关注此人  发短消息
文章分类
麦子学院”关注的人------(0
麦子学院”的粉丝们------(0
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!