#pragma mark - 读取缓存数据
- (void)getCaches {
self.maxNum = 0;
self.maxNum1 = 0;//最大值
self.score = 0;//当前得分
self.hisScore = 0;
self.hisScore1 = 0;//历史最高分
self.SoundZT = 1;
self.twoFour = [NSArray arrayWithObjects:@"2",@"2",@"4",@"2",@"2",@"4",@"2",@"2",@"2",@"2", nil];
//获取数据
self.numbers = [[NSMutableArray alloc]init];
self.ScoresArray = [[NSMutableArray alloc]init];
self.dateArrary = [[NSMutableArray alloc]init];
if (args == 2) {//重置
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"DataDict"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"DataDict"];
if (dict == nil) {
self.DataDict = [NSMutableDictionary dictionary];
} else {
self.DataDict = [NSMutableDictionary dictionaryWithDictionary:dict];
self.numbers = [self.DataDict objectForKey:@"numbers"];
NSLog(@"%@",self.numbers);
self.ScoresArray = [self.DataDict objectForKey:@"ScoresArray"];
self.dateArrary = [self.DataDict objectForKey:@"dateArrary"];
self.maxNum = [[self.numbers objectAtIndex:0] intValue];
self.maxNum1 = [[self.numbers objectAtIndex:0] intValue];
self.hisScore = [[self.numbers objectAtIndex:1] intValue];
self.hisScore1 = [[self.numbers objectAtIndex:1] intValue];
self.SoundZT = [[self.numbers objectAtIndex:2] intValue];
}
}
#pragma mark - 设置手势
- (void)setSwipeGesture {
UISwipeGestureRecognizer * recoginzer;
recoginzer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
[recoginzer setDirection:UISwipeGestureRecognizerDirectionUp];
[self.view addGestureRecognizer:recoginzer];
recoginzer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
[recoginzer setDirection:UISwipeGestureRecognizerDirectionDown];
[self.view addGestureRecognizer:recoginzer];
recoginzer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
[recoginzer setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:recoginzer];
recoginzer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
[recoginzer setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:recoginzer];
}
#pragma mark - button按钮功能
-(void)btn
{
if (self.SoundZT) {
[self playSound];
}
RootViewController * rootVC = [[RootViewController alloc]init];
rootVC.title = @"菜单";
rootVC.delegate = self;
rootVC.SoundZT = self.SoundZT;
[self.navigationController pushViewController:rootVC animated:YES];
}
#pragma mark - 排行榜
-(void)phb
{
if (self.SoundZT) {
[self playSound];
}
ScoreViewController * scoreVC = [[ScoreViewController alloc]init];
scoreVC.title = @"排行榜";
scoreVC.array = self.ScoresArray;
scoreVC.dateArray = self.dateArrary;
[self.navigationController pushViewController:scoreVC animated:YES];
}
#pragma mark - 排行榜排序
-(void)SortScores
{
//日期处理
NSDate * date = [NSDate date];
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString * today = [dateFormatter stringFromDate:date];
NSString * temp = [[NSString alloc]init];
if (self.ScoresArray == nil) {
self.ScoresArray = [[NSMutableArray alloc]init];
self.dateArrary = [[NSMutableArray alloc]init];
}
if ([self.ScoresArray count] < 10) {
[self.ScoresArray addObject:[NSString stringWithFormat:@"%d",self.score]];
[self.dateArrary addObject:today];
}else {
for (int i = 0; i < [self.ScoresArray count] - 1; i++) {
for (int j = i+1; j < [self.ScoresArray count]; j++) {
if ([[self.ScoresArray objectAtIndex:i] intValue] > [[self.ScoresArray objectAtIndex:j] intValue]) {
temp = [self.ScoresArray objectAtIndex:i];
[self.ScoresArray replaceObjectAtIndex:i withObject:[self.ScoresArray objectAtIndex:j]];
[self.ScoresArray replaceObjectAtIndex:j withObject:temp];
temp = [self.dateArrary objectAtIndex:i];
[self.dateArrary replaceObjectAtIndex:i withObject:[self.dateArrary objectAtIndex:j]];
[self.dateArrary replaceObjectAtIndex:j withObject:temp];
}
}
}
if (self.score > [[self.ScoresArray objectAtIndex:0] intValue]) {
[self.ScoresArray replaceObjectAtIndex:0 withObject:[NSString stringWithFormat:@"%d",self.score]];
[self.dateArrary replaceObjectAtIndex:0 withObject:today];
}
}
}
#pragma mark - label的get和set //获取label值
-(int)geti:(int)i j:(int)j
{
int value = 0;
UILabel * label;
if (i == 0) {
if (j == 0) {
label = lab0_0;
}else if(j == 1) {
label = lab0_1;
}else if(j == 2) {
label = lab0_2;
}else if(j == 3) {
label = lab0_3;
}
}else if(i == 1) {
if (j == 0) {
label = lab1_0;
}else if(j == 1) {
label = lab1_1;
}else if(j == 2) {
label = lab1_2;
}else if(j == 3) {
label = lab1_3;
}
}else if (i == 2) {
if (j == 0) {
label = lab2_0;
}else if(j == 1) {
label = lab2_1;
}else if(j == 2) {
label = lab2_2;
}else if(j == 3) {
label = lab2_3;
}
}else if (i == 3) {
if (j == 0) {
label = lab3_0;
}else if(j == 1) {
label = lab3_1;
}else if(j == 2) {
label = lab3_2;
}else if(j == 3) {
label = lab3_3;
}
}
value = [label.text intValue];
return value;
}
#pragma mark - 设置label值
-(void)seti:(int)i j:(int)j v:(int)value
{
UILabel * temp = [self getLabeli:i j:j];
if (value != 0) {
temp.text = [NSString stringWithFormat:@"%d",value];
}else{
temp.text = @"";
}
if (self.maxNum != 0) {
self.labMax.text = [NSString stringWithFormat:@"%d",self.maxNum];
}else{
self.labMax.text = @"";
}
if (self.score != 0) {
self.labScore.text = [NSString stringWithFormat:@"分数\n%d",self.score];
}else{
self.labScore.text = @"分数\n";
}
self.hisScore = self.score > self.hisScore?self.score:self.hisScore;
if (self.hisScore != 0) {
self.labHisScore.text = [NSString stringWithFormat:@"历史最高成绩\n%d",self.hisScore];
}else{
self.labHisScore.text = @"历史最高成绩\n";
}
if((self.maxNum > self.maxNum1 || self.hisScore > self.hisScore1)
&& self.hisScore != self.lastHisScore) {
//归档
self.numbers = [NSMutableArray arrayWithObjects:[NSString stringWithFormat:@"%d",self.maxNum],[NSString stringWithFormat:@"%d",self.hisScore],[NSString stringWithFormat:@"%d",self.SoundZT], nil];
[self.DataDict setObject:self.numbers forKey:@"numbers"];
[[NSUserDefaults standardUserDefaults] setObject:self.DataDict forKey:@"DataDict"];
self.lastHisScore = self.hisScore;
}
[self setbackColorv:[self geti:i j:j] label:temp];
}
//获取label
-(UILabel *)getLabeli:(int)i j:(int)j
{
UILabel * temp;
if (i == 0) {
if (j == 0) {
temp = lab0_0;
}else if(j == 1) {
temp = lab0_1;
}else if(j == 2) {
temp = lab0_2;
}else if(j == 3) {
temp = lab0_3;
}
}else if(i == 1) {
if (j == 0) {
temp = lab1_0;
}else if(j == 1) {
temp = lab1_1;
}else if(j == 2) {
temp = lab1_2;
}else if(j == 3) {
temp = lab1_3;
}
}else if (i == 2) {
if (j == 0) {
temp = lab2_0;
}else if(j == 1) {
temp = lab2_1;
}else if(j == 2) {
temp = lab2_2;
}else if(j == 3) {
temp = lab2_3;
}
}else if (i == 3) {
if (j == 0) {
temp = lab3_0;
}else if(j == 1) {
temp = lab3_1;
}else if(j == 2) {
temp = lab3_2;
}else if(j == 3) {
temp = lab3_3;
}
}
return temp;
}
//设置label颜色
-(void)setbackColorv:(int)value label:(UILabel *)label
{
UIColor * color = [self.dict objectForKey:[NSString stringWithFormat:@"%d",value]];
if (color != nil) {
label.backgroundColor = color;
}
if (value > 8192) {
label.backgroundColor = [UIColor purpleColor];
}
}
#pragma mark - 滑动功能
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recoginzer
{
int ischange = 0;
if (self.SoundZT) {
[self playSound];
}
if (recoginzer.direction == UISwipeGestureRecognizerDirectionUp) {
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 3; x++) {
for (int z = x+1; z<4; z++) {
if ([self geti:x j:y] == 0
&& [self geti:z j:y] != 0) {
[self seti:x j:y v:[self geti:z j:y]];
[self seti:z j:y v:0];
ischange++;
}
if ([self geti:x j:y] == [self geti:z j:y]
&& [self geti:x j:y] != 0) {
[self seti:x j:y v:[self geti:z j:y]*2];
[self seti:z j:y v:0];
ischange++;
self.score += [self geti:x j:y];
self.maxNum = [self geti:x j:y] > self.maxNum?[self geti:x j:y]:self.maxNum;
break;
}
if ([self geti:x j:y] != [self geti:z j:y]
&& [self geti:x j:y] != 0
&& [self geti:z j:y] != 0) {
break;
}
}
}
}
}
if (recoginzer.direction == UISwipeGestureRecognizerDirectionDown) {
for (int y = 0; y < 4; y++) {
for (int x = 3; x >= 0; x--) {
for (int z = x-1; z>=0; z--) {
if ([self geti:x j:y] == 0
&& [self geti:z j:y] != 0) {
[self seti:x j:y v:[self geti:z j:y]];
[self seti:z j:y v:0];
ischange++;
}
if ([self geti:x j:y] == [self geti:z j:y]
&& [self geti:x j:y] != 0) {
[self seti:x j:y v:[self geti:z j:y]*2];
[self seti:z j:y v:0];
ischange++;
self.score += [self geti:x j:y];
self.maxNum = [self geti:x j:y] > self.maxNum?[self geti:x j:y]:self.maxNum;
break;
}
if ([self geti:x j:y] != [self geti:z j:y]
&& [self geti:x j:y] != 0
&& [self geti:z j:y] != 0) {
break;
}
}
}
}
}
if (recoginzer.direction == UISwipeGestureRecognizerDirectionLeft) {
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 3; y++) {
for (int z = y+1; z < 4;z++) {
if ([self geti:x j:y] == 0
&& [self geti:x j:z] != 0) {
[self seti:x j:y v:[self geti:x j:z]];
[self seti:x j:z v:0];
ischange++;
}
if ([self geti:x j:y] == [self geti:x j:z]
&& [self geti:x j:y] != 0) {
[self seti:x j:y v:[self geti:x j:z]*2];
[self seti:x j:z v:0];
ischange++;
self.score += [self geti:x j:y];
self.maxNum = [self geti:x j:y] > self.maxNum?[self geti:x j:y]:self.maxNum;
break;
}
if ([self geti:x j:y] != [self geti:x j:z]
&& [self geti:x j:y] != 0
&& [self geti:x j:z] != 0) {
break;
}
}
}
}
}
if (recoginzer.direction == UISwipeGestureRecognizerDirectionRight) {
for (int x = 0; x < 4; x++) {
for (int y = 3; y >= 0; y--) {
for (int z = y-1; z >= 0;z--) {
if ([self geti:x j:y] == 0
&& [self geti:x j:z] != 0) {
[self seti:x j:y v:[self geti:x j:z]];
[self seti:x j:z v:0];
ischange++;
}
if ([self geti:x j:y] == [self geti:x j:z]
&& [self geti:x j:y] != 0) {
[self seti:x j:y v:[self geti:x j:z]*2];
[self seti:x j:z v:0];
ischange++;
self.score += [self geti:x j:y];
self.maxNum = [self geti:x j:y] > self.maxNum?[self geti:x j:y]:self.maxNum;
break;
}
if ([self geti:x j:y] != [self geti:x j:z]
&& [self geti:x j:y] != 0
&& [self geti:x j:z] != 0) {
break;
}
}
}
}
}
if (ischange) {
[self randNum:1];
}
}
//设置随机数
-(int)randNum:(int)index
{
//index 判断是否生产随机数,
srand((unsigned)time(0));
int baseNum = 0;//基数
CGPoint point;
NSValue * value;
NSMutableArray * nums = [[NSMutableArray alloc]init];
for (int i = 0; i < X1; i++) {
for (int j = 0; j < Y1; j++) {
if([self geti:i j:j] == 0) {
point.x = i;
point.y = j;
value = [NSValue valueWithCGPoint:point];
[nums addObject:value];
}
}
}
//index = 0 校验是否结束
//index = 1 生成随机数
if ([nums count] > 0) {
if (index == 1) {
int location = 0;//位置
location = rand()%([nums count]);
value = [nums objectAtIndex:location];
point = [value CGPointValue];
baseNum = rand()%10;//随机2、4
[self seti:point.x j:point.y v:[[self.twoFour objectAtIndex:baseNum] intValue]];
UILabel * label = [self getLabeli:point.x j:point.y];
[self setAnimation:label];
}
}
if([nums count] <= 1) {
int isend = [self isEnd];//是否结束游戏
if (isend == 0) {
//归档
//排序
[self SortScores];
[self.DataDict setObject:self.ScoresArray forKey:@"ScoresArray"];
[self.DataDict setObject:self.dateArrary forKey:@"dateArrary"];
[[NSUserDefaults standardUserDefaults] setObject:self.DataDict forKey:@"DataDict"];
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"运行提示" message:@"游戏结束!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
}
}
return [nums count];
}
//返回值 0 结束,1,正常
-(int)isEnd
{
int isEnd = 0;//是否结束
//up
for (int i = 1; i < X1; i++) {
for (int j = 0; j < Y1; j++) {
if ([self geti:i j:j] == [self geti:i-1 j:j]
&& [self geti:i j:j] != 0) {
isEnd = 1;
}else if ([self geti:i-1 j:j] == 0
&& [self geti:i j:j] != 0) {
isEnd = 1;
}
}
}
//down
for (int i = X1-2; i >= 0; i--) {
for (int j = Y1 - 1; j >= 0; j--) {
if ([self geti:i j:j] == [self geti:i+1 j:j]
&& [self geti:i j:j] != 0) {
isEnd = 1;
}else if ([self geti:i+1 j:j] == 0
&& [self geti:i j:j] != 0) {
isEnd = 1;
}
}
}
//left
for (int j = 1; j < Y1; j++) {
for (int i = 0; i<X1; i++) {
if ([self geti:i j:j] == [self geti:i j:j-1]
&& [self geti:i j:j] != 0) {
isEnd = 1;
}else if ([self geti:i j:j-1] == 0
&& [self geti:i j:j] != 0) {
isEnd = 1;
}
}
}
//right
for (int j = Y1-2; j >= 0; j--) {
for (int i = X1-1; i>=0; i--) {
if ([self geti:i j:j] == [self geti:i j:j+1]
&& [self geti:i j:j] != 0) {
isEnd = 1;
}else if ([self geti:i j:j+1] == 0
&& [self geti:i j:j] != 0) {
isEnd = 1;
}
}
}
return isEnd;
}