前回の正しく重複なしに作成できてるかのチェックをいれたソースは以下にアップ。
------------
サンプルプロジェクト:ChimpanzeeExaminerTest2.zip
まあ、1000回やっても重複ないし問題ないでしょう。
いよいよChimpanzee-03に組み込んでみる。
プロジェクトを立ち上げてファイル>新規ファイル...メニューで出てきたダイアログでNSObject subclassを選択。

次に出るダイアログは適当に、そのまま「完了」ボタンでもいいし、作成場所を変更するのもよし。
これでプロジェクトにChimpanzeeExaminer.hとChimpanzeeExaminer.mが組み込まれた。

ChimpanzeeExaminer.hにはChimpanzeeExaminerTestでstatic変数として持っていた
int BLOCK_COUNT;
int curtNumber;
int* locations;
を
@interface ChimpanzeeExaminer : NSObject {
}
に入れ
const int* startGame(int countOfNumbers)
をObjective Cのメソッド定義に変更して宣言する。ついでにjudgeも追加。ま~メッセージ経由なんでヘッダーで定義いらないっちゃいらないけどな...
- (const int*)startGame:(int)countOfNumbers;
-(BOOL)judge:(int)location;
ChimpanzeeExaminer.mにはChimpanzeeExaminerを初期化するメソッドと削除するメソッドを用意。
- (void)init
- (void)dealloc
こいつらはNSObjectですでに定義されているものなので、それぞれ
- (void)init
{
[super init];
・
自分の変数の初期化
・
}
- (void)dealloc
{
・
自分の変数の削除処理
・
[super dealloc];
}
として親クラスのメソッドを呼び出しておくこと。
- (const int*)startGame:(int)countOfNumbers
はChimpanzeeExaminerTestからstartGame関数をコピーして最初の
const int* startGame(int countOfNumbers)
の表記を変えるだけ。中身はそのまま使える。judgeも「iPhoneアプリ開発、その(28)」からひっぱてきて、引数をintに変えたものに変更すればいい。
これでChimpanzeeExaminerを使う準備ができた。あとはChimpanzeeGameViewController側で使うだけ。まずChimpanzeeGameViewController.hに以下の変数を追加。
int level;
ChimpanzeeExaminer* examiner;
examinerはChimpanzeeExaminerそのもの。levelはボタン順序を何個まで出題するか。あとは
@class ChimpanzeeExaminer;
という宣言で
#import "ChimpanzeeExaminer.h"
をせずにすませる。なぜ#importを避けるかというと依存度を減らすため。ここらへんは大規模開発で意味が出てくる。興味がある人は
新装版 大規模C++ソフトウェアデザイン
を読みましょう。
次にChimpanzeeGameViewController.m。こっちは必ず
#import "ChimpanzeeExaminer.h"
がいります。あとはrespondToButtonClickで
clickSquence[index] == tag
から
[examiner judge:tag]
とexaminerを使うようになるのと
if (clickSquence[index] == SEQUENCE_TERMINATOR) {
が
if (index == level) {
となること、viewDidLoadで
level = 5;
examiner = [[ChimpanzeeExaminer alloc] init];
を追加すること、startの「いったん手動で順序設定」となってたところをexaminerを使うようにすること
const int* locs = [examiner startGame:level];
for (int i = 0; i < level; i++) {
clickSquence[i] = locs[i];
}
clickSquence[level] = SEQUENCE_TERMINATOR;
deallocでexaminerを削除すること。
[examiner dealloc];
そんなとこ。
ちなみにlevel変数は成功する毎に1つ大きくしてる。
レベルに対応してゲーム開始時間調整しないと、全部を点滅しきれないな。
あ、でもこれはこれで覚えるのの妨害になって逆に面白いかも。
次回はメッセージの日本語化だ!
------------
サンプルプロジェクト:Chimpanzee-04.zip
------------
サンプルプロジェクト:ChimpanzeeExaminerTest2.zip
まあ、1000回やっても重複ないし問題ないでしょう。
いよいよChimpanzee-03に組み込んでみる。
プロジェクトを立ち上げてファイル>新規ファイル...メニューで出てきたダイアログでNSObject subclassを選択。

次に出るダイアログは適当に、そのまま「完了」ボタンでもいいし、作成場所を変更するのもよし。
これでプロジェクトにChimpanzeeExaminer.hとChimpanzeeExaminer.mが組み込まれた。

ChimpanzeeExaminer.hにはChimpanzeeExaminerTestでstatic変数として持っていた
int BLOCK_COUNT;
int curtNumber;
int* locations;
を
@interface ChimpanzeeExaminer : NSObject {
}
に入れ
const int* startGame(int countOfNumbers)
をObjective Cのメソッド定義に変更して宣言する。ついでにjudgeも追加。ま~メッセージ経由なんでヘッダーで定義いらないっちゃいらないけどな...
- (const int*)startGame:(int)countOfNumbers;
-(BOOL)judge:(int)location;
ChimpanzeeExaminer.mにはChimpanzeeExaminerを初期化するメソッドと削除するメソッドを用意。
- (void)init
- (void)dealloc
こいつらはNSObjectですでに定義されているものなので、それぞれ
- (void)init
{
[super init];
・
自分の変数の初期化
・
}
- (void)dealloc
{
・
自分の変数の削除処理
・
[super dealloc];
}
として親クラスのメソッドを呼び出しておくこと。
- (const int*)startGame:(int)countOfNumbers
はChimpanzeeExaminerTestからstartGame関数をコピーして最初の
const int* startGame(int countOfNumbers)
の表記を変えるだけ。中身はそのまま使える。judgeも「iPhoneアプリ開発、その(28)」からひっぱてきて、引数をintに変えたものに変更すればいい。
これでChimpanzeeExaminerを使う準備ができた。あとはChimpanzeeGameViewController側で使うだけ。まずChimpanzeeGameViewController.hに以下の変数を追加。
int level;
ChimpanzeeExaminer* examiner;
examinerはChimpanzeeExaminerそのもの。levelはボタン順序を何個まで出題するか。あとは
@class ChimpanzeeExaminer;
という宣言で
#import "ChimpanzeeExaminer.h"
をせずにすませる。なぜ#importを避けるかというと依存度を減らすため。ここらへんは大規模開発で意味が出てくる。興味がある人は
新装版 大規模C++ソフトウェアデザイン
を読みましょう。
次にChimpanzeeGameViewController.m。こっちは必ず
#import "ChimpanzeeExaminer.h"
がいります。あとはrespondToButtonClickで
clickSquence[index] == tag
から
[examiner judge:tag]
とexaminerを使うようになるのと
if (clickSquence[index] == SEQUENCE_TERMINATOR) {
が
if (index == level) {
となること、viewDidLoadで
level = 5;
examiner = [[ChimpanzeeExaminer alloc] init];
を追加すること、startの「いったん手動で順序設定」となってたところをexaminerを使うようにすること
const int* locs = [examiner startGame:level];
for (int i = 0; i < level; i++) {
clickSquence[i] = locs[i];
}
clickSquence[level] = SEQUENCE_TERMINATOR;
deallocでexaminerを削除すること。
[examiner dealloc];
そんなとこ。
ちなみにlevel変数は成功する毎に1つ大きくしてる。
レベルに対応してゲーム開始時間調整しないと、全部を点滅しきれないな。
あ、でもこれはこれで覚えるのの妨害になって逆に面白いかも。
次回はメッセージの日本語化だ!
------------
サンプルプロジェクト:Chimpanzee-04.zip