Skip to content

Instantly share code, notes, and snippets.

@zhaoyswd
Last active August 29, 2015 14:16
Show Gist options
  • Save zhaoyswd/ef49587cf1f4aa7fcc55 to your computer and use it in GitHub Desktop.
Save zhaoyswd/ef49587cf1f4aa7fcc55 to your computer and use it in GitHub Desktop.
Apple Watch拼图游戏
#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>
@interface PuzzleInterfaceController : WKInterfaceController
@property (nonatomic, weak) IBOutlet WKInterfaceButton *button1;
@property (nonatomic, weak) IBOutlet WKInterfaceButton *button2;
@property (nonatomic, weak) IBOutlet WKInterfaceButton *button3;
@property (nonatomic, weak) IBOutlet WKInterfaceButton *button4;
@property (nonatomic, weak) IBOutlet WKInterfaceButton *button5;
@property (nonatomic, weak) IBOutlet WKInterfaceButton *button6;
@property (nonatomic, weak) IBOutlet WKInterfaceButton *button7;
@property (nonatomic, weak) IBOutlet WKInterfaceButton *button8;
@property (nonatomic, weak) IBOutlet WKInterfaceButton *button9;
@property (nonatomic, weak) IBOutlet WKInterfaceGroup *row1;
@property (nonatomic, weak) IBOutlet WKInterfaceGroup *row2;
@property (nonatomic, weak) IBOutlet WKInterfaceGroup *row3;
@end
#import "PuzzleInterfaceController.h"
@interface PuzzleInterfaceController(){
NSUInteger _selectedButtonIndex;//当前选中的buttonIndex
int _vector[3][3];//当前的图片分布
BOOL _finished;//是否已经完成
int _step;//当前使用的步数
int _difficulty;//难度
}
@property (nonatomic, retain) NSArray *buttons;//所有9个button
@property (nonatomic, retain) UIImage *wholeImage;//完整的图片
@property (nonatomic, retain) NSMutableArray *subImages;//9个小图片
@end
@implementation PuzzleInterfaceController
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
if (context && [context isKindOfClass:[NSDictionary class]] && [context objectForKey:@"difficulty"]) {
_difficulty = [[context objectForKey:@"difficulty"] intValue];
}
else{
_difficulty = 4;
}
//一行3个button均分,button高度=宽度
//其实可以在storyboard里分别设置两种尺寸的布局,但是太懒了。。。
CGFloat width = [[WKInterfaceDevice currentDevice] screenBounds].size.width/3;
[_row1 setHeight:width];
[_row2 setHeight:width];
[_row3 setHeight:width];
self.buttons = @[_button1,_button2,_button3,_button4,_button5,_button6,_button7,_button8,_button9];
for (WKInterfaceButton *button in _buttons) {
[button setWidth:width];
[button setTitle:nil];
}
//生成9个小图片,并加到手表的图片缓存里
[[WKInterfaceDevice currentDevice] removeAllCachedImages];
self.wholeImage = [UIImage imageNamed:@"me"];
int image_width = (int)(MIN(_wholeImage.size.width, _wholeImage.size.height)/3);
self.subImages = [NSMutableArray array];
for (int i = 0; i<9; i++) {
int x = i%3;
int y = i/3;
_vector[x][y] = i;
UIImage *clip = [self crop:_wholeImage rect:CGRectMake(x*image_width, y*image_width, image_width, image_width)];
[_subImages addObject:clip];
[[WKInterfaceDevice currentDevice] addCachedImage:clip name:[NSString stringWithFormat:@"clip_%d",i]];
}
//随机交换_difficulty次
for (int i = 0; i<_difficulty; i++) {
[self randomSwap];
}
//按照_vector的布局,显示buttons背景
for (int i = 0; i<[_subImages count]&&i<[_buttons count]; i++) {
int x = i%3;
int y = i/3;
[(WKInterfaceButton *)_buttons[i] setBackgroundImageNamed:[NSString stringWithFormat:@"clip_%d",(int)_vector[x][y]]];
}
//再来一次menu
[self addMenuItemWithItemIcon:WKMenuItemIconRepeat title:@"再来一次" action:@selector(restart)];
}
- (void)willActivate {
// This method is called when watch view controller is about to be visible to user
[super willActivate];
}
- (void)didDeactivate {
// This method is called when watch view controller is no longer visible
[super didDeactivate];
}
//重新开始游戏,重新生成_vector
- (void)restart{
for (int i = 0; i<9; i++) {
int x = i%3;
int y = i/3;
_vector[x][y] = i;
}
for (int i = 0; i<_difficulty; i++) {
[self randomSwap];
}
for (int i = 0; i<[_subImages count]&&i<[_buttons count]; i++) {
int x = i%3;
int y = i/3;
[(WKInterfaceButton *)_buttons[i] setBackgroundImageNamed:[NSString stringWithFormat:@"clip_%d",(int)_vector[x][y]]];
}
_finished = NO;
_step = 0;
[self setTitle:@"0"];
}
//判断游戏是否结束,如果_vector还原到了原始状态,则拼图完成
- (BOOL)isFinished{
for (int i = 0; i<9; i++) {
int x = i%3;
int y = i/3;
if (_vector[x][y] != i){
return NO;
}
}
return YES;
}
//随机交换两个相邻的图片
- (void)randomSwap{
while (YES) {
int index1 = arc4random()%9;
int index2 = arc4random()%9;
if (index1 != index2 && [self canSwap:index1 index2:index2]) {
[self swap:index1 index2:index2 updateButton:NO];
return;
}
}
}
//判断index1和index2位置是否可以交换,只有相邻位置可以交换
- (BOOL)canSwap:(NSInteger)index1 index2:(NSInteger)index2{
if ((index1%3) == (index2%3) && ABS((index1/3) - (index2/3)) < 2) {//上下
return YES;
}
if (ABS((index1%3) - (index2%3)) == 1 && (index1/3) == (index2/3)) {//同一行距离为1的两个
return YES;
}
return NO;
}
//交换index1和index2位置的图片
- (void)swap:(NSInteger)index1 index2:(NSInteger)index2 updateButton:(BOOL)updateButton{
NSInteger x1 = index1%3;
NSInteger y1 = index1/3;
NSInteger x2 = index2%3;
NSInteger y2 = index2/3;
int image1 = _vector[x1][y1];
int image2 = _vector[x2][y2];
_vector[x1][y1] = image2;
_vector[x2][y2] = image1;
if (updateButton) {
_step ++;
[self setTitle:[NSString stringWithFormat:@"%d",_step]];
WKInterfaceButton *button1 = _buttons[index1];
WKInterfaceButton *button2 = _buttons[index2];
[button1 setTitle:nil];
[button2 setTitle:nil];
[button1 setBackgroundImageNamed:[NSString stringWithFormat:@"clip_%d",image2]];
[button2 setBackgroundImageNamed:[NSString stringWithFormat:@"clip_%d",image1]];
}
}
//点击了第index个button
- (void)selectButton:(NSInteger)index{
//已经完成拼图
if (_finished) {
return;
}
//点击了上一次的button
if (_selectedButtonIndex == index+1) {
WKInterfaceButton *button = _buttons[index];
[button setTitle:nil];
_selectedButtonIndex = 0;
return;
}
//已经有选中的button,如果可以的话,交换当前button和上次选中button的图片
if (_selectedButtonIndex>0) {
if (![self canSwap:index index2:_selectedButtonIndex-1]) {
[self setTitle:@"只能移动相邻图片"];
return;
}
else{
[self swap:index index2:_selectedButtonIndex-1 updateButton:YES];
_selectedButtonIndex = 0;
if ([self isFinished]) {
_finished = YES;
[self setTitle:@"拼图完成!!!"];
}
}
}
//没有选中的button,当前button标记为选中
else{
_selectedButtonIndex = index+1;
WKInterfaceButton *button = _buttons[index];
[button setTitle:@"👆"];
}
}
- (IBAction)select1{[self selectButton:0];}
- (IBAction)select2{[self selectButton:1];}
- (IBAction)select3{[self selectButton:2];}
- (IBAction)select4{[self selectButton:3];}
- (IBAction)select5{[self selectButton:4];}
- (IBAction)select6{[self selectButton:5];}
- (IBAction)select7{[self selectButton:6];}
- (IBAction)select8{[self selectButton:7];}
- (IBAction)select9{[self selectButton:8];}
//裁剪图片
-(UIImage*)crop:(UIImage *)image rect:(CGRect)rect
{
CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
UIGraphicsBeginImageContext(smallBounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, smallBounds, subImageRef);
UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
return smallImage;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment