Last active
August 29, 2015 14:00
-
-
Save coderd00d/11159694 to your computer and use it in GitHub Desktop.
Challenge 159 - Daily Programmer - Rock Paper Scissor Lizard Spock Part 1
This file contains 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
For reddit/r/dailyprogrammer on 4-21-2014 Challenge #159 |
This file contains 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
// | |
// main.m | |
// 159 RPSLS part 1 | |
// | |
#import <Foundation/Foundation.h> | |
#import "Player.h" | |
#import "RPSLSEngine.h" | |
int main(int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
RPSLSEngine *engine = [[RPSLSEngine alloc] init]; | |
bool isGameOver = false; | |
Player *one = [[Player alloc] initWithName: @"Human"]; | |
Player *two = [[Player alloc] init]; | |
while (!isGameOver) { | |
[one PromptForMove]; | |
if ([one move] < Rock || [one move] > Spock) { | |
isGameOver = true; | |
} else { | |
[engine playedAGame]; | |
[two PickRandomMove]; | |
[one DisplayMoveText]; | |
[two DisplayMoveText]; | |
[engine comparePlayer: one toPlayer: two]; | |
} | |
} // game loop | |
printf("\n====== Game Stats ======\n\n"); | |
printf("Games Played: %d\n", [engine gamesPlayed]); | |
printf("%s won: %d games (%%%f win rate)\n", | |
[[one name] UTF8String], | |
[one wins], | |
((float) [one wins] / (float) [engine gamesPlayed] * 100.0)); | |
printf("%s won: %d games (%%%f win rate)\n", | |
[[two name] UTF8String], | |
[two wins], | |
((float) [two wins] / (float) [engine gamesPlayed] * 100.0)); | |
printf("%d ties (%%%f tie rate)\n", | |
[one ties], | |
((float) [one ties] / (float) [engine gamesPlayed] * 100.0)); | |
} | |
return 0; | |
} | |
This file contains 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
// | |
// Player.h | |
// 159 RPSLS part 1 | |
// | |
#import <Foundation/Foundation.h> | |
typedef enum MoveType: NSUInteger { | |
Rock, | |
Paper, | |
Scissor, | |
Lizard, | |
Spock | |
} MoveType; | |
@interface Player : NSObject | |
@property (readonly) int wins; | |
@property (readonly) int ties; | |
@property (readonly) NSString *name; | |
@property (readonly) MoveType move; | |
- (instancetype) initWithName: (NSString *) name; | |
- (NSString *) MoveString; | |
- (void) DisplayMoveText; | |
- (void) PickRandomMove; | |
- (void) PromptForMove; | |
- (void) winAGame; | |
- (void) tieAGame; | |
@end |
This file contains 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
// | |
// Player.m | |
// 159 RPSLS part 1 | |
// | |
#import "Player.h" | |
@implementation Player { | |
NSArray *pick; | |
} | |
@synthesize wins; | |
@synthesize ties; | |
@synthesize name; | |
@synthesize move; | |
- (instancetype) initWithName: (NSString *) n { | |
if (self = [super init]) { | |
name = [n copy]; | |
move = Rock; | |
wins = 0; | |
ties = 0; | |
pick = [NSArray arrayWithObjects: @"Rock", @"Paper", @"Scissors", @"Lizard", @"Spock", nil]; | |
} | |
return self; | |
} | |
- (instancetype) init { | |
return [self initWithName: @"Computer"]; | |
} | |
- (NSString *) MoveString { | |
return [pick objectAtIndex: move]; | |
} | |
- (void) DisplayMoveText { | |
printf("%s picks %s\n", [name UTF8String], [[pick objectAtIndex: move] UTF8String]); | |
} | |
- (void) PickRandomMove { | |
move = arc4random() % 5; | |
} | |
- (void) PromptForMove { | |
char dummy; | |
printf("\n\nEnter Move by typing a number:\n"); | |
printf("0) Rock\n"); | |
printf("1) Paper\n"); | |
printf("2) Scissors\n"); | |
printf("3) Lizard\n"); | |
printf("4) Spock\n"); | |
printf("5) ==Quit Game==\nYour Move>"); | |
scanf("%ld%c", &move, &dummy); | |
printf("\n"); | |
} | |
- (void) winAGame { | |
wins++; | |
} | |
- (void) tieAGame { | |
ties++; | |
} | |
@end |
This file contains 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
// | |
// RPSLSEngine.h | |
// 159 RPSLS part 1 | |
// | |
#import <Foundation/Foundation.h> | |
@class Player; | |
@interface RPSLSEngine : NSObject | |
@property (readonly) int gamesPlayed; | |
- (void) playedAGame; | |
- (void) comparePlayer: (Player *) one toPlayer: (Player *) two; | |
@end |
This file contains 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
// | |
// RPSLSEngine.m | |
// 159 RPSLS part 1 | |
// | |
#import "RPSLSEngine.h" | |
#import "Player.h" | |
@implementation RPSLSEngine { | |
NSArray *verbs; | |
NSArray *results; | |
} | |
@synthesize gamesPlayed; | |
- (instancetype) init { | |
if (self = [super init]) { | |
verbs = [NSArray arrayWithObjects: | |
@"ties", @"cut", @"covers", @"crushes", | |
@"poisons", @"smashes", @"decapitate", @"eats", | |
@"disproves", @"vaporizes", nil]; | |
results = [NSArray arrayWithObjects: | |
@[@0, @-2, @3, @3, @-9], | |
@[@2, @0, @-1, @-7, @8], | |
@[@-3, @1, @0, @6, @-5], | |
@[@-3, @7, @-6, @0, @4], | |
@[@9, @-8, @5, @-4, @0], nil]; | |
gamesPlayed = 0; | |
} | |
return self; | |
} | |
- (void) playedAGame { | |
gamesPlayed++; | |
} | |
- (void) comparePlayer: (Player *) one toPlayer: (Player *) two { | |
int result = [[[results objectAtIndex: (NSUInteger) [one move]] objectAtIndex: (NSUInteger) [two move]] intValue]; | |
if (result == 0) { | |
printf("%s %s %s\n", [[one name] UTF8String], [[verbs objectAtIndex: result] UTF8String], [[two name] UTF8String]); | |
[one tieAGame]; | |
[two tieAGame]; | |
} else if (result < 0) { | |
printf("%s %s %s\n", [[two MoveString] UTF8String], [[verbs objectAtIndex: (result * - 1)] UTF8String], [[one MoveString] UTF8String]); | |
printf("%s wins!\n", [[two name] UTF8String]); | |
[two winAGame]; | |
} else { | |
printf("%s %s %s\n", [[one MoveString] UTF8String], [[verbs objectAtIndex: result] UTF8String], [[two MoveString] UTF8String]); | |
printf("%s wins!\n", [[one name] UTF8String]); | |
[one winAGame]; | |
} | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment