私の最初の Objective C プロジェクトでは、Mastermind ゲームの一般的な形式のコードを扱っています。私は現在、コンピューターが 6 つのランダムな「色」のうち 4 つを生成できるようにするプログラムを持っています。誰かがそれを変更して、選択した 4 つの色を入力して、コンピューターが選択した色を推測できるようにする方法を教えてもらえないかと思っていました。次に、シーケンスを出力します (正しい推測ごとに「.」が 1 つ、間違った推測ごとに「o」が 1 つ)。
#import "Gameplay.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Gameplay * gp = [[Gameplay alloc] init];
}
return 0;
}
#import <Foundation/Foundation.h>
@interface Gameplay : NSObject
-(NSMutableArray *) board;
@end
@implementation Gameplay
- (id)init {
self = [super init];
if (self)
{
[self board];
}
return self;
}
-(NSMutableArray *) board{
NSMutableArray *colors = [NSMutableArray arrayWithObjects:@"r",@"b",@"y",@"g",@"o",@"p", nil];
NSMutableArray *choose = [[NSMutableArray alloc] initWithCapacity:4];
for (int i = 0; i < 4; i++) {
int randomize = arc4random()%[colors count];
NSString *turn = [[NSString alloc] initWithString:[colors objectAtIndex: randomize]];
[choose addObject: turn];
[colors removeObjectAtIndex: randomize];
}
NSFileHandle(
for(int i = 0;i < 4;i++){
NSLog(@"%@",[choose objectAtIndex:i]);
}
return choose;
}
@end
#import "Gameplay.h"
int main(int argc,
@autoreleasepool {
Gameplay * gp = [[Gameplay alloc] init];
}
return 0;
}
#import <Foundation/Foundation.h>
@interface Gameplay : NSObject
-(NSMutableArray *) board;
@end
@implementation Gameplay
- (id)init {
self = [super init];
if (self)
{
[self board];
}
return self;
}
-(NSMutableArray *) board{
NSMutableArray *colors = [NSMutableArray arrayWithObjects:@"r",@"b",@"y",@"g",@"o",@"p", nil];
NSMutableArray *choose = [[NSMutableArray alloc] initWithCapacity:4];
for (int i = 0; i < 4; i++) {
int randomize = arc4random()%[colors count];
NSString *turn = [[NSString alloc] initWithString:[colors objectAtIndex: randomize]];
[choose addObject: turn];
[colors removeObjectAtIndex: randomize];
}
NSFileHandle(
for(int i = 0;i < 4;i++){
NSLog(@"%@",[choose objectAtIndex:i]);
}
return choose;
}
@end