How to distribute cards until the entire card stack/pack is distributed based on shuffling/random numbers, with a click of a button?

StackOverflow https://stackoverflow.com/questions/11115205

質問

I am creating my first card game, and need to shuffle the cards. So for starters, I have created a shuffle button for a sample stack of 4 values with the line:

int i = arc4random() % 4;
NSLog(@"%d", i);

Shuffling works well, but I want this button to allow the user to distribute random cards until there are no more cards in the stack. Each time I click this sample button, I need NSLoged result to be something like 3, then 1, then 0, then 2, then "No more cards", for example (instead of a list of four random numbers and a message).

Is there a simple way to "distribute a number" randomly with each click of a button?

Also, does arc4random generate real random numbers or pseudo-random numbers? I have read a lot of threads about it, and it doesn't seem so clear. What would be the best way to randomize numbers?

役に立ちましたか?

解決

You can create an array with all the possible elements in the random space, for example:

NSMutableArray *randomNumber = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1],[NSNumber numberWithInt:2],[NSNumber numberWithInt:3], nil];

Then, you take randomly elements of the array, and you remove them after:

for (int i = 0; i < 4; i++){

int randIndex = arc4random()%[randomNumber count];
NSLog(@" %d %d", randIndex, [[randomNumber objectAtIndex:randIndex] intValue]);
[randomNumber removeObjectAtIndex:randIndex];
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top