0

私の最初の 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
4

1 に答える 1

-1

あなたが達成したいことを正確に質問できませんでした。しかし、可変配列をランダムに配置するには、以下のように試してみることをお勧めします.コードを実行するたびに、可変配列の順序がランダムに変更されるため、それに基づいて値を取得し、それに応じてチェッカーを作成できます.ユーザー入力に基づいて.:--

NSMutableArray *mutArr=[NSMutableArray arrayWithObjects:@"red",@"green",@"blue",@"white",nil];
int i=0;
for (i=0; i<[mutArr count]; i++)
{
NSUInteger rand= (arc4random() %[mutArr count]);
    [mutArr exchangeObjectAtIndex:i withObjectAtIndex:rand];
    NSLog(@"%@",mutArr);
}
于 2013-10-04T06:13:36.907 に答える