OBJECTIVE-C implement 2048 algorithm class _ios

Source: Internet
Author: User

The

parameter model has a two-dimensional array of data, and the order matrix

. h documents @class Datamodel;     @interface algorithm:nsobject @property (nonatomic,assign) int addscore;      Bonus points-(void) Caculatetop: (Datamodel *) model;     Upper sliding rule-(void) Caculatebottom: (Datamodel *) model;      Sliding rule-(void) Caculateleft: (Datamodel *) model;     Left sliding rule-(void) Caculateright: (Datamodel *) model;
 
Right sliding rule-(BOOL) Randnewone: (Datamodel *) data;
 
-(int) Getaddscore; @end//. m file @implementation algorithm #pragma mark-sliding rule-(void) Caculatetop: (Datamodel *) model {[Self Up_remov
  E_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-New-(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) {A
      LL = 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) {A
        LL = all + 1;
          if (all = = Index+1) {Data[i][j] = 2;
        return YES;
}}} return NO;
  #pragma mark-Slide algorithm-(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) {//The one above is empty//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) {//The one above is empty//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) {//The one above is empty//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) {//The one above is empty//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]) {_addScor
        E = _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]) {_addSco
        Re = _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++) {//each column for (i=0;i<model.matrix-1;i++) {if (Data[i][j]==data[i+1][j]) {_addscore = _addscore + D
        ATA[I][J];
        DATA[I][J]=DATA[I][J]+DATA[I+1][J];
        data[i+1][j]=0;
      Remove space [self up_remove_blank:model];
  }}-(void) down: (Datamodel *) Model {Array data = [model GetData];
  int i,j; for (j=0;j<model.matrix;j++) {//each column for (i=model.matrix-1;i>=1;i--) {if (Data[i][j]==data[i-1][j]) {_a
        Ddscore = _addscore + data[i][j];
        DATA[I][J]=DATA[I][J]+DATA[I-1][J];
        data[i-1][j]=0;
      Remove space [self down_remove_blank:model];
 @end}}}}

Related Article

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.