Objective-C 實(shí)現(xiàn)2048算法類
更新時(shí)間:2015年06月10日 11:40:49 投稿:hebedich
本文給大家介紹的是使用Objective-C 實(shí)現(xiàn)的IOS版小游戲2048算法類,十分的實(shí)用,有需要的小伙伴可以參考下。
參數(shù)model有一個(gè)二維數(shù)組data,及階數(shù)matrix
// .h文件
@class DataModel;
@interface Algorithm : NSObject
@property (nonatomic,assign) int addScore; // 加分
- (void)caculateTop:(DataModel *)model; // 上滑規(guī)則
- (void)caculateBottom:(DataModel *)model; // 下滑規(guī)則
- (void)caculateLeft:(DataModel *)model; // 左滑規(guī)則
- (void)caculateRight:(DataModel *)model; // 右滑規(guī)則
- (BOOL)randNewOne:(DataModel *)data;
- (int)getAddScore;
@end
// .m文件
@implementation Algorithm
#pragma mark - 滑動(dòng)規(guī)則
- (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 - 新一個(gè)
- (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 - 滑動(dòng)算法
- (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){//上面的那個(gè)為空
//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){//上面的那個(gè)為空
//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){//上面的那個(gè)為空
//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){//上面的那個(gè)為空
//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
相關(guān)文章
ios12中遇到的帶input彈窗的錯(cuò)位問題的解決方法
這篇文章主要介紹了ios12中遇到的帶input彈窗的錯(cuò)位問題的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05
iOS 高德地圖仿微信發(fā)送實(shí)時(shí)位置
這篇文章主要介紹了iOS 高德地圖仿微信發(fā)送實(shí)時(shí)位置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
iOS?RN啟動(dòng)中管理Native?Module詳解
這篇文章主要為大家介紹了iOS?RN啟動(dòng)中?Native?Module?是如何被管理的,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
IOS UI學(xué)習(xí)教程之使用代碼創(chuàng)建button
這篇文章主要為大家詳細(xì)介紹了IOS UI學(xué)習(xí)教程之使用代碼創(chuàng)建button,感興趣的小伙伴們可以參考一下2016-03-03
簡(jiǎn)單談?wù)凜ore Animation 動(dòng)畫效果
下面小編就為大家?guī)硪黄?jiǎn)單談?wù)凜ore Animation 動(dòng)畫效果。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06

