ios - How to generate random letters as per the word -
i’m developing word puzzle game,where 1 word given.according word random letters generated…for that, applied below logic how random letters not generated per given word.
nsmutablearray *arrofabcd = [[nsmutablearray alloc] initwithobjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z", nil];
float x = 70; float y = 100; float width = 30; float height = 20; for(int =0;i<32;i++) { nsuinteger randomindex = arc4random() %(arrofabcd.count); uibutton *btnletter = [uibutton buttonwithtype:uibuttontyperoundedrect]; [btnletter setframe:cgrectmake(x, y, width, height)]; [btnletter settitle:[nsstring stringwithformat:@"%@",arrofabcd[randomindex]]
forstate:uicontrolstatenormal]; [self.view addsubview:btnletter];
x = x + width + 30; if((i+1)%4==0) { x = 70; y = y + height + 20; } }
can 1 suggest me made mistake?
should generate unique random number nsmutablearray* arrofwords = [[nsmutablearray alloc] initwithobjects:@"good",@"home",@"beauty",@"good",nil]; nsmutablearray *arrofabcd = [[nsmutablearray alloc] initwithobjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z", nil]; nsmutablearray *arrduplicate=[[nsmutablearray alloc]init]; float x = 70; float y = 100; float width = 30; float height = 20; nsinteger rowindex=4; nsstring *datastring=[arrofwords objectatindex:0]; nsmutablearray *arrnumber = [[nsmutablearray alloc]init]; (int = 0; < [datastring length]; i++) { nsinteger index=arc4random_uniform(rowindex)+rowindex; nsnumber *number=[nsnumber numberwithint:index]; while ([arrduplicate containsobject:number] ) { nsinteger index=arc4random_uniform(rowindex)+rowindex; number=[nsnumber numberwithint:index]; } [arrnumber addobject:number]; [arrduplicate addobject:number]; } [arrduplicate removeallobjects]; nsmutablearray *arraychar = [nsmutablearray array]; (int = 0; < [datastring length]; i++) { [arraychar addobject:[nsstring stringwithformat:@"%c", [datastring characteratindex:i]]]; } for(int =0;i<32;i++) { nsstring *str=[arrofabcd objectatindex:arc4random_uniform(arrofabcd.count)]; if ([arrnumber containsobject:[nsnumber numberwithint:i]] ) { str=[arraychar objectatindex:arrduplicate.count]; [arrduplicate addobject:str]; } nslog(@"%@",str); uibutton *btnletter = [uibutton buttonwithtype:uibuttontyperoundedrect]; [btnletter setframe:cgrectmake(x, y, width, height)]; [btnletter settitle:str forstate:uicontrolstatenormal]; x = x + width + 30; if((i+1)%4==0) { x = 70; y = y + height + 20; } }
Comments
Post a Comment