redribben icon

BMGameDynamics

redribben | PRO | 11/22/15 12:01:10 AM UTC | 0 ⭐ | 7494 👁️ | Never ⏰ | []
Objective C |

0 B

|

None

|

0 👍

/

0 👎

//
//  BMGameDynamics.m
//  tic tac toe
//
//  Created by Bogdan Michalchuk on 10/27/15.
//  Copyright © 2015 PDXRR. All rights reserved.
//
 
#import "BMGameDynamics.h"
#import "Values.h"
 
@implementation BMGameDynamics
 
@synthesize tillCatsGame;
@synthesize playersTurn;
 
 
- (void)cleanArray {
    for (int i = 0; i < 9; i++) {
        xoSpots[i] = 0;
    }
}
 
- (void)modifyXOSpotsWithIndex:(int)indexOfSquare {
    if (playersTurn == BMPlayerTypeX) {
        xoSpots[indexOfSquare-1] = BMPlayerTypeX;
        [_pDelegate updateCurrentSquare:indexOfSquare withCurrentPlayer:BMPlayerTypeX];
        playersTurn = BMPlayerTypeO;
    }
    else if (playersTurn == BMPlayerTypeO) {
        xoSpots[indexOfSquare-1] = BMPlayerTypeO;
        [_pDelegate updateCurrentSquare:indexOfSquare withCurrentPlayer:BMPlayerTypeO];
        playersTurn = BMPlayerTypeX;
    }
    [self checkForWinWithMove:YES];
}
 
- (void)checkForWinWithMove:(BOOL)move {
    NSUInteger winArray[24]={0,1,2,  3,4,5,  6,7,8,  0,3,6,  1,4,7,  2,5,8,  0,4,8,  2,4,6};
    for(NSUInteger i=0;i<24; i+=3){
        NSInteger totalPoint = xoSpots[winArray[i]] + xoSpots[winArray[i+1]] + xoSpots[winArray[i+2]];
        switch (totalPoint) {
            case BMPlayerTypeX * 3:
                [_pDelegate updateGameStatusWithWinner:BMPlayerTypeX];
                return;
            case BMPlayerTypeO * 3:
                [_pDelegate updateGameStatusWithWinner:BMPlayerTypeO];
                return;
            default:
                break;
        }
    }
    if (move) {
        // This checks if its a Cats Game
        tillCatsGame = tillCatsGame -1;
        if (tillCatsGame == 0) {
            [_pDelegate updateGameStatusWithWinner:BMPlayerTypeTie];
        }
    }
}
 
- (void)startAnew {
    tillCatsGame = 9;
    [self cleanArray];
}
 
- (void)restoreGame {
    for (int i = 0; i < 9; i++) {
        switch (xoSpots[i]) {
            case BMPlayerTypeX:
                [_pDelegate updateCurrentSquare:(i+1) withCurrentPlayer:BMPlayerTypeX];
                break;
            case BMPlayerTypeO:
                [_pDelegate updateCurrentSquare:(i+1) withCurrentPlayer:BMPlayerTypeO];
                break;
            default:
                break;
        }
    }
    [self checkForWinWithMove:FALSE];
}
 
- (NSArray*)returnSpotArray {
    NSMutableArray* spotsArray = [[NSMutableArray alloc] initWithObjects:@0,@0,@0,@0,@0,@0,@0,@0,@0, nil ];
    for (int i = 0; i < 9; ++i) {
        NSNumber *value = [NSNumber numberWithInt:xoSpots[i]];
        [spotsArray replaceObjectAtIndex:i withObject:value];
    }
    return spotsArray;
}
 
- (void)setXOSpots:(NSArray*)incomingValues {
    for (int i = 0; i < [incomingValues count]; ++i) {
        xoSpots[i] = [[incomingValues objectAtIndex:i] intValue];
    }
}
 
//- (void) saveGameState {
//    NSArray *spotsStateArray = [[NSArray alloc] initWithArray:[self returnSpotArray]]; // For saving each players' moves
//    NSNumber *gameCount = [[NSNumber alloc] initWithInt:tillCatsGame]; // For saving the countdown to Scratch game
//    NSNumber *currentTurn = [[NSNumber alloc] initWithInt:playersTurn]; // For saving the player turn
//    NSDictionary *dataDict = [NSDictionary dictionaryWithObjectsAndKeys:
//                              spotsStateArray, @"squares",
//                              gameCount, @"count",
//                              currentTurn, @"turn",
//                              nil];
//    [NSKeyedArchiver archiveRootObject:dataDict toFile:[self stateFileLocation]];
//}
//
//- (BOOL)loadGameState {
//    if ([[NSFileManager defaultManager] fileExistsAtPath:[self stateFileLocation]]) {
//        NSData *data = [NSData dataWithContentsOfFile:[self stateFileLocation]];
//        NSDictionary *savedData = [NSKeyedUnarchiver unarchiveObjectWithData:data];
//
//        NSArray *spotsStateArray = [[NSArray alloc] initWithArray:[savedData objectForKey:@"squares"]];
//        [self setXOSpots:spotsStateArray];
//        tillCatsGame = [[savedData objectForKey:@"count"] intValue];
//        playersTurn = [[savedData objectForKey:@"turn"] intValue];
//        return 1;
//    }
//    return 0;
//}
 
- (id)initWithCoder:(NSCoder *)decoder {
    self = [super init];
    if (!self) {
        return nil;
    }
    NSArray *myspots = [[NSArray alloc] initWithArray:[decoder decodeObjectForKey:@"squares"]];
    [self setXOSpots:myspots];
//    [self setXOSpots:[decoder decodeObjectForKey:@"squares"]];
    //    [decoder decodeArrayOfObjCType:@encode(int) count:9 at:xoSpots];
    int yes = [decoder decodeIntForKey:@"count"];
    self.tillCatsGame = yes;
    self.playersTurn = [decoder decodeIntForKey:@"turn"];
    return self;
}
 
- (void)encodeWithCoder:(NSCoder *)encoder {
    NSArray *spotsStateArray = [[NSArray alloc] initWithArray:[self returnSpotArray]];
    [encoder encodeObject:spotsStateArray forKey:@"squares"];
//    [encoder encodeArrayOfObjCType:@encode(int) count:9 at:xoSpots];
    [encoder encodeInt:self.tillCatsGame forKey:@"count"];
    [encoder encodeInt:self.playersTurn forKey:@"turn"];
}
 
@end

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎