iOS 类似2048、4096小游戏-OC

大概思路(初步制作,粗工制造):demo

1、Collection 创建cell

2、cell上添加一个view,用来添加手势

3、字典用来存放数据-》每次执行StarGame数组接收没有数字的字典,原来字典对应的删除-》随机选中改变数组-》插回原来数据-》每次滑动 用选择排序分别相加相邻且相等的数据后,再用选择排序 进行排序(详情下文)-》刷新cell-》♻️

iOS 类似2048、4096小游戏-OC iOS 类似2048、4096小游戏-OCiOS 类似2048、4096小游戏-OCiOS 类似2048、4096小游戏-OCiOS 类似2048、4096小游戏-OC

//数据
NSMutableDictionary * dictChange;//放 数值为0(代表没有数值的位置) 的字典
UISwipeGestureRecognizer * recognizer;//滑动手势
NSMutableDictionary * dictData;//数据字典
// 先添加 初始数据
-(void)creaDATA{ dictData = [NSMutableDictionary dictionary];
for (int i=; i<; i++) { [dictData setObject:@"" forKey:@(i)]; } }
-(void)StarGame{
//筛选出 模块上可以添加数据的 位置字典。装到数组,随机分配
dictChange = [NSMutableDictionary dictionary];
for (int i=; i<; i++) {
NSString * str = [dictData objectForKey:@(i)];
if ([str isEqualToString:@""]) { [dictChange setObject:@"" forKey:@(i)]; } } //先判断有没有满足要求
NSArray * value = [dictData allValues];
if ([value containsObject:_GameNum]) {
NSInteger haomiao,miao,fen; haomiao = timernumber%;
miao = timernumber/;
fen = miao/; [UIView pushAlertTwoActionViewWithMessage:[NSString stringWithFormat:@"恭喜你,完成游戏\n用时:%02ld:%02ld:%02ld",fen,miao,haomiao] Target:self Title:@"OH耶!!!" oneAlertTitle:@"挑战时间" twoAlertTitle:@"休息一下" oneActionfunc:^{
//继续
[dictData removeAllObjects];
[collectionvieww reloadData];
timernumber = ;
[self StarGame];
[self timerStar]; } twoActionfunc:^{ //返回上一页 }]; } //如果没有位置可以再添加数字了 游戏就结束了
if (dictChange.count ==) {
[timerss invalidate];
timerss = nil; [UIView pushAlertTwoActionViewWithMessage:@"游戏结束" Target:self Title:@"再挑战一局吧" oneAlertTitle:@"好的" twoAlertTitle:@"休息一下" oneActionfunc:^{
//继续
[dictData removeAllObjects];
[collectionvieww reloadData];
timernumber = ;
[self StarGame];
[self timerStar]; } twoActionfunc:^{ //返回上一页 }];
return;
} //随机添加数据。刷新表格
int random = (int)( (arc4random() % (dictChange.count)));
NSArray * dictKey = [dictChange allKeys];
[dictChange setObject:@"" forKey:dictKey[random]];
//返回给数据字典
for (NSString * key in dictKey) { [dictData setObject:dictChange[key] forKey:key]; }
[dictChange removeAllObjects];
//刷新表
[collectionvieww reloadData]; }

每次插入数字时

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(silidingTop)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[frontView addGestureRecognizer:recognizer]; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(silidingLift)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[frontView addGestureRecognizer:recognizer]; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(silidingDown)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[frontView addGestureRecognizer:recognizer]; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(silidingRight)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[frontView addGestureRecognizer:recognizer];

上左下右 手势添加

-(void)silidingLift{

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ), ^{
for (int h = ; h<; h++) { //选择 筛选相邻数字相加。只相加一次
for (int i=; i<; i++) { if (![dictData[@(i+*h)] isEqualToString:@""]) { for (int j=i+; j<; j++) { if (![dictData[@(j+*h)] isEqualToString:@""]){ if ([dictData[@(i+*h)] isEqualToString: dictData[@(j+*h)]]) { //后边的数是 相加后的值
[dictData setObject:[NSString stringWithFormat:@"%d",[dictData[@(j+*h)] intValue] + [dictData[@(i+*h)] intValue]] forKey:@(i+*h)];
[dictData setObject:@"" forKey:@(j+*h)];//前边的 设置为0 }
break; // 有了 就不再循环了 }
} } if (i==) {
[self Lift2:h];
} } } dispatch_async(dispatch_get_main_queue(), ^{
[collectionvieww reloadData]; [self StarGame];
});
}); } -(void)Lift2:(int)h{ //选择排序 整理位置
for(int i=;i<;i++){
if ([dictData[@(i+*h)] isEqualToString:@""])//i为0 的时候
{ for (int j=i+; j<; j++)
{
if (![dictData[@(j+*h)] isEqualToString:@""]) //j 有值的时候 //i 的值 为j 的。j设置为0
{ [dictData setObject:dictData[@(j+*h)] forKey:@(i+*h)];
[dictData setObject:@"" forKey:@(j+*h)]; break; // 继续下一次循环
} } } } }

上一篇:extend简单用法


下一篇:vue请求拦截