Sonu
Sonu

Reputation: 957

How to generate random letters as per the word

I’m developing word puzzle game,where one word is given.according to that word the random letters are generated…for that, i applied below logic but some how the random letters not generated as per the 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 i =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 any one suggest me that where i made mistake?

Upvotes: 1

Views: 201

Answers (1)

Sunny Shah
Sunny Shah

Reputation: 13020

   You should generate the 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];
            for (int i = 0; i < [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];
            for (int i = 0; i < [dataString length]; i++) {
                [arrayChar addObject:[NSString stringWithFormat:@"%C", [dataString characterAtIndex:i]]];
            }
            for(int i =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;
                }

        }

Upvotes: 1

Related Questions