Thursday, August 22, 2013

Stanford's Coding Together Assignment 1: Matchisimo - Is Done Done And Done! :>

I just finished Stanford's Coding Together Assignment: 1 - Matchisimo!

Below is a snap shot of what my view from Xcode/iOS Simulator looks like, along with a closeup of the simulator and some of its code.





Below is the code for my implementation files: CardMatchingGame.m and CardGameViewController.m.



//
//  CardMatchingGame.m
//  Matchisimo
//
//  Created by Marguerita de Senna on 8/14/13.
//  Copyright (c) 2013 Marguerita de Senna. All rights reserved.
//

#import "CardMatchingGame.h"

@interface CardMatchingGame()
@property (readwrite, nonatomic) int score;
@property (strong, nonatomic) NSMutableArray *cards; //of Card
@property (readwrite, nonatomic) NSString *flipResultStr;
@property (readwrite, nonatomic) NSUInteger gameMatchType; //describes how many cards to match
@end


@implementation CardMatchingGame


- (NSMutableArray *) cards {
    if (!_cards){
        _cards = [[NSMutableArray alloc] init];
    }
    return _cards;
        
}

#define MATCH_BONUS 4
#define MISMATCH_PENALTY 2
#define FLIP_COST 1
#define MATCH_BONUS_THREE 8

- (void)flipCardAtIndex:(NSUInteger)index
{
    //card trying to flip
    Card *card = [self cardAtIndex:index];
    //int counter = 1;

    if (card && !card.isUnplayable){
        self.flipResultStr = [@"Found " stringByAppendingString:card.contents];
        if (!card.isFaceUp){
            for(Card *otherCard in self.cards) {
                if (otherCard.isFaceUp && !otherCard.isUnplayable) {
                    int matchScore = [card match:@[otherCard]];
                    if (matchScore) {
                        card.Unplayable = YES;
                        otherCard.Unplayable = YES;
                        
                        self.score += matchScore * MATCH_BONUS;
                        self.flipResultStr = [card.contents stringByAppendingFormat:@" & %@ MATCHED for %d points!", otherCard.contents, MATCH_BONUS];
    
                    } else {
                        otherCard.faceUp = NO;
                        self.score -= MISMATCH_PENALTY;
                        self.flipResultStr = [card.contents stringByAppendingFormat:@" & %@ DON'T MATCH! %d point penalty!", otherCard.contents, MISMATCH_PENALTY];
                    }
                    break;
                }
            }
            self.score -= FLIP_COST;
        }
        if (card.isFaceUp) {
            self.flipResultStr = @"Keep trying!";
        }
        card.faceUp = !card.isFaceUp;
    }
}





- (void)setGameMatchType:(NSUInteger)gameMatchType {
    _gameMatchType = gameMatchType;
}

-(NSUInteger)getGameMatchType {
    return self.gameMatchType;
}


- (Card *)cardAtIndex:(NSUInteger)index;
{
    return (index < [self.cards count]) ? self.cards[index] : nil;
}


- (id)initWithCardCount:(NSUInteger)count
              usingDeck:(Deck *)deck;
{
    self = [super init];
    
    if(self){
        for (int i = 0; i < count; i++) {
            Card *card = [deck drawRandomCard];
            if (!card){
                self=nil;
            }
            else {
                self.cards[i]=card;
            }
        }
    }
    
    self.gameMatchType = 2;
    return self;
}


@end

--------------------------

//
//  CardGameViewController.m
//  Matchisimo
//
//  Created by Marguerita de Senna on 8/13/13.
//  Copyright (c) 2013 Marguerita de Senna. All rights reserved.
//

#import "CardGameViewController.h"
#import "PlayingCardDeck.h"
#import "CardMatchingGame.h"

@interface CardGameViewController ()
@property (weak, nonatomic) IBOutlet UILabel *flipsLabel;
@property (nonatomic) int flipCount;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
@property (strong, nonatomic) CardMatchingGame *game;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (weak, nonatomic) IBOutlet UILabel *flipResultLabel;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControl;
@property (nonatomic) int gameType;
@property (weak, nonatomic) IBOutlet UILabel *matchTypeLabel;
@end

@implementation CardGameViewController


- (CardMatchingGame *)game
{
    if(!_game) _game = [[CardMatchingGame alloc] initWithCardCount:[self.cardButtons count] usingDeck:[[PlayingCardDeck alloc] init]];
    return _game;
}



- (void)setCardButtons:(NSArray *)cardButtons
{
    _cardButtons = cardButtons;
    [self updateUI];
}

- (void) updateUI
{
    
    for (UIButton *cardButton in self.cardButtons){
        Card *card = [self.game cardAtIndex:[self.cardButtons indexOfObject:cardButton]];
        [cardButton setTitle:card.contents forState:UIControlStateSelected];
        [cardButton setTitle:card.contents forState:UIControlStateSelected|UIControlStateDisabled];
        cardButton.selected = card.isFaceUp;
        cardButton.enabled = !card.isUnplayable;
        cardButton.alpha = (card.isUnplayable ? 0.3 : 1.0);
        
        if(!cardButton.isSelected && cardButton.isEnabled){
            [cardButton setImage:[UIImage imageNamed:@"cardback.jpg"] forState:UIControlStateNormal];
        } else {
            [cardButton setImage:nil forState:UIControlStateNormal];
        }
        
                
    }
    self.scoreLabel.text = [NSString stringWithFormat:@"Score: %d", self.game.score];
    self.flipResultLabel.text = self.game.flipResultStr;
}




- (void) setFlipCount:(int)flipCount
{
    _flipCount = flipCount;
    self.flipsLabel.text = [NSString stringWithFormat:@"Flips: %d", self.flipCount];
}

- (IBAction)flipCard:(UIButton *)sender
{    
    [self.segmentedControl setEnabled:NO forSegmentAtIndex:0];
    [self.segmentedControl setEnabled:NO forSegmentAtIndex:1];
    
    [self.game flipCardAtIndex:[self.cardButtons indexOfObject:sender]];

  
    
    self.flipCount++;
    [self updateUI];
}


- (IBAction)dealButton:(UIButton *)sender {
    self.game = [[CardMatchingGame alloc] initWithCardCount:[self.cardButtons count] usingDeck:[[PlayingCardDeck alloc] init]];
    self.flipCount = 0;
    self.scoreLabel.text = @"Score: 0";
    self.flipResultLabel.text = @"Welcome to Matchismo!";
    
    for (UIButton *cardButton in self.cardButtons){
        cardButton.selected = NO;
        cardButton.enabled = YES;
        cardButton.alpha = 1.0;
    }
    [self.segmentedControl setEnabled:YES forSegmentAtIndex:0];
    [self.segmentedControl setEnabled:YES forSegmentAtIndex:1];
    [self updateUI];
}

//Match 2, Match 3
- (IBAction)gameSwitch:(UISegmentedControl *)sender
{
    if (self.segmentedControl.selectedSegmentIndex == 0) {
        self.matchTypeLabel.text = @"Matching 2";
        [self.segmentedControl setEnabled:NO forSegmentAtIndex:0];
        [self.segmentedControl setEnabled:NO forSegmentAtIndex:1];
        [self.game setGameMatchType:2];
    }
    if (self.segmentedControl.selectedSegmentIndex == 1) {
        self.matchTypeLabel.text = @"Matching 3";
        [self.segmentedControl setEnabled:NO forSegmentAtIndex:0];
        [self.segmentedControl setEnabled:NO forSegmentAtIndex:1];
        [self.game setGameMatchType:3];
    }
    
}

@end



I am well aware there are probably better ways to do this but, hey, it works (I think) :>

What fun projects have you accomplished or cool things have you learned lately? Share in the Comments or Tweet/Instagram us, we'd love to hear! :> :> xoxo


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...