トップ «前の日記(2012-07-22) 最新 次の日記(2012-07-24)» 編集

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|

2012-07-23 [iOS]15パズル(3)

1個の駒の動作が完成すれば、15個も大丈夫はずだ。

ただ、駒に印がないと区別がつかないので、数字のラベルを貼付けた。

また、既に駒があるマスには移動しないように、マスにフラグを持たせた。でも、ちょっと、フラグの扱いが汚いね。今後の課題としよう。

駒に番号のラベルを貼付けた。

@implementation GamePieceView
...
- (void)_init
{
    static NSInteger    count = 0;
    self.delegate = nil;
    UILabel *label = [[UILabel alloc]
                      initWithFrame:CGRectMake(self.bounds.origin.x + 2.0,
                                               self.bounds.origin.y + 2.0,
                                               self.bounds.size.width - 4.0,
                                               self.bounds.size.height - 4.0)];
    label.text = [[NSString alloc] initWithFormat:@"%d", count++];
    label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor orangeColor];
    [self addSubview:label];
}
...
@end

マスにフラグを持たせた。

@interface GameSquare : NSObject
...
@property (nonatomic, assign) BOOL      isEmpty;
...
@end

駒を15個配置。

@implementation GameBoardView
...
- (void)setupWithDelegate:(id)delegate
{
    ....
    self.squaresArray = [[NSMutableArray alloc] init];
    for (int i=0; i < 16; i++) {
        GameSquare  *square = [[GameSquare alloc] initWithFrame:rect[i]];
        square.index = i;
        if (i != 15) {
            square.isEmpty = NO;  ←フラグを設定
        }
        else {
            square.isEmpty = YES;  ←フラグを設定
        }
        [self.squaresArray addObject:square];
    }
    
    self.pieceViewArray = [[NSMutableArray alloc] init];
    for (int i=0; i < 15; i++) {    ←駒を15個配置。
        GamePieceView   *pieceView = [[GamePieceView alloc] initWithFrame:rect[i]];
        pieceView.delegate = delegate;
        [self addSubview:pieceView];
        [self.pieceViewArray addObject:pieceView];
    }
}
...
@end

異動先のマスに駒があったら、元に戻る。

@interface GameController ()
@property(nonatomic, weak) GameSquare       *square;
...
@end
 
- (void)gameBoardViewTouchDown:(GameBoardView *)gameBoardView location:(CGPoint)touchPt taps:(int)taps event:(UIEvent*)event
{
    GameSquare      *square = [self.gameBoardView squareAtPoint:touchPt];
    if (square) {
        self.square = square;  ←元のマスを覚えておく
    }
    ....
}
 
- (void)gameBoardViewTouchUp:(GameBoardView *)gameBoardView location:(CGPoint)touchPt taps:(int)taps event:(UIEvent*)event
{
    if (self.pieceView) {
        GameSquare      *square = [self.gameBoardView squareAtPoint:touchPt];
        if (square.isEmpty) {  ←駒がなかったら移動
            [self.pieceView moveWithSquare:square];
            self.square.isEmpty = YES;
            square.isEmpty = NO;
        }
        else {  ←元のマスに戻る
            [self.pieceView moveWithSquare:self.square];
        }
    }
    self.pieceView = nil;
}
...
@end
15パズル

_ ソースコード

GitHubからどうぞ。
https://github.com/murakami/workbook/tree/master/ios/FifteenPuzzle - GitHub

_ 【Cocoa練習帳】

http://www.bitz.co.jp/weblog/
http://ameblo.jp/bitz/(ミラー・サイト)

トップ «前の日記(2012-07-22) 最新 次の日記(2012-07-24)» 編集