I am creating a random number game that has 9 different numbers and i want to randomise the numbers so they are randomised with non duplicates (e.g., 1,4,2,3,6,5,9,7,8) but at the moment i am just getting 9random numbers between 1 and 9 with multiple duplicates. Can anyone suggest anything without changing the existing code too much.
Thank you.
-(IBAction)Randomise
{
int rNumber1 = arc4random_uniform(9);
Text1.text = [NSString stringWithFormat: @"%d",rNumber1 + 1];
if ([Text1.text isEqualToString: @"1"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"1.png"]];
}
else if ([Text1.text isEqualToString: @"2"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"2.png"]];
}
else if ([Text1.text isEqualToString: @"3"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"3.png"]];
}
else if ([Text1.text isEqualToString: @"4"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"4.png"]];
}
else if ([Text1.text isEqualToString: @"5"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"5.png"]];
}
else if ([Text1.text isEqualToString: @"6"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"6.png"]];
}
else if ([Text1.text isEqualToString: @"7"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"7.png"]];
}
else if ([Text1.text isEqualToString: @"8"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"8.png"]];
}
else if ([Text1.text isEqualToString: @"9"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"9.png"]];
}
int rNumber2 = arc4random_uniform(9);
Text2.text = [NSString stringWithFormat: @"%d",rNumber2+1];
if ([Text2.text isEqualToString: @"1"])
{
[AnimatedPicture1 setImage:[UIImage imageNamed:@"1.png"]];
}
else if ([Text2.text isEqualToString: @"2"])
{
[AnimatedPicture1 setImage:[UIImage imageNamed:@"2.png"]];
}
else if ([Text2.text isEqualToString: @"3"])
{
[AnimatedPicture1 setImage:[UIImage imageNamed:@"3.png"]];
}
else if ([Text2.text isEqualToString: @"4"])
{
[AnimatedPicture1 setImage:[UIImage imageNamed:@"4.png"]];
}
else if ([Text2.text isEqualToString: @"5"])
{
[AnimatedPicture1 setImage:[UIImage imageNamed:@"5.png"]];
}
else if ([Text2.text isEqualToString: @"6"])
{
[AnimatedPicture1 setImage:[UIImage imageNamed:@"6.png"]];
}
else if ([Text1.text isEqualToString: @"7"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"7.png"]];
}
else if ([Text1.text isEqualToString: @"8"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"8.png"]];
}
else if ([Text1.text isEqualToString: @"9"])
{
[AnimatedPicture setImage:[UIImage imageNamed:@"9.png"]];
}
int rNumber3 = arc4random_uniform(9);
Text3.text = [NSString stringWithFormat: @"%d",rNumber3+1];
if ([Text3.text isEqualToString: @"1"])
{
[AnimatedPicture2 setImage:[UIImage imageNamed:@"1.png"]];
}
else if ([Text3.text isEqualToString: @"2"])
{
[AnimatedPicture2 setImage:[UIImage imageNamed:@"2.png"]];
}
else if ([Text3.text isEqualToString: @"3"])
{
[AnimatedPicture2 setImage:[UIImage imageNamed:@"3.png"]];
}
else if ([Text3.text isEqualToString: @"4"])
{
[AnimatedPicture2 setImage:[UIImage imageNamed:@"4.png"]];
}
else if ([Text3.text isEqualToString: @"5"])
{
[AnimatedPicture2 setImage:[UIImage imageNamed:@"5.png"]];
}
else if ([Text3.text isEqualToString: @"6"])
{
[AnimatedPicture2 setImage:[UIImage imageNamed:@"6.png"]];
}
else if ([Text3.text isEqualToString: @"7"])
{
[AnimatedPicture2 setImage:[UIImage imageNamed:@"7.png"]];
}
else if ([Text3.text isEqualToString: @"8"])
{
[AnimatedPicture2 setImage:[UIImage imageNamed:@"8.png"]];
}
else if ([Text3.text isEqualToString: @"9"])
{
[AnimatedPicture2 setImage:[UIImage imageNamed:@"9.png"]];
}
int rNumber4 = arc4random_uniform(9);
Text4.text = [NSString stringWithFormat: @"%d",rNumber4+1];
if ([Text4.text isEqualToString: @"1"])
{
[AnimatedPicture3 setImage:[UIImage imageNamed:@"1.png"]];
}
else if ([Text4.text isEqualToString: @"2"])
{
[AnimatedPicture3 setImage:[UIImage imageNamed:@"2.png"]];
}
else if ([Text4.text isEqualToString: @"3"])
{
[AnimatedPicture3 setImage:[UIImage imageNamed:@"3.png"]];
}
else if ([Text4.text isEqualToString: @"4"])
{
[AnimatedPicture3 setImage:[UIImage imageNamed:@"4.png"]];
}
else if ([Text4.text isEqualToString: @"5"])
{
[AnimatedPicture3 setImage:[UIImage imageNamed:@"5.png"]];
}
else if ([Text4.text isEqualToString: @"6"])
{
[AnimatedPicture3 setImage:[UIImage imageNamed:@"6.png"]];
}
else if ([Text4.text isEqualToString: @"7"])
{
[AnimatedPicture3 setImage:[UIImage imageNamed:@"7.png"]];
}
else if ([Text4.text isEqualToString: @"8"])
{
[AnimatedPicture3 setImage:[UIImage imageNamed:@"8.png"]];
}
else if ([Text4.text isEqualToString: @"9"])
{
[AnimatedPicture3 setImage:[UIImage imageNamed:@"9.png"]];
}
int rNumber5 = arc4random_uniform(9);
Text5.text = [NSString stringWithFormat: @"%d",rNumber5+1];
if ([Text5.text isEqualToString: @"1"])
{
[AnimatedPicture4 setImage:[UIImage imageNamed:@"1.png"]];
}
else if ([Text5.text isEqualToString: @"2"])
{
[AnimatedPicture4 setImage:[UIImage imageNamed:@"2.png"]];
}
else if ([Text5.text isEqualToString: @"3"])
{
[AnimatedPicture4 setImage:[UIImage imageNamed:@"3.png"]];
}
else if ([Text5.text isEqualToString: @"4"])
{
[AnimatedPicture4 setImage:[UIImage imageNamed:@"4.png"]];
}
else if ([Text5.text isEqualToString: @"5"])
{
[AnimatedPicture4 setImage:[UIImage imageNamed:@"5.png"]];
}
else if ([Text5.text isEqualToString: @"6"])
{
[AnimatedPicture4 setImage:[UIImage imageNamed:@"6.png"]];
}
else if ([Text5.text isEqualToString: @"7"])
{
[AnimatedPicture4 setImage:[UIImage imageNamed:@"7.png"]];
}
else if ([Text5.text isEqualToString: @"8"])
{
[AnimatedPicture4 setImage:[UIImage imageNamed:@"8.png"]];
}
else if ([Text5.text isEqualToString: @"9"])
{
[AnimatedPicture4 setImage:[UIImage imageNamed:@"9.png"]];
}
int rNumber6 = arc4random_uniform(9);
Text6.text = [NSString stringWithFormat: @"%d",rNumber6+1];
if ([Text6.text isEqualToString: @"1"])
{
[AnimatedPicture5 setImage:[UIImage imageNamed:@"1.png"]];
}
else if ([Text6.text isEqualToString: @"2"])
{
[AnimatedPicture5 setImage:[UIImage imageNamed:@"2.png"]];
}
else if ([Text6.text isEqualToString: @"3"])
{
[AnimatedPicture5 setImage:[UIImage imageNamed:@"3.png"]];
}
else if ([Text6.text isEqualToString: @"4"])
{
[AnimatedPicture5 setImage:[UIImage imageNamed:@"4.png"]];
}
else if ([Text6.text isEqualToString: @"5"])
{
[AnimatedPicture5 setImage:[UIImage imageNamed:@"5.png"]];
}
else if ([Text6.text isEqualToString: @"6"])
{
[AnimatedPicture5 setImage:[UIImage imageNamed:@"6.png"]];
}
else if ([Text6.text isEqualToString: @"7"])
{
[AnimatedPicture5 setImage:[UIImage imageNamed:@"7.png"]];
}
else if ([Text6.text isEqualToString: @"8"])
{
[AnimatedPicture5 setImage:[UIImage imageNamed:@"8.png"]];
}
else if ([Text6.text isEqualToString: @"9"])
{
[AnimatedPicture5 setImage:[UIImage imageNamed:@"9.png"]];
}
int rNumber7 = arc4random_uniform(9);
Text7.text = [NSString stringWithFormat: @"%d",rNumber7+1];
if ([Text7.text isEqualToString: @"1"])
{
[AnimatedPicture6 setImage:[UIImage imageNamed:@"1.png"]];
}
else if ([Text7.text isEqualToString: @"2"])
{
[AnimatedPicture6 setImage:[UIImage imageNamed:@"2.png"]];
}
else if ([Text7.text isEqualToString: @"3"])
{
[AnimatedPicture6 setImage:[UIImage imageNamed:@"3.png"]];
}
else if ([Text7.text isEqualToString: @"4"])
{
[AnimatedPicture6 setImage:[UIImage imageNamed:@"4.png"]];
}
else if ([Text7.text isEqualToString: @"5"])
{
[AnimatedPicture6 setImage:[UIImage imageNamed:@"5.png"]];
}
else if ([Text7.text isEqualToString: @"6"])
{
[AnimatedPicture6 setImage:[UIImage imageNamed:@"6.png"]];
}
else if ([Text7.text isEqualToString: @"7"])
{
[AnimatedPicture6 setImage:[UIImage imageNamed:@"7.png"]];
}
else if ([Text7.text isEqualToString: @"8"])
{
[AnimatedPicture6 setImage:[UIImage imageNamed:@"8.png"]];
}
else if ([Text7.text isEqualToString: @"9"])
{
[AnimatedPicture6 setImage:[UIImage imageNamed:@"9.png"]];
}
int rNumber8 = arc4random_uniform(9);
Text8.text = [NSString stringWithFormat: @"%d",rNumber8+1];
if ([Text8.text isEqualToString: @"1"])
{
[AnimatedPicture7 setImage:[UIImage imageNamed:@"1.png"]];
}
else if ([Text8.text isEqualToString: @"2"])
{
[AnimatedPicture7 setImage:[UIImage imageNamed:@"2.png"]];
}
else if ([Text8.text isEqualToString: @"3"])
{
[AnimatedPicture7 setImage:[UIImage imageNamed:@"3.png"]];
}
else if ([Text8.text isEqualToString: @"4"])
{
[AnimatedPicture7 setImage:[UIImage imageNamed:@"4.png"]];
}
else if ([Text8.text isEqualToString: @"5"])
{
[AnimatedPicture7 setImage:[UIImage imageNamed:@"5.png"]];
}
else if ([Text8.text isEqualToString: @"6"])
{
[AnimatedPicture7 setImage:[UIImage imageNamed:@"6.png"]];
}
else if ([Text8.text isEqualToString: @"7"])
{
[AnimatedPicture7 setImage:[UIImage imageNamed:@"7.png"]];
}
else if ([Text8.text isEqualToString: @"8"])
{
[AnimatedPicture7 setImage:[UIImage imageNamed:@"8.png"]];
}
else if ([Text8.text isEqualToString: @"9"])
{
[AnimatedPicture7 setImage:[UIImage imageNamed:@"9.png"]];
}
int rNumber9 = arc4random_uniform(9);
Text9.text = [NSString stringWithFormat: @"%d",rNumber9+1];
if ([Text9.text isEqualToString: @"1"])
{
[AnimatedPicture8 setImage:[UIImage imageNamed:@"1.png"]];
}
else if ([Text9.text isEqualToString: @"2"])
{
[AnimatedPicture8 setImage:[UIImage imageNamed:@"2.png"]];
}
else if ([Text9.text isEqualToString: @"3"])
{
[AnimatedPicture8 setImage:[UIImage imageNamed:@"3.png"]];
}
else if ([Text9.text isEqualToString: @"4"])
{
[AnimatedPicture8 setImage:[UIImage imageNamed:@"4.png"]];
}
else if ([Text9.text isEqualToString: @"5"])
{
[AnimatedPicture8 setImage:[UIImage imageNamed:@"5.png"]];
}
else if ([Text9.text isEqualToString: @"6"])
{
[AnimatedPicture8 setImage:[UIImage imageNamed:@"6.png"]];
}
else if ([Text9.text isEqualToString: @"7"])
{
[AnimatedPicture8 setImage:[UIImage imageNamed:@"7.png"]];
}
else if ([Text9.text isEqualToString: @"8"])
{
[AnimatedPicture8 setImage:[UIImage imageNamed:@"8.png"]];
}
else if ([Text9.text isEqualToString: @"9"])
{
[AnimatedPicture8 setImage:[UIImage imageNamed:@"9.png"]];
}
AnimatedPicture.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"], nil];
AnimatedPicture1.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"], nil];
AnimatedPicture2.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"], nil];
AnimatedPicture3.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"], nil];
AnimatedPicture4.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"], nil];
AnimatedPicture5.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"], nil];
AnimatedPicture6.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"], nil];
AnimatedPicture7.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"], nil];
AnimatedPicture8.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"], nil];
[AnimatedPicture setAnimationRepeatCount:3];
AnimatedPicture.animationDuration = 1.0;
[AnimatedPicture startAnimating];
[AnimatedPicture1 setAnimationRepeatCount:3];
AnimatedPicture1.animationDuration = 1.0;
[AnimatedPicture1 startAnimating];
[AnimatedPicture2 setAnimationRepeatCount:3];
AnimatedPicture2.animationDuration = 1.0;
[AnimatedPicture2 startAnimating];
[AnimatedPicture3 setAnimationRepeatCount:3];
AnimatedPicture3.animationDuration = 1.0;
[AnimatedPicture3 startAnimating];
[AnimatedPicture4 setAnimationRepeatCount:3];
AnimatedPicture4.animationDuration = 1.0;
[AnimatedPicture4 startAnimating];
[AnimatedPicture5 setAnimationRepeatCount:3];
AnimatedPicture5.animationDuration = 1.0;
[AnimatedPicture5 startAnimating];
[AnimatedPicture6 setAnimationRepeatCount:3];
AnimatedPicture6.animationDuration = 1.0;
[AnimatedPicture6 startAnimating];
[AnimatedPicture7 setAnimationRepeatCount:3];
AnimatedPicture7.animationDuration = 1.0;
[AnimatedPicture7 startAnimating];
[AnimatedPicture8 setAnimationRepeatCount:3];
AnimatedPicture8.animationDuration = 1.0;
[AnimatedPicture8 startAnimating];
}
Aucun commentaire:
Enregistrer un commentaire