Skip to content

Instantly share code, notes, and snippets.

@boredzo
Created May 11, 2014 21:25
Show Gist options
  • Save boredzo/edfba48ca9b52d7c09d8 to your computer and use it in GitHub Desktop.
Save boredzo/edfba48ca9b52d7c09d8 to your computer and use it in GitHub Desktop.
Card dealing command-line program
#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