Objective-C 实现2048算法类

时间:2021-08-29 14:37:03

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// .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