Created
May 11, 2014 21:25
-
-
Save boredzo/edfba48ca9b52d7c09d8 to your computer and use it in GitHub Desktop.
Card dealing command-line program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
int main(int argc, char *argv[]) { | |
@autoreleasepool { | |
NSArray *suits = @[ | |
@"♠", @"♡", @"♢", @"♣" | |
]; | |
NSArray *ranks = @[ | |
@"A", | |
@"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", | |
@"J", @"Q", @"K", | |
]; | |
NSMutableArray *deck = [NSMutableArray arrayWithCapacity:52]; | |
for (NSString *suit in suits) { | |
for (NSString *rank in ranks) { | |
[deck addObject:[rank stringByAppendingString:suit]]; | |
} | |
} | |
bool keepDealing = true; | |
while (keepDealing && deck.count > 0) { | |
NSUInteger idx = arc4random_uniform(deck.count); | |
NSString *card = deck[idx]; | |
[deck removeObjectAtIndex:idx]; | |
NSLog(@"%@ - Deal again? ('quit' to stop)", card); | |
enum { bufSize = 16 }; | |
char buf[bufSize] = ""; | |
fgets(buf, bufSize, stdin); | |
keepDealing = (0 != strcmp(buf, "quit")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment