Last active
December 26, 2015 09:39
-
-
Save jpotts18/7130680 to your computer and use it in GitHub Desktop.
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
// | |
// RookGame.m | |
// Matchismo | |
// | |
// Created by Steve Liddle on 10/19/13. | |
// Copyright (c) 2013 Brigham Young University. All rights reserved. | |
// | |
#import "RookGame.h" | |
#import "RookCardDeck.h" | |
#import "RookCard.h" | |
#define MIN_NEST_SIZE 3 | |
@interface RookGame() | |
@property (nonatomic) NSUInteger numberOfCardsPerPlayer; | |
@property (nonatomic) NSUInteger numberOfCardsInNest; | |
@property (nonatomic) NSUInteger numberOfPlayersInGame; | |
@property (strong, nonatomic) NSMutableArray *nestCards; | |
@property (strong, nonatomic) NSMutableArray *playerCards; | |
@end | |
@implementation RookGame | |
#pragma mark - Initialization | |
- (id)initWithNumberOfPlayers:(NSUInteger)numberOfPlayers { | |
self = [super init]; | |
if (self) { | |
if(numberOfPlayers > MAX_PLAYERS || numberOfPlayers < MIN_PLAYERS){ | |
self = nil; | |
return nil; | |
} else { | |
[[self.playerCards alloc] init]; | |
RookCardDeck *rookCardDeck = [[RookCardDeck alloc] init]; | |
self.numberOfCardsPerPlayer = 57 / numberOfPlayers; | |
self.numberOfCardsInNest = 57 % numberOfPlayers; | |
if(self.numberOfCardsInNest < 3){ | |
self.numberOfCardsInNest = self.numberOfCardsInNest + numberOfPlayers; | |
self.numberOfCardsPerPlayer--; | |
} | |
for (int i = 1; i < numberOfPlayers ; i++) { | |
NSMutableArray *playerCardArray =[rookCardDeck drawRandomCards:self.numberOfCardsPerPlayer]; | |
[self.playerCards addObject:playerCardArray]; | |
} | |
self.highBidderIndex = -1; | |
self.numberOfPlayersInGame = numberOfPlayers; | |
} | |
} | |
return self; | |
} | |
#pragma mark - Getters and setters | |
- (void) setHighBidderIndex:(NSInteger)highBidderIndex{ | |
if(highBidderIndex < [self.playerCards count]){ | |
_highBidderIndex = highBidderIndex; | |
} else { | |
_highBidderIndex = NO_HIGH_BIDDER; | |
} | |
} | |
#pragma mark - Helpers | |
- (RookCard *)cardForPlayer:(NSUInteger)playerIndex atIndex:(NSUInteger)cardIndex { | |
NSMutableArray *userCards = [self.playerCards objectAtIndex:playerIndex]; | |
if(cardIndex < [userCards count]){ | |
RookCard *rookCard = [userCards objectAtIndex:cardIndex]; | |
return rookCard; | |
} | |
return nil; | |
} | |
// This is a helper method that doesn't need to be public | |
- (NSMutableArray *)cardsForPlayer:(NSUInteger)playerIndex { | |
if(playerIndex < [self.playerCards count]){ | |
NSMutableArray *userCards = [self.playerCards objectAtIndex:playerIndex]; | |
return userCards; | |
} | |
return nil; | |
} | |
- (void)flipCardForPlayer:(NSUInteger)playerIndex atIndex:(NSUInteger)cardIndex { | |
RookCard *selectedCard = [self cardForPlayer:playerIndex atIndex:cardIndex]; | |
selectedCard.faceUp = !selectedCard.faceUp; | |
} | |
- (RookCard *)nestCardAtIndex:(NSUInteger)cardIndex { | |
if(cardIndex < [self.nestCards count]){ | |
RookCard *selectedCard = [self.nestCards objectAtIndex:cardIndex]; | |
return selectedCard; | |
} | |
return nil; | |
} | |
- (NSUInteger)numberOfCardsInNest { | |
return [self.nestCards count]; | |
} | |
- (void)swapCard:(NSUInteger)cardIndex withNestCard:(NSUInteger)nestIndex { | |
if(self.highBidderIndex == NO_HIGH_BIDDER){ | |
return; | |
} | |
if((cardIndex < [self.highBidderCards count]) && (nestIndex < [self.nestCards count])){ | |
// get cards | |
RookCard *swapCard = [self.highBidderCards objectAtIndex:cardIndex]; | |
RookCard *tempRookCard = [self.nestCards objectAtIndex:nestIndex]; | |
// | |
[self.highBidderCards setObject:tempRookCard atIndexedSubscript:nestIndex]; | |
[self.nestCards setObject:swapCard atIndexedSubscript:cardIndex]; | |
// | |
// NSMutableArray *newHighBidderCards = [self.highBidderCards copy]; | |
} | |
} | |
// Sort the nestCards and high bidder's playerCards arrays again | |
// You'll want sortedArrayUsingSelector, and you'll want mutableCopy | |
// Assign the sorted, mutable copies back to the propertie | |
-(NSMutableArray *) highBidderCards{ | |
return [self.playerCards objectAtIndex:self.highBidderIndex]; | |
} | |
-(NSMutableArray *)sortCards:(NSMutableArray *)cards { | |
SEL selector = @selector(compareCard:); | |
NSArray *sortedArray = [cards sortedArrayUsingSelector:selector]; | |
return [sortedArray mutableCopy]; | |
} | |
@end |
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
- (NSMutableArray *)drawRandomCards:(NSUInteger)cardCount { | |
// This method is helpful for dealing cards in a Rook game | |
// If cardCount is positive, do this: | |
// 1. Make a mutable array | |
// 2. Draw a random card and add it to the array | |
// 3. Repeat step 2 as many times as cardCount | |
// 4. Sort the array using RookCard's "compareCard:" selector | |
// 5. Return a mutable copy of of the sorted array | |
// Else return nil | |
if(cardCount > 0){ | |
NSMutableArray *randomCards = [[NSMutableArray alloc] init]; | |
for(int i = 0; i < cardCount; i++){ | |
[randomCards addObject:[self drawRandomCard]]; | |
} | |
SEL selector = @selector(compareCard:); | |
NSArray *sortedArray = [randomCards sortedArrayUsingSelector:selector]; | |
return [sortedArray mutableCopy]; | |
} else { | |
return nil; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment