在我的第一个目标C项目中,我正在处理一个游戏大师一般形式的代码。我目前有一个程序,允许计算机生成六种随机“颜色”中的四种,我想知道是否有人能告诉我如何修改它,这样一个人就可以键入它的四种颜色来猜测计算机选择的颜色,然后打印一个序列(一种)。对于每一个正确的猜测,每个错误的猜测都有一个"o“)。
#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发布于 2013-10-04 06:13:36
我没有完全明白你想要达到什么目的。但是我想你建议,为了随机排列你的可变数组,在下面这样做,每当你执行代码时,你的可变数组的顺序会被随机改变,因此你可以在此基础上根据用户的输入获取值并相应地写检查器:--
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);
}https://stackoverflow.com/questions/19169785
复制相似问题