首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在目标C中使用输入和打印?

问如何在目标C中使用输入和打印?
EN

Stack Overflow用户
提问于 2013-10-03 21:45:16
回答 1查看 93关注 0票数 0

在我的第一个目标C项目中,我正在处理一个游戏大师一般形式的代码。我目前有一个程序,允许计算机生成六种随机“颜色”中的四种,我想知道是否有人能告诉我如何修改它,这样一个人就可以键入它的四种颜色来猜测计算机选择的颜色,然后打印一个序列(一种)。对于每一个正确的猜测,每个错误的猜测都有一个"o“)。

代码语言:javascript
复制
#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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-04 06:13:36

我没有完全明白你想要达到什么目的。但是我想你建议,为了随机排列你的可变数组,在下面这样做,每当你执行代码时,你的可变数组的顺序会被随机改变,因此你可以在此基础上根据用户的输入获取值并相应地写检查器:--

代码语言:javascript
复制
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);
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19169785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档