1

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?

Sonu
  • 937
  • 1
  • 10
  • 39

1 Answers1

1
   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;
                }

        }
Sunny Shah
  • 12,990
  • 9
  • 50
  • 86
  • thanks buddy..but if the word is GOOD than what?i mean the letter O comes for two time..what i want is i want letter as per the word. – Sonu Jun 02 '14 at 04:39
  • I have two nsmutablearray..one for abcd and another for words. ArrOfWords = [[NSMutableArray alloc] initWithObjects:@"Creature",@"Home",@"Beauty",@"Good",nil];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]; – Sonu Jun 02 '14 at 04:59
  • so you only want repeat o words? – Sunny Shah Jun 02 '14 at 05:03
  • i have nsmutablearray with different word,as i mentioned...so,i want letters as per given word...if word is good than i want o two times. – Sonu Jun 02 '14 at 05:05
  • suppose i take good word there is no word b so there is no problem when b comes two time – Sunny Shah Jun 02 '14 at 05:16
  • No,it's absolutely fine.if b comes two time...i need all letters in the given word.and rest of letter come what ever time..it doesn't matter. – Sonu Jun 02 '14 at 05:19
  • @Suuny Shah : is there any solution? – Sonu Jun 02 '14 at 06:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54900/discussion-between-sunny-shah-and-sweeta). – Sunny Shah Jun 02 '14 at 06:16